In-depth understanding of Android (i) ——— first-known JNI

Source: Internet
Author: User

1. JNI, Java Native Interface, or Java local call.

Role: The JNI layer serves as a bridge between the Java world and the Native world, allowing functions in the Java program to call Native (C + +), functions that can be called Java in Native (c + +) programs.

2. Java to invoke the native function, you need to complete the following two tasks:

(1) load the corresponding JNI dynamic library. The System.loadlibrary method is typically called in the static statement of a Java class to dynamically load the JNI library.

(2) Declare a function modified by the keyword native. Native is a Java keyword that indicates that it is done by the JNI layer.

3. Register the JNI function and associate the native function of the Java layer with the implementation function corresponding to the JNI layer. There are two ways of registering:

(1) Static registration: The relationship between the Java function and the JNI function is established according to the function name.

Step: i) write the Java code First, and then generate a compilation to generate the. class file.

II) Use Java utility Javah, such as javah-o output Packagename.classname, generates a JNI layer header called Output.h, where Packagename.classname is the Java code-compiled class file. The corresponding JNI layer function is declared in Output.h, as long as the function inside can be implemented.

(2) Dynamic registration: Call JNIEnv's registernatives function in the JNI layer code to implement the Registration association relationship.

Time of dynamic registration: After loading the JNI dynamic library through System.loadlibrary (), the Java layer will look for the Jni_onload function in the library, and if it is found, it is called, so the work of dynamic registration can be performed in this function.

4. jnienv

(1) jnienv is a thread-related struct representing the JNI environment and provides some JNI system functions that allow you to:

    • Call the Java function.
    • Manipulate Jobject objects and other things.

(2) The relationship between JAVAVM and jnienv

The entire process has only one JAVAVM, and through JAVAVM's Attachcurrentthread () function in the thread, you can get the JNIENV structure of the threads so that the Java functions can be recalled in the background thread. Before the background thread exits, call JAVAVM's Detachcurrentthread () function to release the corresponding resource.

5. Garbage collection

Three types of reference for JNI:

(1) Local Reference: Native reference. The non-global reference objects used in the JNI layer functions are local Reference, including the jobject that were passed in when the function was called and the Jobject created in the JNI layer function. Once the JNI layer function returns, Jobject may be garbage collected.

(2) Global Reference: globally referenced. If this object is not released voluntarily, it will never be garbage collected.

(3) Weak global Reference: weak globally referenced. It is possible to be garbage collected during the run, and before using it, call JNIEnv's issameobject to determine if it is being recycled.

Because Java layer objects may be garbage collected, it is possible to have an exception if you save the Java layer's objects as local Reference in the JNI layer. Therefore, when you want to save the Java Layer object in the JNI layer, use global Reference and release it when you are done.

In-depth understanding of Android (i) ——— first-known JNI

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.