如何将 JNI(C/C++ 原生代码)添加到现有的 Android Studio 项目 [英] How to add JNI (C/C++ native code) to existing Android Studio project

查看:39
本文介绍了如何将 JNI(C/C++ 原生代码)添加到现有的 Android Studio 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所说——如何在不破坏当前项目的情况下,将原生代码添加到现有的 Android Studio 项目中,包括 gradle 和 proguard 设置?

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.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

这篇关于如何将 JNI(C/C++ 原生代码)添加到现有的 Android Studio 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆