I'm migrating a big ol groovy android build file t...
# community-support
c
I'm migrating a big ol groovy android build file to kts. I'm on my last issue! We do something like
Copy code
productFlavors {
   create("dev"){
   `ext.envApiKey = "123".
  }
  create("staging"){
   `ext.envApiKey = "456".
  }
... repeat 3 more times since we have 5 build environments total
}
then we have another place in the build file where we iterate over each
productFlavor
and
register
a new task, and try to access that
ext.envApiKey
but ext doesn't exist in kts. chatGpt keeps saying that I should be able to do flavor.extra when iterating over flavors but no dice. is there some way to add like an extra property or something to a
ProductFlavor
so I can retrieve it elsewhere? its in the same build file if that helps.
s
Probably something like
(this as ExtensionAware).extra["envApiKey"]
v
Each instance created through Gradle means is automatically
ExtensionAware
, so it is best to always declare it explicitly e. g. if you define an extension class or interface and similar, so that you do not need to cast it to
ExtensionAware
.
ProductFlavor
according to https://developer.android.com/reference/tools/gradle-api/8.3/null/com/android/build/api/dsl/ProductFlavor does declare it explicitly, so a cast should not be necessary. So you could for example do
val envApiKey by theFlavor.extra
. But actually, practically any usage of
ext
/
extra
is a smell and usually a work-around for not doing something properly. But at least it should work.
e
AGP has extension points. for example, if you wrote a convention plugin like this,
then you could write your build script like
of course you could cut out the bits you don't use, or do it all directly in the build script if it doesn't need to be reused
c
v
What you linked says
This is an interface for the gradle tooling api, and should only be used from Android Studio. It is not part of the DSL & API interfaces of the Android Gradle Plugin.
so if you work with that in your build logic, you are probably anyway wrong. Or rather, you probably did not set the
ext
on the product flavor, but on an outer scope, just irritating the reader by having it in the product flavor configuration. 🤷‍♂️
c
Got it. I think I'm going to try to delete this task anyway lol and do this some other way. it's like weirdly grabbing a variable, just to put it into a task and then that task puts it in a file. idk. weird setup lol
👌 1