How can I get a hold on gradle tasks from a `build...
# gradle
j
How can I get a hold on gradle tasks from a
build.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
Copy code
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
v
Don't even try to do that. You just earn a s***load of problems even if you make it seem to work. What do you try to achieve?
Besides that this is more a general Gradle question and thus off-topic here
a
Gradle runs in phases. Code in the top level of a build script runs in the configuration phase. When there are multiple subprojects, each one is configured in order. Presumably your
logic
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.
j
I’m getting this error after I went from a single module to two:
Copy code
* 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
Copy code
tasks.withType<KotlinCompile>().configureEach {
    dependsOn(tasks.named(":logic:kspCommonMainKotlinMetadata"))
}
But the error came after I moved the existing gradle file to the module
a
Instead of manually trying to set dependencies using tasks, have you tried using cross project dependencies? https://docs.gradle.org/current/userguide/declaring_dependencies_between_subprojects.html Something like:
Copy code
dependencies {
    implementation(project(":logic"))
}
j
Only the module
logic
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 instead
v
As it's about two Kotlin tasks, it is more Lotion again. :-D And you could also come to the Gradle slack as mentioned in the subject of this channel. Cross project dependencies will not help here. Adam probably was just confused because you try to get the tasks by full path, which is usually just needed if you do bad practice cross-project configuration and there a project dependency often might be part of the solution. In your case, after reading again properly what your wrote, it is different. Generally,
findByPath
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.
j
I’m trying to configure ksp into a multiplatform project. When I add the following
Copy code
dependencies {
    add("kspCommonMainMetadata", "com.jeantuffier.statemachine:processor:$stateMachineVersion")
}
I get the following error :
Copy code
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 neither
Also the link to join the gradle slack on this page isn’t working anymore (I see you had to fix it multiple times, probably need to do it again)
v
Also 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.
I did try with
tasks.named
instead of
findByPath
but it did not work neither
Yeah,
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.
j
Yes, I just tried now and it worked. Thanks for the help!
v
But as this really is about how to properly use / configure the Kotlin Gradle Plugin, asking here is fine, just was not clear in the beginning, sorry.
I cannot help you, as I don't use KMP nor KSP. But maybe someone else that is familiar with those can help here how to properly set it up.
186 Views