我创建了自定义任务,该任务应该处理gradle dependencies
任务的输出(完全./gradlew dependencies --configuration myFlavorDebugRuntimeClasspath
)。我想为每个buildVariant(PassportGenerateTask passportGen = project.tasks.create("pasportGenerate${variantName}", PassportGenerateTask)
)创建自定义任务。
但我无法为每个passportGen定制dependenciesReportTask属性。当我使用passportGen来调用buildVariant时,它使用所有(project.android.applicationVariants.all)中最后一个构建变体的dependenciesReportTask.setProperty(“configurations”,...)。
对于testlpe,如果我有以下构建变体(googleDebug googleRelease samsungDebug samsungRelease)并调用passportGen任务(./gradlew pasportGenerateGoogleDebug
),它将使用错误属性的depenancesReportTask(配置将是samsungReleaseRuntimeClasspath配置)
class AppPlugin implements Plugin<Project> {
void apply(Project project) {
project.afterEvaluate {
// Create tasks for each Build Variant
project.android.applicationVariants.all { ApplicationVariant variant ->
def variantName = variant.name.capitalize()
def variantOutput = variant.outputs.first()
//Generating configuration name for dependency report
def configurationName = ""
if (variant.productFlavors.size() > 0) {
configurationName += variant.productFlavors.get(0).name
configurationName += variant.getBuildType().name.capitalize()
} else {
configurationName += variant.getBuildType().name
}
configurationName += "RuntimeClasspath"
def configurations = project.configurations.collect()
configurations.removeAll {
!it.name.equals(configurationName)
}
//prepare file for output of "dependencies" tasks
def depReportFileName = "dependeciesReport${variantName}.txt"
def dependenciesReportOutputFile = new File(depReportFileName)
//Get "dependencies" task from all project tasks
def dependenciesReportTask = project.tasks["dependencies"]
dependenciesReportTask.setProperty("configurations", configurations)
dependenciesReportTask.setProperty("outputFile", dependenciesReportOutputFile)
//create cutom task for every buildVariant which depends on "dependencies" task
PassportGenerateTask passportGen = project.tasks.create("pasportGenerate${variantName}", PassportGenerateTask)
passportGen.variant = variant
passportGen.configuration = configurations.collect().get(0)
//add dependency on "dependencies"
passportGen.dependsOn dependenciesReportTask
}
}
}
}
我能达到我想要的唯一方法就是打电话:
def dependenciesReportTask = project.tasks["dependencies"]
dependenciesReportTask.setProperty("configurations", configurations)
dependenciesReportTask.setProperty("outputFile", dependenciesReport)
dependenciesReportTask.execute()
在我的自定义PassportGenerateTask任务主操作方法(@TaskAction)中,但在其他任务中调用execute是不推荐使用的功能,将在gradle 5.0中删除
答案 0 :(得分:2)
它不仅被弃用,它纯粹是内部实现细节,很可能不做你期望它做的事情,并且几乎总是产生比它试图解决并且实际上失败的问题更多的问题。 / p>
你很可能也不应该强奸dependencies
任务。如果您需要使用不同配置的多个此类任务,只需创建DependencyReportTask
类型的新任务,然后根据需要配置它们。