1)android ndk配置流程
1.新建立一个android的工程
2.创建一个带有nativie 方法的类,并加载后续想要的库名
public class JniTest{
public static native String sayHello();
static {
System.loadLibrary("JniTest");
}
}
3.点击编译,生成一个JniTest.class文件,路径如下图
QQ图片20160825171702.png
4用javah命令 在src/main目录底下创建jni目录 并生成.h文件
javah -d app/src/main/jni -jni -classpath app/build/intermediates/classes/debug jwl.hellondk.JniTest
5在jni目录下面建立 .c 文件,引入刚刚生成的.h文件
#include "jwl_hellondk_jniTest.h"
JNIEXPORT jstring JNICALL Java_jwl_hellondk_JniTest_sayHello
(JNIEnv *env, jclass obj){
return (*env)->NewStringUTF(env,"I'm jni");
}
6.在app下面的build.gradle添加
defaultConfig {
......
......
ndk{
moduleName "JniTest" //生成的so名字
abiFilters "armeabi", "armeabi-v7a", "x86" //输出指定三种abi体系结构下的so库。
}
}
gradle.properties文件添加 android.useDeprecatedNdk=true
local.properties 文件添加
ndk.dir=E\:\\BaiduYunDownload\\android-ndk-r10d
sdk.dir=D\:\\androidTools\\android_sdk\\adt-bundle-windows-x86_64-20140321\\sdk
2)开始编译,以及出现的问题
:app:compileDebugNdk
:app:compileDebugJavaWithJavac UP-TO-DATE
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\BaiduYunDownload\android-ndk-r10d\ndk-build.cmd'' finished with non-zero exit value 2
make.exe: *** No rule to make target `E:\android_studio\HelloNdk\app\build\intermediates\ndk\debug\obj/local/armeabi-v7a/objs/JniTest/E_\android_studio\HelloNdk\app\src\main\jni', needed by `E:\android_studio\HelloNdk\app\build\intermediates\ndk\debug\obj/local/armeabi-v7a/objs/JniTest/E_\android_studio\HelloNdk\app\src\main\jni\JniTest.o'. Stop.
Information:BUILD FAILED
网上给的解决办法是在jni目录创建一个空的.c文件
3) 点开.cpp文件,发现好多红色
QQ截图20160825174234.png
google后给的资料是
sourceSets.main{
jni.srcDirs = [] //去掉gradle默认编译Android.mk文件
jniLibs.srcDir 'src/main/libs' //存放so目录
}
这样就需要自己编写Android.mk文件
4)如何让build.gradle编译jni目录下Android.mk文件
修改build.gradle
导入 import org.apache.tools.ant.taskdefs.condition.Os
在 android {....} 中添加如下代码
sourceSets {
main {
jni.srcDirs = []
jniLibs.srcDirs = ['src/main/libs']
}
}
task ndkBuild(type: org.gradle.api.tasks.Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd() //执行ndk-build 命令
}tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
task cleanNative(type: Exec) {
workingDir file('src/main') commandLine getNdkBuildCmd(), 'clean'
}
clean.dependsOn cleanNative
2.android{} 外面添加如下:
def getNdkDir() {
if (System.env.ANDROID_NDK_ROOT != null)
return System.env.ANDROID_NDK_ROOT
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties')
.newDataInputStream()) //从属性文件里得到ndk路径
def ndkdir = properties.getProperty('ndk.dir', null)
if (ndkdir == null)
throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.")
return ndkdir
}
def getNdkBuildCmd() {
def ndkbuild = getNdkDir() + "/ndk-build"
if (Os.isFamily(Os.FAMILY_WINDOWS))
ndkbuild += ".cmd"
return ndkbuild
}
5)如何将第三方需要的.so文件添加到你指定的目录
1.首先将第三方 xxx.so拷贝到jni目录下面
2.然后在自己写的Android.mk中末端添加如下代码
include $(CLEAR_VARS)
LOCAL_MODULE := xxx
LOCAL_SRC_FILES := xxx.so
include $(PREBUILT_SHARED_LIBRARY)