The purpose of JNI design is as follows:
JNI is short for Java Native Interface:
The standard Java class library may not support the platform-dependent features needed by your application.
You may already have a library or application written in another programming language and you wish to make it accessible to Java applications
You may want to implement a small portion of time-critical code in a lower-level programming language, such as assembly, and then have your Java application call these functions.
Why JNI is used in Android
The android upper layer uses the Java language to develop applications. In the architecture diagram of the Android system, we can see that the android framework layer also provides Java APIs to application developers, however, all local databases are implemented using C/C ++. Therefore, you need to use JNI to provide interfaces to Java. the Java-layer interfaces can call local implementations.
JNI Architecture
Most of the APIs in the android framework layer call the native method, which is also implemented by local code and then registered to the system. During the registration process, nativehelper, a tool library in Dalvik, is used to register the functions locally called by Java (that is, interfaces are provided up the layer through JNI ).
The source code for implementing the nativehelper library is located in the "Dalvik \ libnativehelper" directory and will eventually be compiled into the libnativehelper. So Dynamic Link Library. This library is usually used in JNI mode. The main header file is as follows:
Dalvik \ libnativehelper \ include \ nativehelper \ JNI. H (based on the JNI standard header file)
Dalvik \ libnativehelper \ include \ nativehelper \ jnihelper. H (header file for JNI Registration)
The specific implementation of JNI code is mainly located in the frameworks \ base \ core \ JNI \ directory, which implements most of the android API functions and will eventually be compiled into the libandroid_runtime.so dynamic link library, placed in the \ System \ lib directory under the out directory.
JNI implementation
In fact, the main content that JNI needs to implement is two: first, register the native Methods declared in Java and code locally. Then register it to the system.
Q:
When writing JNI, it is very likely that the startup will fail during system startup. There are two main reasons for the analysis: one is that we cannot find the class, and the other is that we cannot find the nativemethod. Both cases are caused by the mismatch of Java and CPP functions or path names when writing files. Therefore, in this case, we must carefully check the side. When something went wrong, I still thought it was Android's xuanjicang that made us unable to modify and add files and functions. Later I found it was caused by my carelessness.