JAVA JNI Function Registration process detailed Introduction _java

Source: Internet
Author: User

A detailed introduction to the registration process for JAVA JNI functions

When we call native code in Java, it's usually done through JNI, All we need to do is load the local. So library file in the Java class and declare the native method, and then call it where it needs to be called, as for the concrete implementation of the native method in Java, all to the native layer. What is the premise of correctly invoking the corresponding function in the native code in Java? The answer is to build the one by one correspondence between the native method in Java and the function in the local code by a certain mechanism, so what is the mechanism? Is the registration mechanism of the JNI function.

There are two ways to register a JNI function, one is the static registration method, the other is the dynamic registration method. Let's introduce the two implementations separately.

 A. Static registration.

1. Implementation principle: According to the function name to establish the Java method and the JNI function of the one by one correspondence between.

2. Implementation process:

① to write Java code;

② compiles Java code to generate. class files;

③ uses the javah instruction to generate a JNI. h file using the generated. class file.

The ④ generated JNI header file contains the Java function's declaration in the JNI layer;

3. Disadvantages:

① writing is inconvenient because the name of the JNI layer function must follow a particular format and have a very long name;

② can cause a lot of work on the programmer because the JNI header file must be written for all Java classes that declare the native function;

The ③ program is inefficient because the first call to the native function requires searching for the corresponding local function in the JNI layer based on the function name, and then establishing the corresponding relationship, which is time-consuming.

 Two. Dynamic registration.

 1. Realization principle: directly tells native function its corresponding function in JNI the pointer;

2. Implementation process:

① uses the structure body Jninativemethod to save the correspondence relation between the Java native function and the JNI function;

② the correspondence between all the native functions and the JNI functions in a jninativemethod array;

After loading the JNI dynamic library through System.loadlibrary in Java, ③ calls the Jni_onload function and starts to register dynamically.

In ④jni_onload, the Androidruntime::registernativemethods function is called to register the function.

⑤androidruntime::registernativemethods The final call Jniregisternativemethods complete registration.

3. Advantages: Overcome the drawbacks of static registration.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.