I'm attempting to add some configuration to all Ko...
# gradle
r
I'm attempting to add some configuration to all Kotlin source sets based on my Plugin's extension (to add a dependency, or not based on the extension property). This seems pretty straightforward, just use
KotlinBaseExtension.sourceSets.configureEach { ... }
. However, the source sets appear to be configured/resolved before the build script is ran, meaning setting properties in my extension isn't reflected in the configuration since the added configuration runs first. Is this known or expected behaviour? I can probably slap an
afterEvaluate
on it but I'd prefer to avoid that if possible since it's a whole can of worms.
I found a workaround by moving the provider into the dependency declaration itself (i.e.
implementation(myProperty.map{ ifIit) REAL_DEPENDENCY else null })
but this still seems pretty ugly and I'm surprised it's necessary
v
Yes, fully expected, not at all surprising or ugly, and using a provider is exactly the right thing to do. 🙂
afterEvaluates
is almost never the way to go. The main benefits of using it are timing problems, ordering problems, and race conditions as you seem to know. 🙂 If you would not have been able to use a
Provider
to add the dependency, for example because you wanted to register a task based on the property, there would be other tactics, like always registering the task but skipping it in
onlyIf { ... }
checking the property, or not using a property in the extension, but instead have a function which your user calls and which then uses the argument and does the necessary logic in its body.