Reputation: 2085
Traditionally, in Groovy, it was possible to define flavor-specific variables in the ext
{}
block, but switching to Kotlin DSL it seems that the extra
map has the project scope.
It looks like it is possible to force a flavor scope for extras by using:
productFlavors {
register("flavor1") {
require(this is ExtensionAware)
...
}
register("flavor2") {
require(this is ExtensionAware)
...
}
}
(source: https://github.com/gradle/kotlin-dsl-samples/issues/1254)
But if it needs to be used later, for example to tweak the variables based on buildType
like this:
variants.forEach { variant ->
val flavor = variant.productFlavors[0]
val size = ?? // extra??
variant.buildConfigField("String", "SIZE", size)
}
how would these flavor-scoped references to extra
be used?
Upvotes: 5
Views: 2202
Reputation: 16200
Currently, only one working method to access ext properties on ProductFlavor is this way
variant.productFlavors.forEach { flavor ->
require(flavor is ReadOnlyProductFlavor)
val properties = (flavor.getProperty("ext") as DefaultExtraPropertiesExtension).properties
}
Or maybe more clear
val flavor = variant.productFlavors[0] as ReadOnlyProductFlavor
val extra = flavor.getProperty("ext") as DefaultExtraPropertiesExtension
extra.get("myPropertyName")
It just does the same thing what Groovy does when you call the non-existing property ext
I created a feature request for making it easier https://issuetracker.google.com/issues/161115878
Upvotes: 5