屏蔽gradle自动生成的Android.mk,而去使用自己编写的Android.mk文件去编译.
在build.gradle中:
applyplugin:
'com.android.application'
android {
………………
………………
sourceSets {
main {
jni.srcDirs = []
//设置禁止gradle生成Android.mk
jniLibs.srcDirs = [
'src/main/jniLibs'
]
//设置目标的so存放路径
}
}
}
然后继续在build.gradle中添加:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn
'ndkBuild'
,
'copyThirdso'
,
'copyJniLibs'
}
task ndkBuild(type: Exec) {
//设置新的so的生成目录
//"ndk-build.cmd" 在Mac下为"ndk-build"
def ndkBuildingDir = project.plugins.findPlugin(
'com.android.application'
).sdkHandler.getNdkFolder().absolutePath
commandLine ndkBuildingDir +
"/ndk-build.cmd"
,
'-C'
,
'src/main/jni'
,
"NDK_OUT=$buildDir/intermediates/ndk/obj"
,
"NDK_APP_DST_DIR=$buildDir/intermediates/ndk/libs/\$(TARGET_ARCH_ABI)"
}
task copyJniLibs(type: Copy) {
//将新生成的so拷贝到jniLibs目录
from fileTree(dir: file(buildDir.absolutePath +
'/intermediates/ndk/libs'
), include:
'**/*.so'
)
into file(
'src/main/jniLibs'
)
}
task copyThirdso(type: Copy) {
//将第三方的so拷贝到jniLibs目录
from file(
'src/main/jni/vvw/libs'
)
into file(
'src/main/jniLibs'
)
}