The Java Native Interface (JNI) standard is part of the Java platform that allows Java code to interact with code written in other languages. JNI is a local programming interface that enables Java code that runs inside a Java Virtual machine (VM) to interoperate with applications and libraries written in other programming languages, such as C, C + +, and assembly language.
Since the classes in the Android application layer are written in Java, these Java classes are compiled into a DEX-style bytecode and must be performed by Dalvik virtual machines (vm:virtual Machine). The VM plays a very important role in the Android platform.
(Many of us do not say, introduction or anything everywhere can go to search)
Let's build a Ndkjnidemo project first.
New project. png
New Jniutils class implementation native method
public class Jniutils {public static native String getstringformc ();
}
Build.png
Clean project then rebuild project to generate the class file,
This time, open the folder below to see if the Classes folder has been generated, no build please come back.
folder. png
Open the terminal input command again
CD App/build/intermediates/classes/debug
And then enter the instructions.
Javah-jni com.wobiancao.ndkjnidemo.ndk.JniUtils
Note Here Javah-jni is followed by the full path of the Jniutils class, if the Javah newspaper does not exist or not, your Java environment is not configured well.
Paste_image.png
At this time open classes/debug the following file found one more file
Com_wobiancao_ndkjnidemo_ndk_jniutils.h
Then, under Src/main, create a new folder Jni, copy or cut the generated. h files to the Jni folder, create a new C class, and add the following code
//// created by xy on 16/1/4.//#include "Com_wobiancao_ndkjnidemo_ndk_
JniUtils.h "/* * class: java_com_wobiancao_ndkjnidemo_ndk_jniutils * method: getstringformc * signature: () Ljava/lang/String; * * JNIEXPORT JSTRING JNICALL JAVA_COM_WOBIANCAO_NDKJNIDEMO_NDK_JNIUTILS_GETSTRINGFORMC (jnienv *env, jobject obj) { return (*ENV)->newstringutf (env, "here is a string from C");
The header file #include <jni.h> is red because we have not yet configured the NDK environment to open the FILE->PROJECT structure
Select the NDK environment path you downloaded, if there is no NDK here's an address. You can download an Android tool collection site: http://androiddevtools.cn/
Paste_image.png
After the set up, found that the header file is still red, and then build the project, there will be a hint
ERROR:NDK integration is deprecated at the current plugin. Consider trying the new experimental plugin. For details, Http://tools.android.com/tech-docs/new-build-system/gradle-experimental. The Set "Android.usedeprecatedndk=true" in gradle.properties to continue using the current NDK integration.
Just follow the prompts. Add android.usedeprecatedndk=true at the end of gradle.properties file ok
Then add the following code in the Build.gradle->defaultconfig parentheses under the app file
NDK {
modulename "Ndkjnidemo"//generated so name abifilters "Armeabi",
"armeabi-v7a", "x86"/output specified three kinds of ABI The so library under the architecture is currently optional. }
By the start of the build project, the discovery has not turned red. The next step is to use, add the following code in the Jniutils class
static {
system.loadlibrary ("Ndkjnidemo");//before the so name set in Build.gradle must be consistent}
Then the simple call on the line, mainactivity code as follows
public class Mainactivity extends Appcompatactivity {
TextView TextView;
@Override protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView = (TextView) Findviewbyid (r.id.ndk_text);
Textview.settext (JNIUTILS.GETSTRINGFORMC ());
}
}
Run the results as shown
Run diagram. png
We turned on the App->intermediates-ndk-debug discovery generated three folders and corresponded to the abifilters we had previously configured in Build.gradle
Done.
Create a new Libs folder put these three files in.
Paste_image.png
Then we try to delete our JNI files, whether the project can be used.
OK Tutorial finished
Finally, a use of JNI AES encryption demo run results as shown in the figure
AES encryption. png