The NDK programming for Android requires the download of the NDK compilation environment, which can be downloaded from the official website window64 and then extracted to any directory.
The implementation of the NDK is actually using the Jni method in Java, so the previous steps can refer to the implementation of JNI, but can be compiled using the NDK tools.
First write the Java source program, for example:
1 Packagetest.android;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.widget.Toast;7 Importandroid.widget.LinearLayout;8 ImportAndroid.widget.Button;9 Ten Public classMiniextendsActivity { One A Static { -System.loadlibrary ("JT"); - } the - Public nativeString Stringfromjni (); - - Public voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - +LinearLayout Lla =NewLinearLayout ( This); AButton B =NewButton ( This); atB.settext ("Click Me"); - - Lla.addview (b); - This. Setcontentview (LLA); - - FinalActivity _this = This; in -B.setonclicklistener (NewView.onclicklistener () { to @Override + Public voidOnClick (View v) { - Toast.maketext (_this, Stringfromjni (), Toast.length_long). Show (); the } * }); $ }Panax Notoginseng}
Use the Javah tool to generate the. h header file, and then Create a new JNI directory in the project root directory and create a new Jni.c file in the JNI directory, as follows:
1#include <string.h>2#include <jni.h>3#include"test_android_mini.h"4 5 jniexport jstring jnicall java_test_android_mini_stringfromjni6(JNIENV *env, Jobject _this)7 {8 return(*env)->newstringutf (env,"return from C");9}
Create a new Android.mk file (note case) in the JNI directory, which shows how to compile the dynamic link library, as follows:
1 Local_path: = $ (call my-dir)23include $ (clear_vars)4 5 local_module: = JT6 local_src_files: = jni.c78 Include $ (build_shared_library)
Open the command handler, switch to the project directory, and enter the following command:
D:\Android\android-ndk-r11c\ndk-build
Ndk-build Front is your NDK decompression directory.
At this point, the project will produce the Libs directory, which is the generated dynamic link library.
Because Android has a multi-CPU platform, you can also configure the generated platform type with a single file.
Create a new Application.mk (note case) in the JNI directory and enter the following:
App_abi: = Armeabi armeabi-v7a x86
Using the Ndk-build command again to compile, you will generate a dynamic-link library of three platforms under Libs
The next packaging process and the previous packaging step is only one step, using Apkbuilder to package all files into an apk, add the dynamic link library file directory, with the-NF directory, for example:
D:\Android\sdk\tools\apkbuilder.bat \workspace\test\android\minimum\mini_unsigned.apk-v-u-z D:\workspace\test\ ANDROID\MINIMUM\BIN\MINI.AP_-F D:\WORKSPACE\TEST\ANDROID\MINIMUM\BIN\CLASSES.DEX-RF D:\workspace\test\android\ MINIMUM\SRC-NF D:\workspace\test\android\minimum\libs
Then the signature will generate the final APK file.
The simplest Android project (NDK command-line compilation)