Development of JNI applications in Android Studio

Source: Internet
Author: User

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.propertiesAdd 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
    }
}

ndkYou 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.