Reputation: 338
I want to create a custom gradle task for an Android project, that will call chain of other tasks depends on build flavor and build configuration.
So, that what I do in my build.gradle.kts (we use kotlin script)
gradle.projectsEvaluated {
rootProject.allprojects.filter { project ->
!Config.CodeQuality.ignoredProjects.contains(project.name)
}.forEach { project ->
project.tasks.filter { task ->
task.name.startsWith("lint")
}.forEach { task ->
val taskSyfix = task.name.drop(4)
val taskName = "codeQuality$taskSyfix"
println("qualityscripts create task $taskName")
project.tasks.create(taskName) {
group = "verification"
dependsOn("checkstyle", "deteltCkeck", "ktlint", task.name)
}
}
}
}
So than if I run ./gradlew tasks
...
Verification tasks
------------------
check - Runs all checks. checkstyle - Runs checkstyle.
codeQuality
codeQualityDebug
codeQualityDevMenuFlavor1Debug
codeQualityDevMenuFlavor1Release
codeQualityDevMenuFlavor2Debug
codeQualityDevMenuFlavor2Release
...
So they exist. But then, if I try to run any, I got next error:
- What went wrong: Task 'codeQualityDevMenuFlavor1Debug' not found in root project 'myproject-android'.
Upvotes: 1
Views: 3639
Reputation: 12086
I could reproduce your problem and I think it's connected to the Configuration on demand execution mode, which seems to be enabled in your environment. Could you try to disable this mode and retest? ( in Android Studio: File > Settings > Build, Execution, Deployment > Compiler
: uncheck the "configure on demand" option).
It looks like when this mode is enabled, and when you try to execute a task which has been dynamically created (e.g.: your codeQuality
* tasks): Gradle will not execute the gradle.projectsEvaluated {}
block : so in your case your custom tasks will not be created , causing the error Task 'codeQualityDevMenuFlavor1Debug' not found
See Configuration on demand and also this answer for more information about the Configuration on demand feature.
There are also some known issues with Android Studio and this mode, see : Configuration on demand is not supported by the current version of the Android Gradle plugin
Upvotes: 3