如何将JNI(C/C ++本机代码)添加到现有的Android Studio项目 [英] How to add JNI (C/C++ native code) to existing Android Studio project
问题描述
就像标题中所说的那样-如何在不破坏当前项目(包括gradle和proguard设置)的情况下将本机代码添加到现有的Android Studio项目中?
Like the title says - how to add native code to existing Android Studio project, without breaking the current project, including gradle and proguard settings?
推荐答案
从您现有的项目中遵循以下步骤:
Follow this steps from your existing project:
apply plugin: 'com.android.model.application'
model {
android.signingConfigs {
create ("myConfig") {
keyAlias '--your-key-alias--'
keyPassword '--key-password--'
storeFile file('--/path/to/keystore.jks--')
storePassword '--store-password--'
}
}
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "--your.app.name--"
minSdkVersion.apiLevel 19
targetSdkVersion.apiLevel 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles.add(file('proguard-android-optimize.txt'))
proguardFiles.add(file('proguard-rules.pro'))
signingConfig = $("android.signingConfigs.myConfig")
}
}
ndk {
moduleName "--c-file--"
ldLibs.addAll(["android", "log"])
}
}
android.dexOptions {
javaMaxHeapSize "2048m"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
}
您可以复制/粘贴上面的代码,并至少使用"--value--"修改值.匹配您的.
You can copy/paste the above code and modify at least the values with "--value--" to match yours.
它说像这样:
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
对此:
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.9.3'
}
我的示例中的数字 0.9.3 是gradle-experimental的最新版本, .如果尚未,请最终将 gradle-wrapper.properties 中的gradle版本更改为Android Studio推荐的版本.
The number in my example 0.9.3 is the latest version of gradle-experimental to be found here. Eventually change your gradle version in gradle-wrapper.properties to the version recommended by Android Studio if you did not already.
proguard-android-optimize.txt
至app/proguard-android-optimize.txt
像这样
static {
System.loadLibrary("--c-file--");
}
private native byte my_jni(Context context, byte[] mByte, int i);
改变您的需求.上面的示例加载c文件(不带扩展名将其写入)-在gradle文件中声明的文件相同,并调用函数my_jni,传递应用程序的Context,一些字节数组和一些int,期望这些函数返回一个字节.
changing to your needs. The example above loads the c-file (write it without the extension) - the same one declared in the gradle file, and calls the function my_jni, passing the application's Context, some byte array and some int, expecting that the functions returns a byte.
现在,函数的名称以红色突出显示-单击行上的红色指示灯,允许Android Studio创建它Create function ...
.这样会在您的c文件中创建函数,并将焦点更改为它.
Now the name of your function is highlighted in red - allow Android Studio to create it Create function ...
with clicking on the red lamp on the row. This creates the function in your c file and changes focus to it.
进一步阅读提示:
-
注意
free
所有malloc
的内容,对于每个GetByteArrayElements
的ReleaseByteArrayElements
,依此类推
Take care to
free
everything youmalloc
,ReleaseByteArrayElements
for everyGetByteArrayElements
and so on
请注意如何正确地将C中的某些危险值返回给Java,例如数组和字符串
Take care how to properly return some dangerous values from C to Java, like arrays and Strings
这篇关于如何将JNI(C/C ++本机代码)添加到现有的Android Studio项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!