Android Studio NDK:使用ndk-build进行编译,并通过Gradle Experiment获得本机支持 [英] Android Studio NDK: Compile with ndk-build and get native support with Gradle Experimental

查看:759
本文介绍了Android Studio NDK:使用ndk-build进行编译,并通过Gradle Experiment获得本机支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Android Studio编辑器中建立索引,我应该在build.gradle中添加以下代码:

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

但是gradle忽略了我的Android.mk,如果我要从build.gradle中删除此代码,那么由于所有头文件都在这2个文件夹中,因此我在编辑器中将无法获得正确的索引. 有谁知道如何通过我的Android.mk进行编译,并且仍然可以进行本机编辑和调试?

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?

我正在使用: Android Studio 2.1稳定 gradle-experimental:0.7.0 我的build.grade:

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
}

推荐答案

您可以设置 experimental 插件来运行您的 buildNative 任务,而不是内置的编译器并链接任务:

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

这篇关于Android Studio NDK:使用ndk-build进行编译,并通过Gradle Experiment获得本机支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆