在gradle中修改所有flavor .class

时间:2017-02-13 05:51:41

标签: android gradle

我的app.gradle中有很多种口味。当我执行gradlew assembleRelease时,我可以在dir app/build/intermediates/classes中找到所有风味类。现在,我希望在类生成后修改类(执行我的任务)。我怎么能这样做?

我的任务名称是m。

gradle.projectsEvaluated {
    def buildTypes = android.buildTypes.collect { type -> type.name }
    def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
    // When no product flavors defined, use empty
    if (!productFlavors) {
        productFlavors.add('')
    }
    productFlavors.each { productFlavorName ->
        buildTypes.each { buildTypeName ->
            def flavorNameCapitalized = "${productFlavorName.capitalize()}"
            def buildNameCapitalized = "${buildTypeName.capitalize()}"
            def targetName = "${flavorNameCapitalized}${buildNameCapitalized}"
            def preTaskName = "compile${targetName}JavaWithJavac"
            def nextTaskName = "compile${targetName}Ndk"
            if (!isDebug(buildNameCapitalized.toString())) {
                    project.tasks.findByName("generate${targetName}Sources").doLast { //generate${targetName}Sources
                    println("projectsEvaluated flavorName: " + flavorNameCapitalized.toString())
                    println("projectsEvaluated buildName: " + buildNameCapitalized.toString())
                    project.ext.set("flavorName", flavorNameCapitalized.toString());
                    project.ext.set("buildName", buildNameCapitalized.toString());
                }
                m.dependsOn project.tasks.findByName(preTaskName)
                project.tasks.findByName(nextTaskName).dependsOn m
            }
        }
    }
}

但我的任务只运行一次。帮助

1 个答案:

答案 0 :(得分:0)

最后,我用动态任务解决了

String newTaskName = "${targetName}m";
task "$newTaskName"() << {
    ...
}
def newTask = project.tasks.findByName("$newTaskName")
newTask.dependsOn project.tasks.findByName(preTaskName)
project.tasks.findByName(nextTaskName).dependsOn newTaskName