In front of the words: This article is a look at how to gracefully use the NDK after the original blog features complement. In order to facilitate everyone's reading order, directly added to the original blog, if there is copyright infringement, please contact me. This blog has been reproduced, there is original, only in the original blog has been modified, so classified to original, if not, please leave a message.
This blog reprint address is as follows:
How to use the NDK gracefully
Using the NDK for some time on Android studio, feeling that the official plugin com.android.tools.build.gradle-experimental is still not stable enough, there are some problems, but Google also stated that the plugin was in the experimental phase. Fortunately, the official has provided another way to use the NDK on Android studio, and this blog is about the way I feel so good about it so far.
Development environment
- Basic Android Development environment: Android Studio, SDK, JDK
- Download the NDK, download it yourself manually or download it using the SDK tool.
I'm using the version: Android Studio 1.5.1
Configure common commands
NDK development more commonly used command is to generate header files, build so package, in Android Studio we can first configure the command, it is very convenient to use, this is the best place I feel.
As shown, I added three commands and added by the plus sign
The configuration details for one of these commands are as follows:
Command Configuration parameters:
Javah for generating header files
Program:$JDKPath$/bin/javah
Parameters:-encoding UTF-8 -d ../jni -jni $FileClass$
Note: This command I added the-encoding UTF-8 The specified code, you can change to the code of your project.
Working directory:$SourcepathEntry$\..\java
Ndk-build for building so packages
Program:你的NDK目录\build\ndk-build.cmd
Note: For Windows ndk-build.cmd
, mac/linux withndk-build
Parameters: No need to fill in anything
Working directory:$ModuleFileDir$\src\main
Ndk-build Clean Clear So pack
Program:你的NDK目录\build\ndk-build.cmd
Note: For Windows ndk-build.cmd
, mac/linux withndk-build
Parameters:clean
Working directory:$ModuleFileDir$\src\main
========== Here Original Blog No, Supplement began ==========
makefile for generating android.mk and Application.mk
And the fixed configurations added to the gradle.properties and local.properties files
Program: 你的NDK目录\makefile.cmd
注意:makefile.cmd源码如下,请自行放到对应上面的目录
Parameters:$FileName$ $ProjectFileDir$
Working directory:$FileDir$
Makefile.cmd source code is as follows:
@EchoOffrem Parameter 1 No manual pass-through, right-click to execute the makefile commandREM C + + source file name (with suffix)Setparamter1=%1REM Parameter 2 the directory where the project residesSetparamter2=%2Setdot=%paramter1:~-2,-1%REM Auto-judge C + + file, generate corresponding parametersif"." =="%dot%" (SetFilename=%paramter1:~0,-2%)Else(SetFilename=%paramter1:~0,-4%)rem android.mk files in ParametersEchoLocal_path: = $ (Pagermy-dir) > Android.mkEchoInclude $ (clear_vars) >> android.mkEcho. >> android.mkEchoLocal_module: =%filename%>> android.mkEcho|Set/p=local_src_files:=>> android.mkREM Traversal of all C + + source files under the current folder for/R%%i inch(*.c *.cpp) Do(Echo|Set/p=%%~Nxi>> android.mk)Echo.>> android.mkEchoInclude $ (build_shared_library) >> android.mkrem application.mk files in ParametersEchoApp_modules: =%filename%> application.mkEcho. >> application.mkEchoApp_abi: = All >> application.mkadditional configuration in rem gradle.propertiesEcho.>>%paramter2%/gradle.propertiesEchoAndroid.usedeprecatedndk=true>>%paramter2%/gradle.propertiesadditional configuration in rem local.propertiesEcho.>>%paramter2%/local.propertiesREM Note: d\:\\developkit\\android-ndk-r10e This is the directory where you changed your NDKEchoNdk.dir=d\:\\developkit\\android-ndk-r10e>>%paramter2%/local.properties
Note: D:\DevelopKit\android-ndk-r10e this to your own NDK directory, the others will not have to change
Instructions for using the command:
javah Command: executes on the class that contains the native method, generates a header file for the class to be used, and if there is more than one class that contains the native method, the corresponding execution occurs once
Makefile Command: click on the C + + source file that you want to be the module, the file name of the source file is the module, corresponding to the local_module in Android.mk and
The parameters behind the App_modules,local_src_files in Application.mk are listed according to the source files of C + + sources in the JNI directory.
Reference comprehension
These can be supplemented without configuration
android.mk
Auto-generated, can modify the configuration in it
LOCAL_PATH:$(call my-dir)include$(CLEAR_VARS)LOCAL_MODULE:NdkTestLOCAL_SRC_FILES:NdkTest.cppinclude$(BUILD_SHARED_LIBRARY)
application.mk
Auto-generated, can modify the configuration in it
APP_MODULES := NdkTestAPP_ABI := all
local.properties Automatically add a fixed configuration
ndk.dir=你的NDK目录的绝对路径
Like D:\DevelopKit\android-ndk-r10e, this is my NDK directory.
gradle.properties Automatically add a fixed configuration
true
Directly change the above makefile.cmd (this file is written in the DOS command line) corresponding to their own configuration. Does it make you less physically? I hope so, hehe ...
For me to add this supplement, what do not know can ask me, blog configuration process, I also personally tried, for the great God point like, particularly useful, recommend everyone to see OH.
========== Here Original blog No, add end ==========
After configuring three commands, you can find these commands in the right-click menu, which you can use directly. As shown in the header file that generated the Ndktest class
Configuration Engineering
Making the project use the NDK requires some configuration work
Fill in the local.properties file with the NDK used
ndk.dir=你的NDK目录的绝对路径
Add the following code to the gradle.properties file
true
Add the following code to the module's Build.gradle:
android { { ndk { "NdkTest"//定义NDKlibrary的名字 //ldLibs "log" 添加log库,看自己需求 } } //这里设置目录,默认就这样写就可以了 sourceSets { main { "src/main/jni" "src/main/libs" } }}
Hello Word
You can enjoy the NDK on Android studio after you've configured all of the steps above.
Write a simple program that returns a string of Hello Word.
First, declare a local method in Java and load the local library as follows:
publicclass NdkTest{ static { System.loadLibrary("NdkTest"); } publicstaticgetString();}
Generate its header file (the automatically generated name is the package name + class name Me_majiajie_ndktest_ndktest.h), and create a C + + file (NdkTest.cpp), as follows:
#include "me_majiajie_ndktest_NdkTest.h"*env, jclass jc){ return env->NewStringUTF("Hello word !!!");}
Create a file in the JNI directory named Android.mk, fill in the following content
LOCAL_PATH:$(call my-dir)include$(CLEAR_VARS)LOCAL_MODULE:NdkTestLOCAL_SRC_FILES:NdkTest.cppinclude$(BUILD_SHARED_LIBRARY)
Then create a file in the JNI directory named Application.mk, fill in the following content
APP_MODULES := NdkTestAPP_ABI := all
Note: Android.mk and application.mk are the NDK default profiles, and a detailed description of these two files can be crossing Web.
Finally, the so package is generated, so remember that every time you modify the C file, you need to regenerate the so package:
How to use the ndk[function elegantly in addition]