用androidstudio开发的小伙伴,应该知道自身的ndk自动编译就是鸡肋!
这里说2种方法:
1> 进入到工程jni目录运行ndk-build 如何快速复制jni路径
右键Copy Path或者按快捷键Ctrl+Shift+C
cd /home/wangxiong/Documents/Github/libraries/blur/src/main/jni~/Soft/android-ndk-r10e/ndk-build
编译完成就会在libs生成各个平台的so文件
2> 第2种方式,脚本配置首先要把as自动编译关掉
import org.apache.tools.ant.taskdefs.condition.Os
//导入Os包,方便下面判断系统平台Linux \ windows
apply plugin: 'com.android.library'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
def getNdkBuildName() {//NDK编译工具名称,区别Linux和windows
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
return "ndk-build.cmd"
} else {
return "ndk-build"
}
}
def getNdkBuildFullPath() {//NDK全路径
File propFile = project.rootProject.file('local.properties')
if (!propFile.exists()) {
return getNdkBuildName()
}
Properties properties = new Properties()
properties.load(propFile.newDataInputStream())
def ndkCommand = properties.getProperty('ndk.command')
if (ndkCommand != null) {
return ndkCommand
}
def path = null
def ndkPath = properties.getProperty('ndk.path')
if (ndkPath != null) {
path = ndkPath
} else {
def ndkDir = properties.getProperty('ndk.dir')
if (ndkDir != null) {
path = ndkDir
}
}
if (path != null) {
if (!path.endsWith(File.separator)) {
path += File.separator
}
return path + getNdkBuildName()
} else {
// if none of above is provided, we assume ndk-build is already in $PATH
return getNdkBuildName()
}
}
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode = 200
versionName = "2.0.0"
}
buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.pro'))
}
}
sourceSets.main {
jni.srcDirs = [] //关掉自动编译
jniLibs.srcDirs 'src/main/libs'
}// This prevents the auto generation of Android.mk
}
task hello_ndk_build(type: Exec) {
commandLine getNdkBuildFullPath(),
'NDK_APPLICATION_MK=Application.mk',
'NDK_OUT=' + temporaryDir,
"NDK_LIBS_OUT=" + file("src/main/libs").absolutePath,
'-C', file("src/main/jni").absolutePath,
'--jobs', Runtime.getRuntime().availableProcessors()
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn "hello_ndk_build"//与task任务名相同
}
task hello_ndk_clean(type: Exec) {
commandLine getNdkBuildFullPath(),
'clean',
'-C', file("src/main/jni").absolutePath
}
clean.dependsOn 'hello_ndk_clean'//clean依赖上面定义的任务
参数ps:
@NDK_PROJECT_PATH
指定NDK编译的代码路径为当前目录,如果不配置,则必须把工程代码放Android工程的jni目录下
@NDK_APP_APPLICATION_MK
指定NDK编译使用的application.mk文件 @clean清除所有编译出来的临时文件和目标文件
@NDK_OUT
指定编译生成的文件的存放位置
@NDK_LIBS_OUT
编译后最终的lib目录
注意观察build目录下生成的一些文件,和编译配置时的关联!