Reference:
http://blog.csdn.net/sbsujjbcy/article/details/48469569
http://www.androiddevtools.cn/
NDK Download and configuration:
Android Studio has added NDK support since version 1.3 (see Android NDK Preview), so if you need to use the NDK, it needs to be updated to the >=1.3 version, which is used in the 2.1 version (the latest version when writing this article).
Two methods:
The first type:
Android Studio comes with SDK manager download NDK, now Although Google is wall, but feel updated download these development tools is smooth.
Figure A
Install NDK to display the install in, and prompt for updates:
Figure II
The second type:
1, can be downloaded separately (can be downloaded here), and then unzip it.
2. Enter the path of the NDK in the following location:
Might
Once configured, the following code appears in Local.properties:
ndk.dir=D\:\\Android\\android-ndk-r10
When downloading manually, the NDK has two types of zip and exe. I think the difference is that the download zip, directly extracted after use, is not shown in Figure two install, and can not prompt for updates.
The first NDK project
Reference:
http://yanbober.github.io/2015/02/14/android_studio_jni_1/
http://blog.csdn.net/qiujuer/article/details/42040963
1. Build new project Ndkdemo and create 2 new classes
Because there is only one class, to generate a good JNI file, the compilation will always appear the following error, I do not know why (see the NDK bug in Windows), two classes, the generation number JNI file, it will work!
Make.exe: * * * No rule to make target ' F:\studioprojects\ndktest2\app\build\intermediates\NDK\debug\obj/local/armeabi-v7a/objs/jnidemo/f_\studioprojects\ndktest2\app\src\main\jni', needed by ' F:\studioprojects\ndktest2\app\build\intermediates\NDK\debug\obj/local/armeabi-v7a/objs/jnidemo/f_\studioprojects\ndktest2\app\src\main\jni\com_EXAMPLE_XUE_NDKTEST2_MATHKIT.O '. Stop.Error:Execution failed for task ': App:compiledebugndk ' .> com.android.ide.common.process.ProcessException: Org.gradle.process.internal.ExecException:Process ' Command ' D:\android\android-ndk-r10\NDK-build.cmd ' finished with Non-zero exit value 2
In Mathkit we implement the calculation of the square number of the INT value.
publicclass MathKit { publicstaticintsquare(int num); static { System.loadLibrary("JniDemo"); }
Stringkit This class what function also does not have, sucks's:
publicclass StringKit { publicstaticvoidsetNull(String str); static { System.loadLibrary("JniDemo"); }
2. Prepare the. h file
There are two ways to recommend mastering the first type:
The first type:
- Enter the Java directory for the Ndkdemo project;
- CMD enters the directory for Javah operation (you can hold down SHIFT right-click here to open the command window):
Note that you must enter the full package name Net.qiujuer.ndkdemo.jni.MathKit(not necessarily the same as the package name) in the project's Java directory.
Some friends may know that Java in the compilation of files, the package name will be converted to a path, as long as the control of the input Javah command to run the path and the input package name is OK. In fact, please look (I re-establish the project, so the package name and not the same)
The second type:
1,build->make modules Get intermediate file, we are concerned about the. class file.
2. The class file generated after compiling OK is in the following directory of the AS project: \app\build\intermediates\classes\debug.
3. Then the next step is to generate the corresponding. h header file using Javah based on the generated class file.
4. Click the Terminal tab of the AS and go to the app folder of the project by default. I enter the following command under the Windows platform to jump to the class intermediate file generation path:
xxxxx\app> cd build\intermediates\classes\debug
5. Then execute the following Javah command to generate the H file.
xxxxx\debug> javah -jni net.qiujuer.ndkdemo.jni.MathKit
6. After execution you can see the generated. h header file under folder \app\build\intermediates\classes\debug:
Net_qiujuer_ndkdemo_jni_mathkit.h
3. Create a JNI folder:
You can also create new->directory manually, with the folder named JNI.
4. Copy the. h file to the Jni folder and build the. cpp file
1. Copy the. h file generated in the second step to the JNI folder
2. New. cpp file, operations such as:
Implemented separately:
Net_qiujuer_ndkdemo_jni_mathkit.cpp
#include <net_qiujuer_ndkdemo_jni_MathKit.h> JNIEXPORT jint JNICALL Java_net_qiujuer_ndkdemo_jni_MathKit_square num) { returnnum*num; }
Net_qiujuer_ndkdemo_jni_stringkit.cpp
#include <net_qiujuer_ndkdemo_jni_StringKit.h> void JNICALL Java_net_qiujuer_ndkdemo_jni_StringKit_setNull (JNIEnv *env, jclass obj, jstring str) { }
5. Set the APP project Build.gradle
This setting is to set the generated JNI name to: Jnidemo
6. Main file Call:
By this perfect end, you can see the results of the operation:
More
In the usual operations we inevitably have to build:
Android.mk
LOCAL_PATH :$(call my-dir) include$(CLEAR_VARS) LOCAL_MODULE := genius LOCAL_SRC_FILES:= net_qiujuer_imageblurring_jni_ImageBlur.cpp LOCAL_LDLIBS := -lm -llog -ljnigraphics include$(BUILD_SHARED_LIBRARY
Application.mk
APP_ABI := all APP_PLATFORM:= android-19 APP_OPTIM := release
And here are the two files are not established, then how to decide?
We can press and hold CTRL on the NDK's modelname to click on the interface that will appear:
These are all parameters that can be set. corresponding to each:
ndk{ "JniDemo"// 设置生成的文件名 "-DANDROID_NDK -D_RELEASE"//这个对应的是生成模式 "m""log""jnigraphics"//这个对应 LOCAL_LDLIBS 但是需要去掉前面的 “l” "all"//这个是对应的 APP_ABI "stlport_shared"//这个对应的是 APP_STL }
The original author of this project.
NDK Configuration and simple projects