JNI Dynamic Registration

Source: Internet
Author: User

 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

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.