jean
11/16/2023, 9:00 PMbuild.gradle.kts
file inside a module (called logic
)?
This code returns null
for both tasks even though I can find them in the GUI of android studio
val compileDebugKotlinAndroidTask = tasks.findByPath(":logic:compileDebugKotlinAndroid")
val kspCommonMainKotlinMetadataTask = tasks.findByPath(":logic:kspCommonMainKotlinMetadata")
println("compileDebugKotlinAndroidTask exists: $compileDebugKotlinAndroidTask")
println("kspCommonMainKotlinMetadataTask exists: $kspCommonMainKotlinMetadataTask")
compileDebugKotlinAndroidTask?.dependsOn(kspCommonMainKotlinMetadataTask)
...
compileDebugKotlinAndroidTask exists: null
kspCommonMainKotlinMetadataTask exists: null
Vampire
11/16/2023, 9:07 PMVampire
11/16/2023, 9:08 PMAdam S
11/17/2023, 12:39 AMlogic
subproject isn't configured before the code you've written, because Gradle doesn't know about the inter-project dependency. Android Studio fetches the list of all tasks after all projects have been evaluated.
There are hacks to try and quick-fix your problem, like putting your code in afterEvaluate {}
, but that always causes more problems down the line.
Can you say more about what you're trying to achieve? There's probably a better way.jean
11/17/2023, 5:20 AM* What went wrong:
A problem was found with the configuration of task ':logic:compileDebugKotlinAndroid' (type 'KotlinCompile').
- Gradle detected a problem with the following location: '.../logic/build/generated/ksp/metadata/commonMain/kotlin'.
Reason: Task ':logic:compileDebugKotlinAndroid' uses this output of task ':logic:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
Possible solutions:
1. Declare task ':logic:kspCommonMainKotlinMetadata' as an input of ':logic:compileDebugKotlinAndroid'.
2. Declare an explicit dependency on ':logic:kspCommonMainKotlinMetadata' from ':logic:compileDebugKotlinAndroid' using Task#dependsOn.
3. Declare an explicit dependency on ':logic:kspCommonMainKotlinMetadata' from ':logic:compileDebugKotlinAndroid' using Task#mustRunAfter.
For more information, please refer to <https://docs.gradle.org/8.2/userguide/validation_problems.html#implicit_dependency> in the Gradle documentation.
I did have this before creating the logic
module and it was working fine
tasks.withType<KotlinCompile>().configureEach {
dependsOn(tasks.named(":logic:kspCommonMainKotlinMetadata"))
}
But the error came after I moved the existing gradle file to the moduleAdam S
11/17/2023, 5:41 AMdependencies {
implementation(project(":logic"))
}
jean
11/17/2023, 6:24 AMlogic
uses KSP, I'm not sure how cross project dependencies would help in that case? Since the thread was tagged as "not kotlin" I'll ask on SO insteadVampire
11/17/2023, 8:06 AMfindByPath
is a bad idea, as you break task configuration avoidance.
And in your case, it also probably is simply to early. I'm not an AGP expert, but I guess it uses the bad practice afterEvaluate
to register trust tasks and so they are simply not present yet when you try to find them.
If you would use something like tasks.withType<KotlinCompile>().matching { it.name == "compileDebugKotlinAndroid" }.configureEach { dependsOn("kspCommonMainKotlinMetadata") }
, it would probably work and also "fix" that error. But that is just symptom treatment and the advice is bulls***. And explicit dependsOn
is a code smell unless a lifecycle task is on the left-hand side. You should instead find out the actual cause for that error and eradicate that.jean
11/17/2023, 9:19 AMdependencies {
add("kspCommonMainMetadata", "com.jeantuffier.statemachine:processor:$stateMachineVersion")
}
I get the following error :
A problem was found with the configuration of task ':logic:compileDebugKotlinAndroid' (type 'KotlinCompile').
- Gradle detected a problem with the following location: '/Users/jeantuffier/Repos/Entur/Tavla/CommonTavla/logic/build/generated/ksp/metadata/commonMain/kotlin'.
Reason: Task ':logic:compileDebugKotlinAndroid' uses this output of task ':logic:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
Possible solutions:
1. Declare task ':logic:kspCommonMainKotlinMetadata' as an input of ':logic:compileDebugKotlinAndroid'.
2. Declare an explicit dependency on ':logic:kspCommonMainKotlinMetadata' from ':logic:compileDebugKotlinAndroid' using Task#dependsOn.
3. Declare an explicit dependency on ':logic:kspCommonMainKotlinMetadata' from ':logic:compileDebugKotlinAndroid' using Task#mustRunAfter.
I did try with tasks.named
instead of findByPath
but it did not work neitherjean
11/17/2023, 9:33 AMVampire
11/17/2023, 9:44 AMAlso the link to join the gradle slack on this page isn’t working anymore"The link" is a bit broad, as there are multiple links. The link in the original post was only temporary. Further down you find the permanent link that is also on gradle.org/help and also in the subject of this channel. That link is currently working fine.
Vampire
11/17/2023, 9:44 AMI did try withYeah,instead oftasks.named
but it did not work neitherfindByPath
named
if not queried properly leverages task-configuration avoidance, but still it only works for tasks that are already registered at the time you call it. That's why you would need a construct with manual name-matching that I showed above.jean
11/17/2023, 9:45 AMVampire
11/17/2023, 9:45 AMVampire
11/17/2023, 9:46 AM