The process of calling NDK layer configuration is illustrated by jniaes case.
And we judge whether the signature is correct through the bottom layer. If it is correct, we will continue to execute the core code, otherwise we will quit the program to prevent others from maliciously decompiling and repackaging.
First of all, go to official website to download the latest NDK and put it anywhere, just like I put it on D: \ dev \ Android \ Android-NDK-R10d.
(1) Create a new project: JniAes.
First, add a native interface to the java class, and pay attention to writing the native interface and System.loadLibrary (). The code is as follows:
1 public synchronous static native string getFromnativeiv (); 2 public synchronized static native String from native(); 3 public synchronized static native int jnicheckapp (context context); four
Then build the project and get the intermediate file. What we care about is. Class file. The class file generated after compiling OK is in the following directory of AS project:
AES \ app \ build \ intermediates \ classes \ debug \ Android \
② Next, generate the corresponding. H header files and class files, and execute the following command.
Click View->; Tool window-> Terminal refers to the terminal command line tool in Studio. Execute the following commands to generate the C language header file: \ users \ sodinochen \ appdata \ local \ Android \ SDK \ platforms \ Android-16 \ android.jar; ..\..\ build \ intermediates \ classes \ debug com . AES . jniaes . main activity
(3) then cutting. H file. Create a new C file in jni directory and name it at will. My name is strk.c
Strk.c file, which is used to realize the core code logic and judge whether the package name and hash value are legal. If it returns 1, it is legal. On the contrary, it is illegal. The entry method is: jintjava _ com _ AES _ jnieees _ mainactivity _ jnicheckapp (jnienv * env, jobjectcontext,? jobject thiz)
Next, add the NDK path (the NDK downloaded above) to the local.properties file of the project, which is similar to the SDK path. My supplement is as follows:
SDK . dir = D \:\ \ Dev \ \ Android \ \ Android-SDK-windows ndk . dir = D \:\ \ Dev \ \ Android \ \ Android-ndk-r 10d
(4) Next, set the library file name (generated so file name) in build.gradle under the app module directory. Locate the defaultConfig entry of the gradle file and add the following:
Defaultconfig {application id "com.aes.jniaes" minsdkversion 1 5targetsdkversion22 version code 1 version name "1. 0" ndk { moduleName "checkapp-jni "? //The generated so names ABI filters "Armeabi", "Armeabi-V7A" and "x86"? //Output the so libraries under the specified three abi architectures. At present, it is dispensable. } }
(5) Finally, add the static initialization loadLibrary code, as shown below:
static { system . loadlibrary(" check app-JNI "); //The name of the //so file? }
The compiled so file is located in the AES \ app \ build \ intermediaries \ ndk \ debug \ lib directory.
Then how to use the compiled so file can be done in the following ways:
Two. ? Reference so file
(1). Create a new directory named "jniLibs" under the "src/main" directory;
(2) Copy and paste the so file into the "jniLibs" directory.