Wait for Extension configuration before tasks are run

I’m developing a custom plugin that needs some configuration. Projects provision this configuration through an extension, which is convenient

// app/build.gradle.kts

myPlugin {
    defaultOutputDir(layout.buildDirectory.get().dir("generated/source/ble/"))

    // more config
   
    onConfigurationFinished()
}

I had to add the line onConfigurationFinished() or the tasks would some times see an extension missing some fields, but I’m certain there must be a better way to do this.

This topic is a very similar question, but it dates back to 2018. Has something new been introduced since then?

Thanks in advance!

If you register the extension and then immediately read the values, the project did not have time to set it.
You should make the properties of the extension lazy types like Property, RegularFileProperty, DirectoryProperty, ConfigurableFileCollection, and so on, the same in the task, then in the plugin’s apply method you wire the extension properties to the task properties, and at task execution time you then query the value of those properties.

Oh, it’s that simple! :man_facepalming:

Thank you very much

1 Like