Now there are many practical jni tutorials on the Internet, this article is mainly used to record their own implementation methods and the pits encountered.
The first step is to download the NDK.
NDK can go to google Android developer download. Domestic also some Chinese version of the website can be downloaded.
The second step is to write the Java class, which is the intermediary of Java and C/s + + interaction, and defines some native methods, which are implemented by C + +.
Package Com.example.jni; /** */Publicclass jnidemo {static { System.loadlibrary ("newclient"); } Public Static native String SayHello ();}
The upper code is divided into two blocks, and the static code block is used to load the. So library that we follow to generate the C + + implementation. The second part defines a native local method that is implemented by C + +.
The third step is to generate the. h header File
Use Android Studio's Terminal terminal.
Go to the main directory f:\andriod\workspaceas\demo\newclient\src\main>, use the command to generate the. h header file javah-d jni-classpath. /java Com.example.jni.JNIDemo
-D JNI header files are generated to the JNI folder (currently in the <project>\app\src\main directory, so. h is located in the directory <project>\app\src\main\jni)
-classpath./java specifies the class to search for the package name under Java under the current path
The Jni folder and Com_example_jni_jnidemo.h will then be generated under the main folder
/* */<jni.h> * * * "C" {#endif /* * Class: com_example_jni_jnidemo * Method: */jniexport jstring jnicall java_com _example_jni_jnidemo_sayhello *, Jclass); #ifdef __cplusplus} #endif #endif
Fourth step to achieve main.cpp
The. h header file was generated, now to implement the specific implementation of C + +, create a new C + + file:
A main.cpp file is generated and the contents of the. h file are copied into the Main.cpp file and implemented as follows:
////Created by Alexjie on 2017/5/14.///*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Com_example_jni_jnidemo*/#ifndef _included_com_example_jni_jnidemo#define_included_com_example_jni_jnidemo#ifdef __cplusplusextern "C" {#endif/** Class:com_example_jni_jnidemo * Method:sayhello * Signature: () V*/jniexport jstring jnicall Java_com_example_jni_jnidemo_sayhello (jnienv*env, Jobject jobj) { //return a sentence returnEnv->newstringutf ((Char*)"JNI Hahahahahahahaha"); }; #ifdef __cplusplus}#endif#endif
Fifth step, configure the NDK
Open Project's Local.properties file to add the NDK path
Open the App module's Build.gradle file and add the following code to the Defaultconfig node
Note that the modulename here is the so library name we loaded in the class we wrote before.
NDK { "newclient" "Log", "Z", "M" "Armeabi", "armeabi-v7a", "x86" }
Add this sentence in gradle.properties:
Android.usedeprecatedndk=True
Sixth step, generate. So library
The rebuilt project is generated in the following directory. So library: App\build\intermediates\ndk\debug\lib
Under the main folder, create a new Jnilib folder and test the. So library into it.
You can now call Jnidemo in the program. SayHello () method.
Problem:
1.build apk Error: Installation failed with message failed to establish session
FIX: turn off MIUI optimization in MIUI developer options and reboot!
First Android Studio JNI program