最近因项目需要,要将window下的C库移植到android中来,于是决定将NDK好好研究下。之前接触过,但感觉都是皮毛,没有对其提供的demo好好研究过,于是对NDK提供的demo(在android-ndk-r8e/sample目录下)进行系统学习下。
NDK版本:android-ndk-r8e
系统:Ubuntu 12.04 LTS
最简单的demo,hello-jni将整个流程展现出来,麻雀虽小,五脏俱全,下面以NDK提供的hello-jni为例子,对这两天对NDK的学习进行小结。
1、建立android工程,编写java对应JNI层的本地接口:
package com.example.hellojni; import android.app.Activity; import android.widget.TextView; import android.os.Bundle; public class HelloJni extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Create a TextView and set its content. * the text is retrieved by calling a native * function. */ TextView tv = new TextView(this); tv.setText( stringFromJNI() ); //调用本地接口 setContentView(tv); } /* A native method that is implemented by the * 'hello-jni' native library, which is packaged * with this application. */ public native String stringFromJNI();// Java本地接口 /* This is another native method declaration that is *not* * implemented by 'hello-jni'. This is simply to show that * you can declare as many native methods in your Java code * as you want, their implementation is searched in the * currently loaded native libraries only the first time * you call them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! */ public native String unimplementedStringFromJNI(); /* this is used to load the 'hello-jni' library on application * startup. The library has already been unpacked into * /data/data/com.example.hellojni/lib/libhello-jni.so at * installation time by the package manager. */ static { System.loadLibrary("hello-jni"); //加载动态库 } }
2、编写jni层代码,在其中可以调用C代码:
注意:jni层中的方法名一定要以Java_开头,用下划线将包名和类名方法名连接在一起组成jni里面对应Java本地接口名。
#include <string.h> #include <jni.h> #include <stdio.h> #include <android/log.h> //修改日志tag中的值 #define LOG_TAG "logfromc" //日志显示的等级 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) /* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java */ jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { LOGI("hello,world in jni ");//jni 里面添加调试信息方法 return (*env)->NewStringUTF(env, "Hello from JNI !");// 返回字符串 }
3、编写Android.mk文件:
具体解释见android-ndk-r8e/documentation.html文档中的说明。
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FILES := hello-jni.c LOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -llog include $(BUILD_SHARED_LIBRARY)
4、用NDK来编译生成动态库.so文件:
用android-ndk-r8e目录下的ndk-build指令进行编译生成动态.so文件,此时可查看工程目录下自动添加libs和obj这两个目录,其中libs/ armeabi存放此.so文件。
当然可以配置NDK的临时环境变量,直接在jni目录下键入ndk-build即可,配置变量命令行:
NDKROOT=/home/NDK/android-ndk-r8e/ export PATH=$NDKROOT:$PATH
运行程序,手机界面显示Hello from JNI!
two-libs这个demo中使用了静态库来编译生成动态库,折腾了好一会才搞定。
参考资料:
http://developer.android.com/tools/sdk/ndk/index.html
http://hi.baidu.com/joec3/item/0e4fca153eb0e3dcbf904211