android studio 已有NDK代码并且包含application.mk android.mk 使用ndkcmd 直接编译

从传统的NDK编译过渡到android studio 中去编译,不管如何改变,ndkcmd编译始终是支持的,

所以如下的配置,不管是新项目还是移植项目都是支持的,算是一种通用的配置。

依赖关系通过 mk文件维护

1,在APP module中的build.gradle中添加

Android{

task ndkBuild(type: org.gradle.api.tasks.Exec) {
        workingDir file('src/main')
//        workingDir file('src/main/TrulyHandsfreeSDK')
        commandLine  getNdkBuildCmd()
    }
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
    task cleanNative(type: Exec) {
        workingDir file('src/main')
        commandLine getNdkBuildCmd(),'clean'
    }
    clean.dependsOn cleanNative

}

 

def getNdkDir() {
    if(System.env.ANDROID_NDK_ROOT !=null)
        returnSystem.env.ANDROID_NDK_ROOT
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties')
            .newDataInputStream())
    def ndkdir = properties.getProperty('ndk.dir',null)
    if(ndkdir ==null)
        throw newGradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.")
    return ndkdir
}
def getNdkBuildCmd() {
    def ndkbuild =getNdkDir() +"\\ndk-build.cmd"
//    def ndkbuild ="E\\:\\adt\\sdk\\ndk-bundle\\ndk-build.cmd"
    /*if (Os.isFamily(Os.FAMILY_WINDOWS))
        ndkbuild += ".cmd"*/
    return ndkbuild
}

 

 

然后使用ndkBuild task编译即可:

android studio 已有NDK代码并且包含application.mk android.mk 使用ndkcmd 直接编译