How to use custom Android.mk with new gradle build

2019-02-19 13:27发布

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

3条回答
可以哭但决不认输i
2楼-- · 2019-02-19 13:54

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.

查看更多
唯我独甜
3楼-- · 2019-02-19 13:58

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
}
查看更多
Animai°情兽
4楼-- · 2019-02-19 14:06

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'
         }
     }
}
查看更多
登录 后发表回答