When the Java layer invokes the declared native function, he will look for the corresponding C or C + + function from the corresponding so library, if the error is not found, if found on the function and the Java declaration of native establish a connection, in fact, is to save the JNI layer function pointer. You can use this function pointer directly when you call the native function declared in Java again. From here we can see that the static method is based on the function name to establish the relationship between the Java function and the JNI function, and requires that the JNI layer function name must follow a specific format. Dynamic registration: Using the JNINATIVEMETHOD structure to save the Java native function and the JNI function one by one correspondence Jninativemethod defined as follows typedef struct{const CHAR* name;// The name of the native function in Java (without carrying the package name) const char* signature; The signature information of the Java function, the function pointer of void* FNPTR;//JNI layer corresponding function is represented by a string}jninativemethod; How should we use this struct? See how the Mediascanner jni layer is doing the static Jninativemethod gmethod[]={.... {"Processfile"//java the function name of the native layer function (ljava/lang/string; ljava/lang/string; landroid/media/mediascannerclient;) V ",//processfile signature Information (void*) android_media_mediascanner_processfile// The function pointer corresponding to the JNI layer},... {"Native_init", "() V";(void*) android_media_mediascanner_native_init},};//Register jninativemethod Array int register_android _media_mediascanner (jnienv* env) {//"Android/media/mediascanner" indicates which class in Java return Androidruntime:: Registernativemethods (env, "Android/media/mediascanner", Gmethods, Nelem (gmethods) );} Dynamic registration of Function call Timing when the Java layer finishes loading the JNI dynamic library through System.loadlibrary, it will then find a function called Jni_onload in the library, and if so, the job of dynamic registration is done here. Example: Jint jni_onload (javavm* vm, void* reserved) {jnienv* Env=null;jint result=-1;if (vm->getenv (void**) &env,jni _version_1_4)! = Jni_ok) {goto bail;} if (Register_android_media_mediascanner (env) < 0) goto Bail;return jni_version_1_4; This value must be returned, otherwise it will be an error}
Excerpt from an in-depth understanding of Android I
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JNI Dynamic Registration