Java nativie interface (JNI, Chinese name: Java Local interface) is part of the Java platform standard, which allows Java code to interact with code written in other languages. JNI is a local programming interface that enables Java code running inside the Java Virtual Machine (VM) to use other programming languages (such as C, C ++, and assembly languages) write Applications and libraries for interaction. JNI is mainly used to access hardware and pursue high efficiency or reusable C/C ++ libraries.
Android uses JNI to call the C/C ++ method. However, Java JNI is further enhanced in Android to make JNI calls more efficient. Therefore, in general, JNI can be used in the Android system in two ways. First: Java Native JNI, using DLL and other dynamic link libraries; second, Android enhanced version JNI, by dynamically loading *. So Link Library for JNI calls. Today, we have analyzed the first JNI usage method, which is also called the JNI entry.
Because Java uses different syntaxes from other programming languages, to enable Java to communicate with C/C ++ library functions, the following parameter type ing is agreed:
Java C/C ++
Void void
Jboolean Boolean
Jint int
Jlong long
Jdouble double
Jfloat float
Jbyte
Jchar char
Jshort shor
The above is only a ing of simple types, and we will improve the ing of other parameter types later.
Development Environment Introduction (in Windows ):
Eclipse: Mainly used to create Java Projects
Microsoftvc ++ 6.0: generate a dynamic link library for loading corresponding java files
1. Use eclipse to create a Java Project
In this example, we create a Java project hellobabyjni. The absolute path of the project is located inE: \ mycode \ androidcode \ hellobabyjniPath, the main file path is located in\ SRC \ lover \ hellojniPATH (the path is very important for javah compilation)
The hellobabyjni. Java file is as follows:
Package COM. lover. hellojni;/*** a simple Java JNI instance **/public class hellobabyjni {/** static constructor that dynamically loads the hellobabyjni dynamic library. The DLL name is hellobabyjni. DLL ---> created by msvc6.0 */static {system. load ("E:/hellobabyjni. DLL "); // The absolute storage path of the DLL link library may be required}/** register the local C/C ++ method to be called in Java (native method ), that is, the method implemented by C/C ++ */Public native int add (int A, int B); // main method, load the dynamic library to call the C/C ++ local method public static void main (string [] ARGs) {hellobabyjni = new hellobabyjni (); // call the registered add method to obtain the returned value int result = hellobabyjni. add (2, 3); // output system. out. println ("after invoke the native method, the result is" + result );}}
2. Compile the hellobabyjni. Java file to generate the hellobabyjni. Class file in the path \ SRC \ lover \ hellojni \ hellobabyjni. Class
3. Use the javah command to compile hellobabyjni. class file to generate the agreed interface for communication between Java and C/C ++. It specifies the specific interface of nativemethod in Java in C/C ++. After CMD is run, go to the path E: \ mycode \ androidcode \ hellobabyjni \ SRC and use the javah command. The instruction set format is as follows:
Javah-classpath E: \ mycode \ androidcode \ hellobabyjni \ Src-JNI com. Lover. hellojni. hellobabyjni. For instructions, refer to javah-help. This document only applies the code again.
:
After the program runs successfully, we can find the com_lover_hellojni_hellobabyjni.h file in the SRC folder. This file is the agreed interface we need to find. Its naming rule is package name_java file name. To be understandable, we rename it as the hellobabyjni. h file. The content is as follows:
/* Do not edit this file-it is machine generated */# include <JNI. h>/* Header for class com_lover_hellojni_hellobabyjni */# ifndef _ classes # DEFINE _ classes # ifdef _ cplusplusextern "C" {# endif/* Class: required * method: add * Signature: (ii) I * // ** agreed interface generated by Java Native method using the javah command **/jniexport jint jnicall extends (jnienv *, jobject, jint, jint); # ifdef _ cplusplus} # endif
PS: 1. Since the-JNI command is the default option in javah, we can ignore it.
2. in DOS,. indicates the current path, that is, E: \ mycode \ androidcode \ hellobabyjni \ SRC. We can simply use it to specify the current path.
Therefore, a simple javah command is as follows:
In addition, we can execute the javah command in the bin folder of the Project menu. You only need to replace the bin with SRC. Of course, the generated. h file is in the bin folder.
Ii. Use ms vc to generate DLL Dynamic Link Library:
1. Create a DLL project, as shown in:
2. Next, select a simple DLL Project
3. The hellojni project is generated and switched to the file view.
First, import the previously generated hellobabyjni. h header file (My VC environment may be faulty. I created a new hellobabyjni here. h file, copy the file we previously generated through javah. h file content)
Second, add the following header file to be referenced in the stdax. h file:
Third, add the native method implementation in hellobabyjni. cpp and add the corresponding parameters.
// Hellobabyjni. CPP: defines the entry point for the DLL application. // # include "stdafx. H "bool apientry dllmain (handle hmodule, DWORD ul_reason_for_call, lpvoid lpreserved) {return true ;} // The above is automatically generated by VC./* this is the native method of the header file generated by Java, the naming conventions are as follows: Java _ package name_method name * jniexport jnicall jnienv and jobject are all JNI keywords * specific implementations are fully operated according to C ++ syntax */jniexport jint jnicall callback (jnienv * ENV, jobject, jint A, jint B) {int result = a + B; return result ;}Then, this result is returned to the Java caller by our local method.
In the end, do not be proud of your success. We need to add the header file reference of Java JNI for our project. The specific file to be added is under the JDK directory we have installed.
The specific operation is: Select Tools-OPTIONS… on the VC menu ..., Open the option dialog box. In the directories folder, add the include and include/Win32 folders under the JDK folder.
Generate DLL dynamic link library ---> build on libraries ---> build hellobabyjni. dll.
In the debug file, find the hellobabyjni. dll dynamic link library we created.
3. Run the Java ProjectThrough the first two steps, we have successfully created a Java project and the DLL dynamic link library it calls. In order to allow the system to access hellobabyjni during the call. DLL, I just put it in the C: \ window \ system32 folder (in window7 ). Successfully runs the Java project created earlier. Output:
Summary:When calling Java Native JNI, focus on the following aspects:
1. Master the javah command;
2. Parameter type ing;
3. understand and implement the native method in the generated header file (. h.
In this way, we can have a good grasp of the use of Java Native JNI.