当Java代码中执行Native的代码的时候,首先是通过一定的方法来找到这些native方法。JNI有如下两种注册native方法:
下面主要讲一下动态注册,动态注册比较方便jni的移植,而静态注册如果移植到其他的packet,需要修改所有的jni函数名.加载的效率上,动态注册也优于静态注册
static {
System.loadLibrary("nativeffmpeg");
native_init();
}
执行System.loadLibrary()函数时,第一件事是调用JNI_OnLoad()函数,如果JNI Lib没有定义JNI_OnLoad,则dvm调用dvmResolveNativeMethod进行动态解析.
jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{
JNIEnv* env = NULL;
jint result = -1;
javaVM = vm;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
ALOGE("ERROR: GetEnv failed\n");
goto bail;
}
assert(env != NULL);
if (register_native_MediaPlayer(env) < 0) {
ALOGE("ERROR: MediaPlayer native registration failed");
goto bail;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
bail:
return result;
JNI_OnLoad函数主要分成两部分:
vm->GetEnv((void**) &env, JNI_VERSION_1_4)
这里调用了GetEnv函数时为了获取JNIEnv结构体指针, 后面通过JNIEnv提供的函数注册native 方法
static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int methodsNum) {
jclass clazz;
//找到声明native方法的类
clazz = env->FindClass(className);
if(clazz == NULL){
return JNI_FALSE;
}
//注册函数 参数:java类 所要注册的函数数组 注册函数的个数
if(env->RegisterNatives(clazz, gMethods, methodsNum) < 0){
return JNI_FALSE;
}
return JNI_TRUE;
}
static int register_native_MediaPlayer(JNIEnv* env){
//指定类的路径,通过FindClass 方法来找到对应的类
const char* className = "pri/tool/ffmediaplayer/MediaPlayer";
return registerNativeMethods(env, className, gMethods, sizeof(gMethods)/ sizeof(gMethods[0]));
}
static JNINativeMethod gMethods[] = {
{"native_init", "()V", (void *)pri_tool_MediaPlayer_native_init},
{"native_setup", "(Ljava/lang/Object;)V", (void *)pri_tool_MediaPlayer_native_setup},
{"native_setDataSource", "(Ljava/lang/String;)V", (void *)pri_tool_MediaPlayer_native_setDataSource},
{"native_prepareAsync", "()V", (void *)pri_tool_MediaPlayer_native_prepareAsync},
{"native_testCallback", "(Z)V", (void *)pri_tool_MediaPlayer_native_testCallback},
{"native_setSurface", "(Ljava/lang/Object;)V", (void *)pri_tool_MediaPlayer_native_setSurface},
{"native_start", "()V", (void *)pri_tool_MediaPlayer_native_start},
{"native_stop", "()V", (void *)pri_tool_MediaPlayer_native_stop},
{"native_reset", "()V", (void *)pri_tool_MediaPlayer_native_reset},
{"native_release", "()V", (void *)pri_tool_MediaPlayer_native_release},
{"native_pause", "()V", (void *)pri_tool_MediaPlayer_native_pause},
};
private static native final void native_init();
private native final void native_setup(Object mediaplayer_this);
private native final void native_setDataSource(String filePath);
private native final void native_testCallback(boolean bNewThread);
private native final void native_prepareAsync();
private native final void native_setSurface(Object surface);
private native final void native_start() throws IllegalStateException;
private native final void native_stop() throws IllegalStateException;
private native final void native_reset() throws IllegalStateException;
private native final void native_release() throws IllegalStateException;
private native final void native_pause() throws IllegalStateException;
在注册native函数时有这样的定义
{"native_setSurface", "(Ljava/lang/Object;)V", (void *)pri_tool_MediaPlayer_native_setSurface},
其中(Ljava/lang/Object;)V为对应的函数的签名. Java支持函数重载,可以定义相同方法名,但是不同参数的方法,然后Java根据其不同的参数,找到其对应的实现的方法.
函数签名具体格式如下: (参数1类型标示;参数2类型标示;参数3类型标示...)返回值类型标示
类型标示 | Java类型 |
---|---|
Z | boolean |
B | byte |
C | char |
S | short |
I | int |
J | long |
F | float |
D | double |
V | void |
[数组 | 数组 |
[i | int[] |
[Ljava/lang/Object | String[] |