Android Studio NDK: Compile with ndk-build and get native support with Gradle Experimental

元气小坏坏 提交于 2019-12-12 19:32:07

问题


In order to get indexing in the Android Studio editor I should add the following code in build.gradle:

ndk {
            moduleName "MyModule"
            CFlags.add("-I${file("src/main/jni/headers1")}".toString())
            CFlags.add("-I${file("src/main/jni/headers2")}".toString())
}

but then gradle ignores my Android.mk, If I will remove this code from build.gradle, then I won't get proper indexing in the editor since all the header files are in those 2 folders. Is anyone knows how to make gradle to compile by my Android.mk and and still get native editing and debugging?

Im using: 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
}

回答1:


You can set up the experimental plugin to run your buildNative task instead of the built-in compile and link tasks:

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
        }
}


来源:https://stackoverflow.com/questions/36977374/android-studio-ndk-compile-with-ndk-build-and-get-native-support-with-gradle-ex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!