How to use custom Android.mk with new gradle build system

断了今生、忘了曾经 提交于 2020-01-11 07:11:07

问题


I know how use custom Android.mk with old gradle:

    sourceSets.main {
        jniLibs.srcDir 'src/main/jni'
        jni.srcDirs = [] //disable automatic ndk-build call
    }

    // call regular ndk-build(.cmd) script from app directory
    task ndkBuild(type: Exec) {
        commandLine '/.../android-ndk-r10e/ndk-build', '-C', file('src/main').absolutePath
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }

It's not working with new gradle: com.android.tools.build:gradle-experimental:0.2.0 :

Error:Cause: com.android.build.gradle.managed.AndroidConfig_Impl

回答1:


with the new gradle-experimental plugin, your configuration would be:

model {
    //...
    android.sources{
        main.jni {
            source {
                srcDirs = ['src/main/none']
            }
        }
        main.jniLibs {
            source {
                srcDirs = ['src/main/libs']
            }
        }
    }
    //...
}

// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
    commandLine '/.../android-ndk-r10e/ndk-build', '-C', file('src/main').absolutePath
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

Note that version 0.3.0-alpha7 of the gradle-experimental plugin is out.




回答2:


Add this to your build.gradle file. This will cause the ndk-build to run as part of project build using the specified .mk file.

android{
     externalNativeBuild {
         ndkBuild {
             path 'src/main/jni/Android.mk'
         }
     }
}



回答3:


In addition to the previous response: With Experimental Plugin version 0.7.0-alpha1 this works on Windows

model {

    // ...

    android.sources.main {
        jni {
            source {
                srcDirs = ['src/main/none']
            }
        }
        jniLibs {
            source {
                srcDirs = ['src/main/libs']
            }
        }
    }

    // ...

}

task ndkBuild(type: Exec) {
    def cmdline = "${System.env.ANDROID_NDK_HOME}/ndk-build -C \"" + file('src/main').absolutePath + "\" > ndk-build-log.txt 2>&1"
    commandLine 'cmd', '/c', cmdline
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}


来源:https://stackoverflow.com/questions/32970094/how-to-use-custom-android-mk-with-new-gradle-build-system

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