Jni Dynamic Registration, jni dynamic

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.