且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 JNI(C/C++ 原生代码)添加到现有的 Android Studio 项目

更新时间:2021-08-01 05:43:37

从您现有的项目中执行以下步骤:

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.txtapp/proguard-android-optimize.txt

喜欢这个

static {
    System.loadLibrary("--c-file--");
}
private native byte my_jni(Context context, byte[] mByte, int i);

根据您的需要进行更改.上面的例子加载了 c 文件(不带扩展名)——与 gradle 文件中声明的相同,并调用函数 my_jni,传递应用程序的上下文、一些字节数组和一些 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你的所有mallocReleaseByteArrayElements每个GetByteArrayElements等等

注意如何正确地将一些危险值从C返回到Java,比如数组和字符串

Take care how to properly return some dangerous values from C to Java, like arrays and Strings