On the development of NDK in Android studio
- Environment configuration
1) Download NDK
2) Configure the NDK path in Android studio
3) configuration Gradle
Add Android.usedeprecatedndk=true configuration to the gradle.properties to open the NDK
In the development process we will sometimes use C + + to the STL library then we need to add in the Build.gradle
Defaultconfig {
NDK {
ModuleName "Jnitest"
STL "Stlport_static"
Abifilters "Armeabi", "armeabi-v7a", "x86"
}
}
Because Android.mk files in Android Studio are not configurable. Instead, the Android.mk file is automatically generated based on the configuration in Gradle
- New Project
Right-click in the project catalog main to create a new JNI folder, which is then used in this folder
Create a new class under the Java folder under the main folder for example: Jniutils
and add code public static native String Getstringfromjni ();
Then make the module, the purpose is to compile the corresponding class file. Then, based on the generated class file, the corresponding. h header file is generated using Javah.
Click on the Terminal tab of Android Studio and go to the project's app folder by default. If you enter CD Src/main in this directory, switch to the main directory.
javah-d JNI com.vvgool.jni.JniUtils (-D generates a JNI directory and generates it in the directory even if the JNI directory is not manually generated in case of using-D)
/* Don't EDIT this file-it are machine generated * /#include <jni.h>/ * Header for class com_vvgool_jnitest_jniutils * /#ifndef _included_com_vvgool_jnitest_jniutils#define _included_com_vvgool_jnitest_jniutils#ifdef __cplusplusextern"C"{#endif/* * class:com_vvgool_jnitest_jniutils * METHOD:GETSTRINGFROMJNI * Signature: () ljava/lang/string; */Jniexport jstringJnicallJava_com_vvgool_jnitest_jniutils_getstringfromjni (jnienv *, jclass);#ifdef __cplusplus}#endif#endif
Then we can create a new. cpp file or a. c file in the Jni file. I'm here to create a new file to Test.cpp.
Finally added in the Jniutils
static{
System.loadlibrary ("Jnitest");
The Jniutils.getstringfromjni () method is called in the activity to get the string.
On the development of NDK in Android studio