Jni Dynamic Registration, jni dynamic
When the java layer calls the declared native function, it will find the corresponding c or c ++ function from the corresponding so library. If it cannot be found, an error is returned, if it finds a connection between this function and the native declared by java, it is actually to save the pointer of the jni layer function. You can directly use this function pointer when calling the native function declared in java again. From this we can see that the static method is to establish the association between the java function and the jni function based on the function name, And the jni layer function name must follow a specific format. Dynamic Registration: Use the JNINativeMethod struct to save the one-to-one correspondence between java native and jni functions. JNINativeMethod defines the following typedef struct {const char * name; // name of the native function in java (without carrying the package name) const char * signature; // signature information of the java function, which represents void * fnptr using a string; // function pointer of the function corresponding to the jni layer} JNINativeMethod; how should I use this struct? Let's take a look at how the MediaScanner JNI layer performs static JNINativeMethod gMethod [] = {.... {"processFile" // The native-layer function name in java "(Ljava/lang/String; Landroid/media/MediaScannerClient;) V ", // processFile signature information (void *) android_media_MediaScanner_processFile // function pointer corresponding to the jni layer },... {"native_init", "() V"; (void *) handle},}; // register the JNINativeMethod array int register_android_media_MediaScanner (JNIEnv * env ){ // "Android/media/MediaScanner" indicates the class return AndroidRuntime in java: registerNativeMethods (env, "android/media/MediaScanner", gMethods, NELEM (gMethods ));} dynamically register the function call time when the java layer passes the System. after loadLibrary loads the jni dynamic library, it will find a function called JNI_OnLoad in the library. If yes, it will be called, and the dynamic registration work will be completed 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, an error is returned}
From an in-depth understanding of android I
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.