Slackbot
06/12/2023, 10:27 AMVampire
06/12/2023, 10:44 AMVlastimil Brecka
06/12/2023, 10:47 AMVlastimil Brecka
06/12/2023, 10:48 AMtest
tasks for jvm and testDebugUnitTest
for android
but, it feels rather low level, and wondering if there is more idiomatic approachNiels Doucet
06/12/2023, 10:51 AMcheck
lifecycle task is highly specific to the android plugin, no? Gradle just provides the check
task as a hook, nothing more, nothing less. So even if you consider this a generic need, the solution to your specific build is very much android-specific.Vlastimil Brecka
06/12/2023, 10:52 AMVlastimil Brecka
06/12/2023, 10:54 AMNiels Doucet
06/12/2023, 10:57 AMbeforeVariant
callback can help you select which variants to build under what circumstances (this is a guess on my end though): https://developer.android.com/reference/tools/gradle-api/7.1/com/android/build/api/extension/AndroidComponentsExtension#beforevariant%0AsVlastimil Brecka
06/12/2023, 10:58 AMNiels Doucet
06/12/2023, 11:02 AMprojects.all {
tasks.named("test").configureEach {}
tasks.withType(Test).configureEach {}
}
Vampire
06/12/2023, 11:05 AMwhat I think I want is, at a root build.gradle, iterate all projects, collect all tasks which name is either "test" or "testDebugUnitTest" .. that's allYou could simply do
./gradlew test testDebugUnitTest
Vlastimil Brecka
06/12/2023, 11:32 AMVampire
06/12/2023, 11:35 AMVlastimil Brecka
06/12/2023, 11:48 AMtest
has the same issue as check on android, it runs every variantVampire
06/12/2023, 11:52 AMwhat I think I want is, at a root build.gradle, iterate all projects, collect all tasks which name is either "test" or "testDebugUnitTest" .. that's allwouldn't help either as that is exactly what that commandline is triggering. If for whatever reason
test
is configured to depend on those other variants - which sounds like a bad idea - then triggering test
will trigger them, unless you prevent it for example by changing the dependency configuration, disabling the unintended tasks, or excluding them from the commandline using -x
.Vlastimil Brecka
06/12/2023, 11:54 AM:app
and :base
.. app is android application, base is a kotlin jvm module
essentially what I want is
tasks.register("testDebug") {
dependsOn ":app:testDebugUnitTest", ":base:test"
}
Vlastimil Brecka
06/12/2023, 11:55 AMVlastimil Brecka
06/12/2023, 11:57 AM./gradlew :app:testDebugUnitTest :base:test
this does what I want
and is what I'd expect to be already provided but whateverVlastimil Brecka
06/12/2023, 12:04 PMtestDebug
already, although I don't see it in gradlew tasks
but it seems to be just running the app module tests, excluding the base testsVlastimil Brecka
06/12/2023, 12:06 PMtasks.register("testDebug") {
List<Task> testTasks = []
allprojects { project ->
def task = project.tasks.findByName("test")
if (task != null) { testTasks += task }
}
testTasks.each { println it.path }
dependsOn testTasks
}
does this seem idiomatic way to find a certain task over all modules?Vlastimil Brecka
06/12/2023, 12:11 PMVampire
06/12/2023, 12:21 PMalso, there seems to beIt might not have a group assigned, e.g. because it is not intended for manual triggering. Look atalready, although I don't see it intestDebug
gradlew tasks
gw tasks --all
to see all tasks.
does this seem idiomatic way to find a certain task over all modules?Practically all usage of
allprojects
or subprojects
is non-idiomatic.
And it will probably also not work properly.
It would require the other projects to be configured first or you will not find the tasks in question.
Besides that your allprojects { ... }
iirc will be a delayed action so directly using the "result" of the closure will also not work.
And even if it would work, it would not do what you want, because it would produce the same result as calling gw test
and you said that this is not what you want to do.Vlastimil Brecka
06/12/2023, 12:23 PMtasks.register("testDebug") {
List<Task> testTasks = []
allprojects { project ->
def androidTestTask = project.tasks.findByName("testDebugUnitTest")
if (androidTestTask != null) {
testTasks += androidTestTask
} else {
def testTask = project.tasks.findByName("test")
if (testTask != null) {
testTasks += testTask
}
}
}
testTasks.each { println it.path }
dependsOn testTasks
}
now if testDebugUnitTest is available, then it takes it over the "test" which would do all variants yeaVlastimil Brecka
06/12/2023, 12:24 PMVlastimil Brecka
06/12/2023, 12:25 PMallProjects
? should I rather look for a "subgraph"? i.e. app+all it's dependencies?Niels Doucet
06/12/2023, 12:28 PMVampire
06/12/2023, 12:28 PM--parallel
or configuration cache, ...Vlastimil Brecka
06/12/2023, 12:29 PMThe following practices should be avoided:
Explicitly depending on a task from another project via Task.dependsOn(java.lang.Object...).
so I read that as exactly this
tasks.register("testDebug") {
dependsOn ":app:testDebugUnitTest", ":base:test"
}
...huh really? isn't that just a plain composition?Vampire
06/12/2023, 12:34 PMtestDebug
task in all projects that depends on test
in non-android projects and on testDebugUnitTest
in android projects. Then you could safely run gw testDebug
and get the tests run you intend. This configuration is optimally done in a "my.android" convention plugin and in a "my.java" convention plugin that you apply to the according projects. 🙂