Android version: 2.3.7 _ r1
Linux Kernel version: android-goldfish-2.6.29
Before analyzing the HAL of the Android hardware abstraction layer, let's take a simple example to see how JAVA uses Java Native Interface (JNI) it interacts with C/C ++ code.
First, compile NativeTest. java. The Code is as follows:
[Cpp] 1 public class NativeTest {
2 public native static double max (double a, double B );
3
4 static {
5 System. loadLibrary ("max ");
6}
7
8 public static void main (String [] args ){
9 System. out. println (max (1, 2 ));
10}
11}
1 public class NativeTest {
2 public native static double max (double a, double B );
3
4 static {
5 System. loadLibrary ("max ");
6}
7
8 public static void main (String [] args ){
9 System. out. println (max (1, 2 ));
10}
Line 11} 2nd declares that the max function is an native function, which is implemented in C language later.
Line 3: import the system library max. in Linux, the name is libmax. so.
Line 3 calls the max function. The function is implemented in the max library.
Next, we need to compile the JAVA source code and use the javah tool to generate a C header file.
# Javac NativeTest. java
# Javah-jni NativeTest
After the preceding two commands are executed, the NativeTest. class and NativeTest. h files are generated in the current directory.
Open NativeTest. h with the following content:
[Cpp] 1/* do not edit this file-it is machine generated */
2 # include <jni. h>
3/* Header for class NativeTest */
4
5 # ifndef _ Included_NativeTest
6 # define _ Included_NativeTest
7 # ifdef _ cplusplus
8 extern "C "{
9 # endif
10 /*
11 * Class: NativeTest
12 * Method: max
13 * Signature: (DD) D
14 */
15 JNIEXPORT jdouble JNICALL Java_NativeTest_max
16 (JNIEnv *, jclass, jdouble, jdouble );
17
18 # ifdef _ cplusplus
19}
20 # endif
21 # endif
1/* do not edit this file-it is machine generated */
2 # include <jni. h>
3/* Header for class NativeTest */
4
5 # ifndef _ Included_NativeTest
6 # define _ Included_NativeTest
7 # ifdef _ cplusplus
8 extern "C "{
9 # endif
10 /*
11 * Class: NativeTest
12 * Method: max
13 * Signature: (DD) D
14 */
15 JNIEXPORT jdouble JNICALL Java_NativeTest_max
16 (JNIEnv *, jclass, jdouble, jdouble );
17
18 # ifdef _ cplusplus
19}
20 # endif
21 # Line 15-16 of endif. As you can see, this header file declares a function Java_NativeTest_max, which is the function we need to implement in the C program. It corresponds to the max function called in JAVA code.
Write the C program max. c to implement the Java_NativeTest_max function. The Code is as follows:
[Cpp] 1 # include "NativeTest. h"
2 JNIEXPORT jdouble JNICALL Java_NativeTest_max
3 (JNIEnv * jni_env, jclass class, jdouble a, jdouble B)
4 {
5 return a> B? A: B;
6}
1 # include "NativeTest. h"
2 JNIEXPORT jdouble JNICALL Java_NativeTest_max
3 (JNIEnv * jni_env, jclass class, jdouble a, jdouble B)
4 {
5 return a> B? A: B;
6} The Role Of The Java_NativeTest_max function is to return the two parameters that are larger.
Next we need to compile and generate the dynamic link library libmax. so. Before compilation, configure the environment variable:
# Export C_INCLUDE_PATH = $ C_INCLUDE_PATH: $ JAVA_HOME/include
# Export C_INCLUDE_PATH = $ C_INCLUDE_PATH: $ JAVA_HOME/include/linux
# Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH :.
Compile the Dynamic Link Library:
# Gcc max. c-fPIC-shared-o libmax. so
The dynamic link library file libmax. so is generated in the current directory and the JAVA program is executed:
# Java NativeTest
2.0
The above process is the process of connecting JAVA code with C code using JNI.