1. Introduction of NDK
The NDK (Native development Kit) NDK provides a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK. The NDK integrates a cross compiler (a cross compiler requires a UNIX or Linux system environment), and provides the corresponding MK file isolation CPU, platform, ABI, and other differences, developers simply need to modify the Mk file ("which files need to compile", "compile feature requirements", etc.), you can create a so.
2. Why use the NDK
1.) Protection of the Code. Because the Java layer Code of the APK is very easy to decompile, the C + + library is more difficult to reverse.
2.) Easy access to existing open source libraries. Most of the existing open source libraries are written in C + + code.
3.) Improve the efficiency of the execution of procedures. Use c development for application logic that requires high performance to improve application execution efficiency.
4.) Easy to transplant. The library can be used in other embedded platforms easily.
3. What is JNI?
JNI is all called: Java Native Interface. JNI is a local programming interface that enables Java code that runs inside a Java virtual machine to interact with code written in other languages, such as C, C + +
4. Why use JNI?
The purpose of JNI is to enable Java methods to invoke some of the functions implemented by C.
5. What is the so file in Android?
The so file used in Android is a C + + library of functions. In the Jni of Android, the corresponding C language must first be packaged into so library and then imported into the Lib folder for Java to call.
Small notes:
* Javac is to compile the Java file into a. class file
*javah is the *javac of the generated. h file within the JNI to compile the Java source file into a class bytecode file
such as Javac Helloworld.java
After running the Javac command, a helloworld.class file appears if there is no error in compiling successfully.
Java can run a class byte-code file
such as Java HelloWorld
Note that the Java command does not add a. class after
*************************************************************************************************************** ************
Developing JNI in Android Studio:
Preparation: JDK version must be above 1.7, and configure the environment variables (many online do not say), download the NDK and configure environment variables to see if you install the NDK, in the Android Studio Command Window Ndk-build, the following conclusions appear, said that they installed the NDK
(1), use Java code, write local method
public class Testjni { static "Testjni"); // load. So library } public native boolean Init (); public native int Add (int x, int y); public native void Destory ();}
(2),
Compiling a Java file into a. classes file
(3), in the Termanal window of Android Studio enter main workspace, enter the following command javah-d Jni-classpath SDK path \platforms\android-19\android.jar; \.. \build\intermediates\classes\debug Com.example.administrator.rxjavatest.JNI.TestJNI
Run Result: The CPP directory is automatically generated under the Android results directory, and the package name-file name is generated. h header file (middle of package name is-connection)
(4), create a C file under the CPP package (load the newly generated header file, and implement the method), here simple implementation
#include <com_example_administrator_rxjavatest_JNI_TestJNI.h>jniexport Jboolean jnicall java_com_example_administrator_rxjavatest_jni_testjni_init (JNIEnv*, Jobject) { return true; }/** Class:com_example_administrator_rxjavatest_jni_testjni * method:add * Signature: (II) I*/jniexport jint jnicall java_com_example_administrator_rxjavatest_jni_testjni_add (jnienv*, Jobject, Jint, Jint) { return 0; }/** Class:com_example_administrator_rxjavatest_jni_testjni * method:destory * Signature: () V*/Jniexportvoidjnicall java_com_example_administrator_rxjavatest_jni_testjni_destory (jnienv*, Jobject) { return ; }
(5): in the file
NDK {
ModuleName name of "Testjni"//generated library (. So), load library Yes name
Ldlibs "Log", "Z", "M"
Abifilters "Armeabi", "armeabi-v7a", "x86"//
Outputs specify so libraries for three ABI architectures
}
(6) When the project is rebuilt, (complete) The local method can be called through the object of the class.
Android JNI Development