Deep understanding of Android (2) __ in-depth understanding of JNI

Source: Internet
Author: User

Preface: The previous post, we probably understand the Android system architecture, for each level of East also has a basic understanding, then we can formally work, start a breakthrough, to conquer every difficulty, you are not very excited? OK, let's take jni to practice practiced hand.

  Jni:java nativeinterface (Java local call), say the simple point, JNI is a technology, what is it? There are two main points:

    (1) Functions in Java programs can call functions written in native language, native generally refers to the functions written in C/s + +;

(2) functions in the native program can call functions of the Java layer.

See this, what do you think? In fact, we can think of JNI as a bridge connecting the Java layer to the native layer.

Although Jni was written in native, we still classify it in a separate layer: the JNI layer.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --

  1, jni Example: Mediascanner (combined with the analysis of the hierarchy Chart)---an important part of the multimedia system, the function is to scan media files, such as the length of the song, song author and other media information, and put them into the media database.

(1) The Java World corresponds to the Mediascanner,mediascanner this class has some functions that need to be native layer to achieve;

(2) JNI corresponds to the Libmedia_jni.so,media_jni is the name of the JNI library. Where _ line media is the name of the native layer library, here is the Libmedia library. The JNI behind the line means that it is a JNI library. The name of the JNI library can be arbitrarily taken, but the following naming rules are followed: "Lib module name _jni.so";

(3) native corresponds to the libmedia.so, the library completed the actual function;

(4) The Java layer interacts with the libmedia.so of the native library through the libmedia_jni.so of the JNI library.

Let's take a look at the code related to JNI:

1  Public classMediascanner2 {3        Static{4System.loadlibrary ("Media_jni");//loading the corresponding JNI library5Native_init ();//call the native function6     } 7     ......8     //non-native function9      Public voidscandirectories (string[] directories, String volumename)Ten     { One     ...... A     } -     Private StaticNative finalvoidNative_init (); //Declare a native function, native as the Java keyword, indicating that it will be done by the JNI layer.  -     ......  the     PrivateNativevoidprocessfile (string path, string mimeType, Mediascannerclient client); -     ......       -}

The above code lists two more important points:

(1) Loading the JNI library: to invoke native, it must be implemented through a dynamic library located in the JNI layer, typically loaded in the static statement of the class, by:system.loadlibrary, after loading the name of the dynamic library, The system will automatically expand into real dynamic library filenames based on different platforms, for example: it expands into libmedia_jni.soin Linux, and expands into media_jni.dllon the Windows platform.

(2) Java Native function: We find that there are native keywords before the native_init and processfile functions, which means that the two functions will be implemented by the JNI layer.

So, if you want to use JNI, just do the above two tasks. But JNI to complete the task, but more complex, we will analyze together below.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --

  2. Mediascanner Analysis of JNI layer

Due to the time problem, the partial JNI layer code is represented in the form of a picture.

At this point, I believe you may wonder: How do you know that the Java layer's Native_init function corresponds to the Android_media_mediascanner_native_init function of the JNI layer?

  (1) Registering the JNI function

As you know, the Native_init function is in the Android.media package, its full path name is: Android.media.MediaScanner.native_init, and the JNI layer function name is Android_media_ Mediascanner_native_init.

Have you found any problems? Well, I'll tell you directly: in the native language, the symbol "." is a special meaning, so the JNI layer needs to replace "." In the Java function name (including the package name) with "_", that is, in this way, native_ Init found his own brother Android_media_mediascanner_native_init of the JNI layer.

In fact, we are talking about a "registration" problem. "Registering" is: associating the Java layer's native function with the implementation function corresponding to the JNI layer. with this connection, the native function of the Java layer is invoked, and the corresponding function of the JNI layer can be transferred smoothly.

  "Registration" there are two ways:(for the relevance of the specific code is not analyzed, we probably understand the principle of registration, if interested students, can give me a reply, if necessary, I will write a post to explain it)

· Static registration: It is based on the function name to establish the relationship between the Java function and the JNI function (in fact, the function pointer that holds the JNI layer function ), and it requires that the name of the JNI layer function must follow a specific format.

· Dynamic registration: If we specifically use a structure (Jninativemethod) to preserve this association, then we can directly let the native function know the function pointers of the corresponding function of the JNI layer, so is the efficiency improved? The answer is YES!

  We give a direct answer: When the Java layer finishes loading the JNI dynamic library through System.loadlibrary, it will then look for a function called Jni_onload in the library (this function is in android_media_mediaplayer.cpp). If so, call it. The job of dynamic registration is done here.

  (2) data type conversion issues

  Call the native function in Java and pass the arguments to the Java data type, then what will these parameters be passed to the JNI layer?

We know that there are two types of data in Java: The basic data type and the reference data type.

For these two types, JNI is also differentiated, let's take a look at the following:

    · Conversion of basic data types

        

      Note the change in Word length, for example, the word length of Jchar is 16 bits, which accounts for two bytes, which is not the same as the normal char account of one byte .

    · Conversions of reference data types

        

  (3) JNIEnv Introduction

        

Let us analyze the 5 parameters of the following JNI layer functions: The next three parameters, you should be clear, they correspond to the Java native Function 3 parameters, then what are the other two parameters?

    Let's take a look at Jobject: The Mediascanner object representing the Java layer, which represents the processfile that is called on which Mediascanner object. If the Java layer is a static function, then this parameter will be Jclass, which indicates which Java class is being called.

Below, we formally lead the protagonist:jnienv.

JNIEnv: is a thread-related struct representing the JNI environment.

Internal structure:

              

JNIEnv actually provides a number of JNI system functions that can be used to:

1, call the Java function, 2, Operation Jobject object and so on many things

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --

In the next post, we'll take a look at how to use the JNIEnv function, and we'll introduce you to an important point of knowledge about jnienv.

  

Deep understanding of Android (2) __ in-depth understanding of 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.