问题描述
我的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
}
}
}
}
但是我的任务只运行一次。 救命
1楼
最后,我解决了动态任务
String newTaskName = "${targetName}m";
task "$newTaskName"() << {
...
}
def newTask = project.tasks.findByName("$newTaskName")
newTask.dependsOn project.tasks.findByName(preTaskName)
project.tasks.findByName(nextTaskName).dependsOn newTaskName