Hi! I’m creating an Android plugin and got stuck at the following:
I want to set some properties from a DSL, and use these properties to set the Android version on my app.
Following some tutorials, I was able to make the DSL and the setting Android version part work independently, but I can’t combine them.
From my understanding, I can only access my DSL properties after evaluation (since they work if I access them inside a task, but they are null if I try to use them to set the version directly, at configuration time).
The common solution I found online is to access these properties inside an afterEvaluate block (and it works), but if I try setting the Android app version inside it I receive this error:
“It is too late to set versionCode. It has already been read to configure this project. Consider either moving this call to be during evaluation, or using the variant API.”
The Android version is set inside of the defaultConfig in the ApplicationExtension:
extensions.configure<ApplicationExtension> {
defaultConfig {
versionCode = extension.customCode.get()
versionName = extension.customName.get()
}
}
From my understanding, the defaulConfig versionCode and versionName are extension properties as well, so I believe I should be able to access my own extension when setting them. Is there a way I can set these properties at a lifecycle where I can both access my own properties and not receive the “It is too late” error?
If not, is there a good resource where I can understand more about the lifecycle of each step of the Gradle configuration?
Any help is appreciated!