This message was deleted.
# community-support
s
This message was deleted.
v
Most probably a classloading problem. The
KotlinCompile
class you search for in the init script is most probably not coming from the same class loader that is used to register the tasks in question. So as the classes come from different class loaders, they are technically different classes and you get no matches. You can easily verify by printing out the class loaders from the init script and from the task where it works. You might need to instead need to do something like
Copy code
tasks.configureEach {
    if (it.class.name == "org.jetbrains.kotlin.gradle.tasks.KotlinCompile") {
        ...
    }
}
or something similar. Btw. do not do
tasks.withType(...) { ... }
, that totally prevents task-configuration avoidance. Always do
tasks.withType(...).configureEach { ... }
.
j
Thanks @Vampire for once again helping me out. I tried printing all of the
tasks
out, from both a
gradle
task
and an
init script
(as shown above). And both of them printed a quite a different set of tasks. You can see the difference here https://www.diffchecker.com/LA7U9InU/ Am I’m missing something about the lifecycle of
Gradle
here?
Hey, @Vampire. So turns out I had to use
tasks.all{}
to gain access to all of the tasks. Accessing tasks like
def tasks = tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile)
(like I have mentioned above) might not return all of the tasks. Again, I think this has to do something with the lifecycle of
Gradle
(which I’m not completely sure about), but
tasks.all{}
seems to do the trick for me. And thanks for the hint about comparing tasks with their class name, it is working perfectly.
v
Do not use
tasks.all
though, except for trying things out / debugging. It completely circumvents task configuration avoidance, causing each and every task to be realized and configured eagerly and thus wasting your time.
j
Thanks for the heads up @Vampire, could you please suggest an alternative way of achieving the same behaviour. This is how my script looks so far:
Copy code
allprojects {
    afterEvaluate {
        tasks.all {
            if (it.class.name.contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile")) {
                kotlinOptions {
                    freeCompilerArgs += ["-P", "...."]
                }
            }
        }
    }
}
v
Well, what I told you.
tasks.configureEach
, not
tasks.all
There is no point in configuring tasks that are not realized anyway as they are not going to be executed.
j
Got it @Vampire, thanks for the clarification.
👌 1