Native Development Kit Download NDK Development Kit
Links: http://developer.android.com/ndk/downloads/index.html
Select Ndk r10e
Android-ndk-r10e-windows-x86_64.exe
Self-extracting file size 400M, after decompression 3g+.
Configuring NDK Environment variables
Add the NDK directory to the System environment variable path, and then verify the Ndk-build in the newly opened CMD input:
Hellojni
When installing ADT, Eclipse installs Android Native development Tools,
Import the example in Android-ndk-r10e\samples\hello-jni to eclipse:
The initial directory structure:
Then open cmd and switch to the HELLOJNI project directory:
Execute ndk-build to compile:
When finished, go back to eclipse to refresh the Hellojni directory and find that the obj folder is added with the. So library files for each schema, such as:
Then run the project:
Note that when you execute ndk-build above, you compile a lot of schemas, the time will be longer, if you just want to compile the arm we need, you can comment the following contents of the Application.mk file under the project JNI directory:
#APP_ABI := all
Clear the last compilation:
ndk-build clean
Again Ndk-build, soon completed:
The first NDK project
1. New Android Project
Create a native method in Mainactivity:
publicstaticnativegetStringFromC();
2. Create a JNI directory
3. Writing Java Layer native methods
4. Generating JNI header files
CMD switches to the project directory and then executes:
Javah-classpath bin/classes-d JNI com.zms.hellondk.MainActivity
Javah-classpath bin/classes;d:\android\sdk\platforms\android-22\android.jar-d JNI com.zms.hellondk.MainActivity
This is troublesome, or you can: add Android.jar to environment variable: D:\Android\sdk\platforms\android-22\android.jar
You can see that the. h header file is generated under the JNI directory:
The format is as follows:
Then create our own C files under the JNI directory:
hello.c
#include<stdio.h>#include<stdlib.h>#include"com_zms_hellondk_MainActivity.h"JNIEXPORT jstring JNICALL Java_com_zms_hellondk_MainActivity_getStringFromC( JNIEnv env, jclass jclass) { return"Hello from JNI !");}
Then create the Android.mk file under the JNI directory:
Android.mk
LOCAL_PATH:$(call my-dir)include$(CLEAR_VARS)LOCAL_MODULE :HelloNdkLOCAL_SRC_FILES:#对哪个c文件进行编译include$(BUILD_SHARED_LIBRARY)
Then go to cmd to switch to the project directory to compile:
Note that the mainactivity is written with LoadLibrary, the module name in the quotation marks, and the code for the static zone declaration is executed before the OnCreate method:
PackageCOM.ZMS.HELLONDK;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.TextView; Public class mainactivity extends Activity { Public Static nativeStringGETSTRINGFROMC();@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); TextView Textfromjni = (TextView) Findviewbyid (R.ID.TEXTFROMJNI); Textfromjni.settext (GETSTRINGFROMC ()); }Static{System.loadlibrary ("HELLONDK"); }}
Otherwise, the following error will occur:
(--Don't ask me how I know)
NDK Related Concepts
Android NDK Development Basics