[英]Android Build with Kotlin DSL - how to access flavor extra properties?
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.传统上,在 Groovy 中,可以在
ext {}
块中定义特定于风格的变量,但是切换到 Kotlin DSL 似乎extra
地图具有项目范围。
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 ) (来源: 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:但是如果以后需要使用它,例如根据
buildType
调整变量,如下所示:
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?如何使用这些对
extra
风味范围的引用?
Currently, only one working method to access ext properties on ProductFlavor is this way目前,只有一种访问 ProductFlavor 上的 ext 属性的工作方法是这种方式
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
当您调用不存在的属性
ext
时,它所做的事情与 Groovy 所做的事情相同
I created a feature request for making it easier https://issuetracker.google.com/issues/161115878我创建了一个功能请求以使其更容易https://issuetracker.google.com/issues/161115878
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.