Based on Android2.3.5 system: Analysis of JNI and HAL instances [i]

Source: Internet
Author: User

*************************************************************************************************************** ************
Easywave Time: 2015.01.25
Category: Android system-based on Android2.3.5 system: JNI and HAL Instance resolution [a] Disclaimer: reproduced, please keep the link

Note: If there is an error, please correct me. These are the journal articles I studied ...

*************************************************************************************************************** ************

One: Introduction to the JNI under the Android system

The full name of JNI under the Android system is Java Native Interface (JNI), which is part of the Java platform that allows Java code to interact with code written in other languages. JNI is a local programming interface that enables Java code running inside a Java Virtual machine (VM) to interoperate with applications and libraries written in other programming languages such as C, C + +, and assembly language.

Second: Why does Java want to introduce JNI

JNI has a wide range of applications in Android systems. The underlying Android system is implemented in C + +, and the API provided by the upper layer is Java, and Java invokes the underlying implementation via JNI.

    • Java World virtual machines are written in the native language, virtual machines run on a specific platform, the virtual machine itself cannot be platform-independent, JNI can block the Java layer different platform operation differences, so that the Java itself can realize platform-independent features.
    • Adapt to technologies already implemented in native languages.
    • Some of the efficiency issues require native language implementations.

The use of JNI in Java is mainly implemented for the above points, in the Anroid API Multimedia Interface MediaPlayer class, in fact, the underlying through the JNI call Libmedia Library. Since the existence of JNI allows us to reuse many libraries that already exist in C + +, eliminating the hassle of repetitive development, and using many open source libraries (there are many open source libraries in the Android library, such as libjpeg,libpng, etc.), and let us develop the program more efficient Code to perform the best hardware performance).

Third: The JNI invocation procedure in Java

In the Java world, JNI can be seen as a proxy mode for the application itself, and for the beginning it is necessary to implement an agent to actually manipulate the target native function using C/s + +, and in the Java program the JVM invokes the target native function indirectly by loading and invoking the JNI program. Its invocation is as follows:


Figure one: Android under the JNI invocation process (Load dll,dll Depending on the environment )

The general procedure for developing a JNI program in the Android system Java is as follows:

    • Write the calling class in Java;
    • Using Javah to generate the header file of C + + native function;
    • Other function functions are called in C + + to implement the native function;
    • Add all the native libraries and resources that the project relies on to the path of the Java project;
    • Generate Java programs;
    • Publish Java applications and so libraries;

Four: The JNI syntax under the Android system

The JNI of the Android system Java can be regarded as a proxy mode for the application itself, and for the beginning, C + + is needed to implement an agent to actually manipulate the target native function, the basic data type conversion, as follows:


Figure two: Basic Data type conversion table

Note: The symbol attribute is defined in the JNI environment

The Reference data type conversion table is as follows:


Figure III: Reference data type conversion table

Five: Java under the JNI registration function

JNI naming rules under Java: The JNI layer requires the "." In the name of the Java function (including the package name). Instead of "_", you can find the path and file of your own JNI layer in this way. There are two ways to register the native function: static registration and dynamic registration. Static registration static is based on the function name to establish the association between Java and JNI functions, and requires that the name of the JNI layer function must follow a specific format, the disadvantage is that: need to compile all the Java declared class; Javah generated JNI layer function is very long The first call to the native function is to search for the corresponding JNI layer function according to the name, which affects the efficiency. So here is only a detailed explanation of the dynamic registration, Java native and jni through the structure of Jninativemethod to establish a link, the structure of the following:

typedef struce {       const char* name;        The name of the native function in Java, without carrying the path of the package      const char*signature;    The signature information of a Java function, represented by a string, is a combination of the parameter type and the return value type     void *fnptr;             
Using an example of a anroid system to understand and explain the perceptual (Please go online to download Anroid2.3.4 's OK6410 anroid code, consistent with the path of this article, because I own this Android2.3.4 OK6410 ported to Android2.3.5 platform) path to ANDROID2.3.5/ Frameworks\base\services\forlinx_pwm_jni\pwmjniservice.cpp, which is a PWM-controlled JNI, is specific as follows:

Figure IV: Jninativemethod method Android offers a registernatives (jnienv *env, const char* className, const jninativemethod* Gmethods , int nummethods) to implement this function, in fact there is also a registernatives function implemented in AndroidRuntime.cpp, the function of both is the same, as follows:

Let's take a look at the JNI dynamic registration in the PwmJniService.cpp function, as shown below:


In the Jni_onload function, the function is called Register_forlinx_server_pwmservice (), and the function is defined as follows:


Let's take a look at this line of code: static const char* Const KCLASSNAME = "Forlinx_pwm_server/server/pwmservice", that is, by Kclassname find the corresponding library, And this pwmservice is in the Android2.3.5/frameworks\base\services\forlinx_pwm_server\server\pwmservice.java file, as shown below:


That is to say, Java layer through this means to implement the Java layer to call the library of the C + + layer, so that the anroid to encapsulate the bottom layer, and the level of the Android JNI will call the Android HAL layer, and the HAL layer is called the Linux driver!!

Six: Jni garbage collection
The object created in Java is finally reclaimed and freed by the garbage collector, and for JNI, referencing the object directly does not increase the reference count value, and the object referenced in JNI after it returns from JNI may be reclaimed by the garbage collector, while the pointer recorded under JNI may be a wild pointer. To solve such problems, the JNI specification provides the following three types of references:

    • Local Refrence: Native reference, the non-global variables used in the JNI layer functions are taken with the local Refrence, which contains the jobject passed in the function call and the jobject created in the JNI layer function, the most characteristic of which is that once the JNI layer function returns, These jobject may be garbage collected.
    • Global Refrence: Globally referenced, this type of reference will always reside in memory if the program does not actively invoke the destroy interface.
    • Weak Global Refrence: Weak globals, which may be released during use, call the Issameobject function in jnienv to determine if it still exists.
Seven: Signature information for JNI functions under JavaThe following are the writing rules for the JNI signature signature of Jave under the Android system: the character in () represents the parameter, followed by the return value, such as "() V" means void func (), "(II) v" for void func (int, int), and so on. The specific correspondence for each character is as follows:
If the Java function parameter is class, start with "L" and ";" End, the middle is a "/" separated by the package and the class name, the corresponding C function name parameter is Jobject, one exception is the string class, its corresponding class is jstring. As shown below:
          ljava/lang/string;    String     jstring          ljava/net/socket;     Socket     Jobject
static const Jninativemethod in Android2.3.5/frameworks\base\services\forlinx_pwm_jni\pwmjniservice.cpp gMethods[] The array function is an example, as follows:
Select the Forlinx_set_freq function to illustrate the issue of JNI signatures, which you can see in {"_set_freq", "(I) Z", (void *) Forlinx_set_freq},This way of defining it, we are at a glance, against the signature parsing table in the table above. The definition of the Forlinx_set_freq function is as follows:
You can see that the return value type of Forlinx_set_freq is: Jboolean, while JbooleanThe JNI signature represented is: Z, and Jint freqis a value that needs to be set in the Forlinx_set_freq function, and JintThe signature of the JNI represented is: I therefore the signature information for the Forlinx_set_freq function is: (I) Z

Based on Android2.3.5 system: Analysis of JNI and HAL instances [i]

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.