当前位置: 代码迷 >> Android >> 修改gradle中的所有flavor .class
  详细解决方案

修改gradle中的所有flavor .class

热度:98   发布时间:2023-08-04 10:46:25.0

我的app.gradle有很多口味。 当我执行gradlew assembleRelease我可以在dir app/build/intermediates/classes找到所有flavors 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
            }
        }
    }
}

但是我的任务只运行一次。 救命

最后,我解决了动态任务

String newTaskName = "${targetName}m";
task "$newTaskName"() << {
    ...
}
def newTask = project.tasks.findByName("$newTaskName")
newTask.dependsOn project.tasks.findByName(preTaskName)
project.tasks.findByName(nextTaskName).dependsOn newTaskName
  相关解决方案