There are two main scenarios for the JNI development of Android: one is to use a compiled. so dynamic library; The following are respectively described:
First, use the already compiled so
This is a relatively simple case, just put the. so file in the appropriate directory. As follows:
. [Module_name]
. . [SRC]
. . . [Main]
. . . . [Jnilibs]
. . . . . [Armeabi]
. . . . . [ARMEABI-V7A]
. . . . . [x86]
. . . . . [MIPS]
Note that the jniLibs
directory is placed module
below , in Android Studio, the effect is as follows, so after compiling so will be automatically packaged into the APK, the code directly LoadLibrary.
String libname = "HELLONDK"; Library name, note that there is no prefix lib and suffix. So 2 system.loadlibrary (libname);
Second, the use of C + + source code
1 r9d or later NDK
First make sure your NDK version is above r9d, and the latest available is R10:
http://tools.android-studio.org/
Thanks for the selfless dedication of the Chinese team at Android Studio.
If below the r9d version, the NDK compilation under Android Studio will appear with No rule to make target error.
2 Creating a normal Android project
3 Configuring Ndk.dir
local.properties
Add the following configuration:
sdk.dir=/path/to/android-sdk
ndk.dir=/PATH/TO/ANDROID-NDK
4 writing Java code
Create a Java class, Com.parbat.java.jni.JNIBridge.java
Package com.parbat.java.jni;
/**
* Created by 1 on 2017/3/14.
*/
public class Jnibridge {
Public native String Getstrfromjni ();
}
Called in the Startup class Mainactivity.java
String str = new Jnibridge (). Getstrfromjni ();
LOGUTILS.V ("JNI str =" + str);
static {
System.loadlibrary ("Jnitext"); DefaultConfig.ndk.moduleName
}
Build.gradle Configuring the NDK module under 5 app
Defaultconfig {
ApplicationID "Com.parbat.looper"
Minsdkversion 18
Targetsdkversion 24
Versioncode 1
Versionname "1.0"
ndk{
ModuleName "Jnitext"//generated so name
Abifilters "Armeabi", "armeabi-v7a", "x86"//output specify the so library under three ABI architectures
}
}
ndk
You can also configure more options, as follows
NDK { modulename "Myepicgamecode" cFlags "-dandroid_ndk-d_debug dnull=0" //Define some macros Ldlibs "EGL", "GLESv3", "DL", "Log" //link with these libraries! here Add the library STL you originally linked in Makefile Ldlibs Shared " //Use shared STLport Library}
Gradle.properties Add Property under Project
Android.usedeprecatedndk=true
6. Click Make-project
Under normal circumstances, there will be a NDK folder in the directory intermediates, where there is a generated. so file. Test the normal running project and you can see that our code can already invoke the Jni file.
Development of JNI applications in Android Studio