Android NDK初入

来源:互联网 发布:python dill 编辑:程序博客网 时间:2024/06/06 04:50

1.创建NDK工程

直接New Project,在创建应用界面,勾选includ C++ Support,如下图:

ndk工程创建

创建好之后,AS会为我们生成一个NDK Demo工程,该Demo包含了NDK开发所需要的元素:native-lib.cpp,CMakeLists.txt,于是,我们依葫芦画瓢,自己也定义一个新的lib库和native方法。

2.创建自己的lib库和native方法

首先我们定义需要的native方法,创建一个NDKUtils.java类,该类中定义以下2个方法:

public static native int getAddValue(int a,int b);public static native int getMinusValue(int a,int b);

这时编译器会报错,没有该native方法,直接按照编译器的提示,按下alt+Enter,编译器会在native-lib.cpp中生成该2个方法的空实现,我们拷贝一份native-lib.cpp并重命名为我们自己的lib,例如cjx-native-lib.cpp。去除内部无用的方法,只剩我们需要的2个方法,cjx-native-lib.cpp的内容如下:

#include <jni.h>extern "C"{JNIEXPORT jint JNICALLJava_go_haoshuai_cjx_ndkgo_NDKUtils_getAddValue(JNIEnv *env, jclass type, jint a, jint b) {    int c = a + b;    return c;JNIEXPORT jint JNICALLJava_go_haoshuai_cjx_ndkgo_NDKUtils_getMinusValue(JNIEnv *env, jclass type, jint a, jint b) {    int c = a - b;    return c;}}} 

需要注意的是,native方法,必须包括在extern “C”语句块内部,可以直接一个extern “C”大语句块包含所有native方法,也可以单独包含。

3.编写CMakeLists.txt

CMakeLists.txt,就是构建脚步文件,类似以前的android.mk文件,不过新的构建方式推荐使用CMakeLists.txt,当然也可以继续使用android.mk。

原Demo工程的CMakeLists.txt文件只包含了 native-lib库的申明,如果要编译我们的库,则需要在该文件中也申明我们自己的库

3.1添加我们的库,在原add_library下,再添加一个add_library

add_library( # Sets the name of the library.             cjx-ndk-lib             # Sets the library as a shared library.             SHARED             # Provides a relative path to your source file(s).             # Associated headers in the same location as their source             # file are automatically included.             src/main/cpp/cjx-native-lib.cpp)

3.2添加库依赖关系,在原target_link_libraries下添加一下我们库的依赖,需要依赖log-lib库,该库是android已经提供的日志库,可以在native打印出类似Log.d的效果。

target_link_libraries( # Specifies the target library.                       cjx-ndk-lib                       # Links the target library to the log library                       # included in the NDK.                       ${log-lib} )

加上该依赖之后,我们就可以在native中打印内容了,最终的cjx-native-lib.cpp文件如下:

#include <jni.h>#include <android/log.h>#define  LOG_TAG    "cjx-native"#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)extern "C" {JNIEXPORT jint JNICALLJava_go_haoshuai_cjx_ndkgo_NDKUtils_getAddValue(JNIEnv *env, jclass type, jint a, jint b) {    LOGD("getAddValue start %d,%d :", a, b);    int c = a + b;    LOGD("getAddValue end value %d :", c);    return c;}JNIEXPORT jint JNICALLJava_go_haoshuai_cjx_ndkgo_NDKUtils_getMinusValue(JNIEnv *env, jclass type, jint a, jint b) {    LOGI("getMinusValue start %d,%d :", a, b);    int c = a - b;    LOGI("getAddValue end value  %d:", c);    return c;}}

4.做好以上步骤的准备之后,我们就可以将该库应用与我们的工程中了,在NDKUtils.java中加入库

static {        System.loadLibrary("cjx-ndk-lib");    }

然后在MainActivity测试

// Example of a call to a native methodTextView tv1 = (TextView) findViewById(R.id.sample_text1);TextView tv2 = (TextView) findViewById(R.id.sample_text2);int addValue = NDKUtils.getAddValue(1,2);tv1.setText("ndk getAddValue return value :"+addValue);int minusValue = NDKUtils.getMinusValue(5,3);tv2.setText("ndk getMinusValue return value :"+minusValue);
0 0
原创粉丝点击