After the Android program is written in Java, it is easy to decompile after the release, so a way to protect the code or sensitive data (such as a communication key) is required, and the NDK is a good solution.
The NDK can generate a binary library (. So) file that is more difficult to decompile.
First we need to build the NDK environment, the eclipse is very simple to build, first add Android plug-ins in eclipse, it needs to download an NDK package, and then in Eclipse's settings, the NDK in the Android item to set the road strength:
The NDK development can then be started, followed by an example to develop the NDK.
First, build a new Android project, then create a new jar package, and write the interface classes that are ready for C + + development. The code for the class is as follows:
Public class Ndkhelloworld {
Public static native String stringfromndk();
}
Then find the corresponding classes directory, use the Javah command to generate the corresponding header file, the command is:
Javah-jni Com.example.ndk.ndkHelloWorld
You will then be able to see a com_example_ndk_ndkhelloworld.h in the classes directory, which is the generated header file.
Then we are going to add the so library, right-click on the project properties, select Android Tool,add native support, such as:
After clicking, you will be asked to enter the name of so, and after you enter it, you can see that our list of items has a JNI folder, and the relevant data about the NDK is in this folder, and the header file that we previously generated through Javah is placed in the folder.
Android.mk is the JNI compilation file, and the syntax is not expanded.
Implement the methods in Com_example_ndk_ndkhelloworld.h in CPP and introduce the previously generated header files:
#include "Com_example_ndk_ndkhelloworld.h"
Jstring JAVA_COM_EXAMPLE_NDK_NDKHELLOWORLD_STRINGFROMNDK
(jnienv *env, jclass obj) {
Return env->newstringutf("Hello ndk");
}
Then go back to the ndkhelloworld we had before, plus the link to so.
Static {
System. LoadLibrary("Ndkhelloworld");
}
Then we can call the Ndk method of this class in other files.
Mtextview. SetText(ndkhelloworld. STRINGFROMNDK());
Code Location:
http://download.csdn.net/download/highkgao1988/8982487
Use Eclipse for Android NDK development (Javah for header file generation)