Because of the relationship between work content, the work of the NDK involves a relatively wide range (confidentiality, security), so this chapter describes the basic use of the NDK.
There are many such tutorials or descriptions on the Web, but the description is not entirely
Development tools: Android Studio 2.1.2
NDK version: ANDROID-NDK-R10E, supports compilation of 64-bit so libraries
JDK version: 1.8 64 bit
Use the following steps:
The first step: the construction of the NDK environment
①jdk,android SDK These are fully prepared (as long as the normal use of Android studio, this one does not consider the basic)
② downloads the NDK, which can be downloaded through the SDK manager of Android Studio or via this link: As for the version, choose the latest version according to your computer system.
If you do not access, you need to use FQ, you can download the Day line VPN (trial version, one hours a day, the key is not money can also FQ), Fq tool download link: VPN tool download
Step Two: Create a new project to complete the NDK configuration
① Add the NDK's path code to the local.properties of the project:
ndk.dir=c\:\\android-ndk-r10e
② in Gradle.properties to declare code that uses the NDK
Android.usedeprecatedndk=true
③ in the app's Build.gradle, add the NDK's compilation information (including the generated so library name, and the various platform versions compiled)
Apply plugin: ' Com.android.application ' android { compilesdkversion buildtoolsversion "24.0.0" defaultconfig { ApplicationID "Edan.com.as_ndk_demo" minsdkversion targetsdkversion Versioncode 1 versionname "1.0" ndk{ modulename "Mytestjnilib"//generated. so name abifilters "Armeabi", " armeabi-v7a "," x86 " } } buildtypes { release { minifyenabled false Proguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro ' } } sourcesets { main { jnilibs.srcdirs = [' Libs '}}} dependencies { Compile filetree (dir: ' Libs ', include: [' *.jar ']) testcompile ' junit:junit:4.12 ' Compile ' com.android.support:appcompat-v7:23.4.0 '}
Step three: Edit the native method to generate the. h header File
① editing the native method
View Code
② Click Build-->make Project to generate the class file
③ find the class file under the App->build->intermediates->classes->debug directory:
④ generate the. h header file via the Javah command, click Terminal in the menu under Android Studio
In turn:
CD App\build\intermediates\classes\debug
Javah-jni edan.com.as_ndk_demo.ndktest.NdkJniUtils
When using Javah, the specific package name is written according to the actual situation
There are no errors, you can see the. h header file in the Debug directory
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Edan_com_as_ndk_demo_ndktes T_ndkjniutils */#ifndef _included_edan_com_as_ndk_demo_ndktest_ndkjniutils#define _included_edan_com_as_ndk_demo_ Ndktest_ndkjniutils#ifdef __cplusplusextern "C" {#endif/* * Class: edan_com_as_ndk_demo_ndktest_ndkjniutils * Method: getclanguagestring * Signature: () ljava/lang/string; */jniexport jstring Jnicall Java_edan_com_as_1ndk_ 1demo_ndktest_ndkjniutils_getclanguagestring (jnienv *, jobject); #ifdef __cplusplus} #endif #endif
Fourth step: Build the. So library file based on the generated. h header file, as well as the new. C or. cpp file.
① Create a new JNI folder under the App->src->main directory to store header files and source files in C/D + +, and then cut the. h header files into here and create a new. C or. cpp file
#include "edan_com_as_ndk_demo_ndktest_ndkjniutils.h"/* * Class: com_example_edu_ndktest_ndkjniutils * Method: getclanguagestring * Signature: () ljava/lang/string; */jniexport jstring Jnicall Java_edan_com_as_1ndk_1demo_ Ndktest_ndkjniutils_getclanguagestring (jnienv *env, Jobject obj) { return (*env)->newstringutf (env, "My Name is ouyangshengduo,hi! "); }
② Click Build->make Project to compile, build the. So library file, path: App->build->intermediates->ndk->debug->lib under:
③ copy these folders under Lib to app->libs:
Fifth step: Show the contents of C through mainactivity TextView:
Package Edan.com.as_ndk_demo;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.widget.textview;import Edan.com.as_ndk_demo.ndktest.ndkjniutils;public class MainActivity extends appcompatactivity { private TextView mtextview; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mtextview = (TextView) Findviewbyid (r.id.text); Ndkjniutils ndk_util = new Ndkjniutils (); Mtextview.settext (Ndk_util.getclanguagestring ());} }
At this point, the basic of the ndk of Android Studio is finished, during any problems or errors encountered, copy the error message to Google, the basic can be resolved, of course, you can leave a message, know that will liker.
Project source code: Source Download
The NDK chapter of Android Studio