现在的位置: 首页 > 综合 > 正文

Android中JNI 的一些常用说明 JNI_OnLoad registerNatives registerNativeMethods

2013年04月07日 ⁄ 综合 ⁄ 共 3850字 ⁄ 字号 评论关闭

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()函数来更换本地函数之指针,而达到弹性抽换本地函数之目的。

  1. cpp文件中  
  2.   
  3. static JNINativeMethod methods[] = {  
  4.     {"native_setText","([BJI)I",(void*)native_setText },  
  5.     {"native_clearText","(I)I",(void*)native_clearText },  
  6.     {"native_create","(I)I",(void*)native_create },  
  7.     {"native_start","()I",(void*)native_start },  
  8.     {"native_next","()I",(void*)native_next },  
  9.     {"native_flush","()I",(void*)native_flush },  
  10.     {"native_stop","()I",(void*)native_stop },  
  11.     {"native_pause","()I",(void*)native_pause },  
  12.     {"native_resume","()I",(void*)native_resume },  
  13.     {"native_get_current_position","()J",(void*)native_get_current_position },  
  14.     {"native_setSpeed","(I)I",(void*)native_setSpeed },  
  15.     {"native_getSynSpeed","()I",(void*)native_getSynSpeed },  
  16.     {"native_finalize","()I",(void*)native_finalize },  
  17.     {"native_delete","()I",(void*)native_delete },  
  18. };  
  19.   
  20. //Class path name for Register   
  21. static const char *classPathName = "android/tts/TTS";  
  22.   
  23. /* 
  24.  * Register several native methods for one class. 
  25.  */  
  26. static int registerNativeMethods(JNIEnv* env, const char* className,  
  27.                                  JNINativeMethod* gMethods, int numMethods)  
  28. /* 
  29.  * Register native methods for all classes we know about. 
  30.  * 
  31.  * returns JNI_TRUE on success. 
  32.  */  
  33. static int registerNatives(JNIEnv* env)  
  34. {  
  35.     if (!registerNativeMethods(env, classPathName,  
  36.                                methods, sizeof(methods) / sizeof(methods[0]))) {  
  37.         return JNI_FALSE;  
  38.     }  
  39.   
  40.   
  41.     return JNI_TRUE;  
  42. }  
  43.   
  44. java文件中  
  45.    
  46.     static native int native_create(int streamType);  
  47.     static native int native_setSpeed(int speed);  
  48.     static native int native_setText(byte str[],long length,int islast);  
  49.     static native int native_clearText(int appID);  
  50.     static native int native_start();  
  51.     static native int native_next();  
  52.     static native int native_flush();  
  53.     static native int native_pause();  
  54.     static native int native_resume();  
  55.     static native int native_stop();   
  56.     static native int native_finalize();  
  57.     static native int native_delete();  
  58.     static native int native_getSynSpeed();  
  59.     static native boolean native_isPlaying();  
  60.     static native long native_get_current_position();  



JNI组件的入口函数——JNI_OnLoad()、JNI_OnUnload()

JNI组件被成功加载和卸载时,会进行函数回调,当VM执行到System.loadLibrary(xxx)函数时,首先会去执行JNI组件中的JNI_OnLoad()函数,而当VM释放该组件时会呼叫JNI_OnUnload()函数。先看示例代码:


[java] view
plain
copy

  1. typedef union {  
  2.     JNIEnv* env;  
  3.     void* venv;  
  4. } UnionJNIEnvToVoid;  
  5.   
  6. /* This function will be call when the library first be loaded */  
  7. jint JNI_OnLoad(JavaVM* vm, void* reserved)  
  8. {  
  9.     UnionJNIEnvToVoid uenv;  
  10.     JNIEnv* env = NULL;  
  11.     LOGI("JNI_OnLoad!");  
  12.   
  13.     if (vm->GetEnv((void**)&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {  
  14.         LOGE("ERROR: GetEnv failed");  
  15.         return -1;  
  16.     }  
  17.   
  18.     env = uenv.env;;  
  19.   

抱歉!评论已关闭.