Java的Native方法的名称和底层的c/c++方法名称是通过java_<包名>_<类名>_<方法名>这种方式对应的,即它是静态注册的,例如在java层的Native 方法名为stringFromJNI,它的包名为hello,类名为 hellojni则在底层的 c函数名称为java_hello_hellojni_stringFromJNI,这样一来名称就变得很复杂,不便于阅读和维护,在Android NDK下面提供了动态注册这种方法,它让底层的函数命名没有这种局限性,在编写底层c 函数代码时更加灵活 下面是具体做法:
#include <jni.h> #include <android/log.h> #include <stdio.h> #include <stdlib.h> #include <config.h> static JNIEnv *env=NULL; static int nMethods=2; const char* className="Example/PINVOKEMethod"; const char* dataClassName="Data/MyData"; extern "C" { //接收上层传来的字节数组 void jniReceiveData(JNIEnv *e, jclass c,jbyteArray bytes,jint length); //供上层获取解析后的数据 void jniGetGPSData(JNIEnv *e, jclass c,jobject o); }; //动态注册的方法 static JNINativeMethod gMethods[] ={ { "ReceiveData","([BI)V",(void *)jniReceiveData }, { "getData","(LData/MyData;)V",(void *)jniGetData }, }; void jniReceiveData(JNIEnv *e, jclass c,jbyteArray bytes,jint length) { } void jniGetData(JNIEnv *e, jclass c,jobject o) { } jint JNI_OnLoad(JavaVM* vm, void* reserved) { jint result=-1; jclass clz=NULL; if(vm->GetEnv((void**)&env,JNI_VERSION_1_4)!=JNI_OK) return -1; clz=env->FindClass(className); if(env->RegisterNatives(clz,gMethods,nMethods)<0) return -1; return JNI_VERSION_1_4; }
typedef struct {
const char* name; //Java中 native函数的名字
const char* signature; //用字符串描述的java native 函数的参数和返回值
void* fnPtr; //指向底层 C函数的函数指针
} JNINativeMethod;