[英]Android Studio NDK: Compile with ndk-build and get native support with Gradle Experimental
为了在 Android Studio 编辑器中获得索引,我应该在 build.gradle 中添加以下代码:
ndk {
moduleName "MyModule"
CFlags.add("-I${file("src/main/jni/headers1")}".toString())
CFlags.add("-I${file("src/main/jni/headers2")}".toString())
}
但是随后 gradle 忽略了我的 Android.mk,如果我从 build.gradle 中删除此代码,那么我将无法在编辑器中获得正确的索引,因为所有头文件都在这 2 个文件夹中。 有谁知道如何通过我的 Android.mk 编译 gradle 并且仍然可以进行本机编辑和调试?
我正在使用:Android Studio 2.1 stable gradle-experimental:0.7.0 my build.grade:
apply plugin: "com.android.model.application"
def ndkDir = System.getenv("ANDROID_NDK_HOME")
def propertiesFile = project.rootProject.file('local.properties')
if (propertiesFile.exists()) {
Properties properties = new Properties()
properties.load(propertiesFile.newDataInputStream())
ndkDir = properties.getProperty('ndk.dir')
}
model {
android.sources {
main {
jni {
source {
srcDirs.removeAll()
srcDir 'src/main/none'
}
}
jniLibs {
source {
srcDir 'src/main/libs'
}
}
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
ndk {
moduleName "MyModule"
CFlags.add("-I${file("src/main/jni/headers1")}".toString())
CFlags.add("-I${file("src/main/jni/headers2")}".toString())
}
defaultConfig {
applicationId "com.myapp.android.me"
minSdkVersion.apiLevel 19
targetSdkVersion.apiLevel 23
}
buildTypes {
debug {
ndk {
debuggable = true
}
}
}
}
}
dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile 'com.google.code.gson:gson:2.2.4'
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK'){
commandLine "${ndkDir}/ndk-build",'NDK_DEBUG=1','NDK_PROJECT_PATH ='+ getProjectDir() + '/src/main'
}
tasks.withType(AbstractCompile) {
compileTask -> compileTask.dependsOn buildNative
}
您可以设置实验插件来运行您的buildNative任务,而不是内置的编译和链接任务:
tasks.all {
task ->
if (task.name.startsWith('compile') && task.name.contains('MainC')) {
task.enabled = false
}
if (task.name.startsWith('link')) {
task.enabled = false
}
if (task.name.endsWith('SharedLibrary') ) {
task.dependsOn buildNative
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.