from:http://blog.csdn.net/jianguo_liao19840726/article/details/6719224
Android JNI和NDK关系
1、什么JNI
Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互。JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) 内部运行的 Java 代码能够与用其它编程语言(如 C、C++ 和汇编语言)编写的应用程序和库进行交互操作。
上面过程分为2个部分:
第一、用C语言生成一个库文件。
第二、在java中调用这个库文件的函数。
2、NDK
NDK全称:Native Development Kit。
NDK是一系列工具的集合。
* NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk。这些工具对开发者的帮助是巨大的。
* NDK集成了交叉编译器,并提供了相应的mk文件隔离CPU、平台、ABI等差异,开发人员只需要简单修改mk文件(指出“哪些文件需要编译”、“编译特性要求”等),就可以创建出so。
* NDK可以自动地将so和Java应用一起打包,极大地减轻了开发人员的打包工作。
个人理解,NDK就是能够方便快捷开发.so文件的工具。
JNI的过程比较复杂,生成.so需要大量操作,而NDK就是简化了这个过程。
registerNativeMethods
传统java Jni方式:1.编写带有native方法的Java类;--->2.使用javah命令生成.h头文件;--->3.编写代码实现头文件中的方法,这样的“官方” 流程,但也许有人无法忍受那“丑陋”的方法名称,
通用方式:RegisterNatives方法能帮助你把c/c++中的方法隐射到Java中的native方法,而无需遵循特定的方法命名格式。应用层级的Java类别透过VM而呼叫到本地函数。一般是仰赖VM去寻找*.so里的本地函数。如果需要连续呼叫很多次,每次都需要寻找一遍,会多花许多时间。此时,组件开发者可以自行将本地函数向VM进行登记,VM调registerNativeMethods()函数的用途有二: (1)更有效率去找到函数。 (2)可在执行期间进行抽换。由于gMethods[]是一个<名称,函数指针>对照表,在程序执行时,可多次呼叫registerNativeMethods()函数来更换本地函数之指针,而达到弹性抽换本地函数之目的。
- cpp文件中
- static JNINativeMethod methods[] = {
- {"native_setText","([BJI)I",(void*)native_setText },
- {"native_clearText","(I)I",(void*)native_clearText },
- {"native_create","(I)I",(void*)native_create },
- {"native_start","()I",(void*)native_start },
- {"native_next","()I",(void*)native_next },
- {"native_flush","()I",(void*)native_flush },
- {"native_stop","()I",(void*)native_stop },
- {"native_pause","()I",(void*)native_pause },
- {"native_resume","()I",(void*)native_resume },
- {"native_get_current_position","()J",(void*)native_get_current_position },
- {"native_setSpeed","(I)I",(void*)native_setSpeed },
- {"native_getSynSpeed","()I",(void*)native_getSynSpeed },
- {"native_finalize","()I",(void*)native_finalize },
- {"native_delete","()I",(void*)native_delete },
- };
- //Class path name for Register
- static const char *classPathName = "android/tts/TTS";
- /*
- * Register several native methods for one class.
- */
- static int registerNativeMethods(JNIEnv* env, const char* className,
- JNINativeMethod* gMethods, int numMethods)
- /*
- * Register native methods for all classes we know about.
- *
- * returns JNI_TRUE on success.
- */
- static int registerNatives(JNIEnv* env)
- {
- if (!registerNativeMethods(env, classPathName,
- methods, sizeof(methods) / sizeof(methods[0]))) {
- return JNI_FALSE;
- }
- return JNI_TRUE;
- }
- java文件中
- static native int native_create(int streamType);
- static native int native_setSpeed(int speed);
- static native int native_setText(byte str[],long length,int islast);
- static native int native_clearText(int appID);
- static native int native_start();
- static native int native_next();
- static native int native_flush();
- static native int native_pause();
- static native int native_resume();
- static native int native_stop();
- static native int native_finalize();
- static native int native_delete();
- static native int native_getSynSpeed();
- static native boolean native_isPlaying();
- static native long native_get_current_position();
JNI组件的入口函数——JNI_OnLoad()、JNI_OnUnload()
JNI组件被成功加载和卸载时,会进行函数回调,当VM执行到System.loadLibrary(xxx)函数时,首先会去执行JNI组件中的JNI_OnLoad()函数,而当VM释放该组件时会呼叫JNI_OnUnload()函数。先看示例代码:
- typedef union {
- JNIEnv* env;
- void* venv;
- } UnionJNIEnvToVoid;
- /* This function will be call when the library first be loaded */
- jint JNI_OnLoad(JavaVM* vm, void* reserved)
- {
- UnionJNIEnvToVoid uenv;
- JNIEnv* env = NULL;
- LOGI("JNI_OnLoad!");
- if (vm->GetEnv((void**)&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
- LOGE("ERROR: GetEnv failed");
- return -1;
- }
- env = uenv.env;;