Search code examples
android-ndkandroid-gradle-pluginndk-build

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


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

Solution

  • 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.