https://kotlinlang.org logo
Title
c

Charlie Tapping

04/19/2023, 4:29 PM
I need to run a copy task everytime the task kspKotlin successfully passes, I have the following code which seems to work okay:
afterEvaluate {
    tasks.named("kspKotlin") {
        finalizedBy(copyFeatures)
    }
}
I read somewhere whilst learning that finalizedBy happens irrespective of kspKotlin being successful or not, is there an alternative that works only on success? Also is there anyway to avoid the afterEvaluate because I can’t seem to find that task without that block?
a

Adam S

04/19/2023, 4:51 PM
what you might want is instead to hook into a ‘lifecycle task’ https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:lifecycle_tasks so instead of making
kspKotlin
be finaized by
copyFeatures
, instead 1. make
copyFeatures { dependOn(tasks.kspKotlin) }
2. make a lifecycle task, like
assemble
, depend on
copyFeatures
That way every time
assemble
runs, it will run
kspKotlin
and
copyFeatures
in the right order. But… what does
copyFeatures
do? Does it copy generated source code into a src dir?
c

Charlie Tapping

04/19/2023, 4:55 PM
Copy features is essentially this:
val copyFeatures by tasks.registering(Copy::class) {
    from(file("$rootDir/xxx/build/generated/ksp/main/resources/features-gen.yml"))
    rename { "features.yml" }
    into(file("$rootDir/.xxx/"))
}
One of my KSP outputs is a yaml file
a

Adam S

04/19/2023, 4:56 PM
Yeah okay, that makes sense. What happens to the yaml file get used for afterwards? Are there any other tasks that require it exists? I ask because it’s possible to convert the
copyFeatures
task into a ‘file provider’ and use it as a srcDir in a source set, or something, and that makes sorting out task ordering easier
c

Charlie Tapping

04/19/2023, 4:57 PM
The yaml file is actually picked up by a process in CI, its entirely unrelated to anything else
a

Adam S

04/19/2023, 4:57 PM
gotcha
c

Charlie Tapping

04/19/2023, 5:12 PM
So this is okay?
tasks.named(LifecycleBasePlugin.BUILD_TASK_NAME) {
    dependsOn(copyFeatures)
}
I actually need it to run on build as opposed to assemble
a

Adam S

04/19/2023, 5:13 PM
yes that makes sense 👍
c

Charlie Tapping

04/19/2023, 5:13 PM
Thank you Adam !
a

Adam S

04/19/2023, 5:13 PM
you’re welcome!
v

Vampire

04/19/2023, 5:20 PM
You can also have kspKotlin finalized by copyFeatures and copyFeatures dependsOn kspKotlin. Then it is always run after successful kspKotlin execution if that really is what you want.
c

Charlie Tapping

04/19/2023, 5:32 PM
Oddly the lifecycle task block never seems to trigger the copyFeatures task
I tried assemble also
neither seem to be triggered when building my android application
v

Vampire

04/19/2023, 5:39 PM
Run with
-m
and you will see which tasks would get executed.
c

Charlie Tapping

04/19/2023, 6:02 PM
Yeah the copy task doesnt ever seem to get registered, I must be doing something silly
The finalized approach I posted originally works but I was trying to avoid the after evaluate
v

Vampire

04/19/2023, 6:04 PM
Then find out why the task is not present already. Is the plugin not yet registered? Does it itself use
afterEvaluate
to register the task? ...? Many possibilites.
Worst case:
tasks.configureEach {
    if (name == "kspKotlin") {
        finalizedBy(copyFeatures)
    }
}
c

Charlie Tapping

04/19/2023, 6:07 PM
I wonder if its because my android application has variants, so assemble is in fact assembleStaging? Technically they’re different tasks?
v

Vampire

04/19/2023, 6:14 PM
Of course they are different tasks. But I cannot reason about Android, I don't do Android development.