简体   繁体   English

Android Build with Kotlin DSL - 如何访问风味额外属性?

[英]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.

相关问题 通过 Kotlin DSL 的 Android 特定风味依赖 - Android specific flavor dependency via Kotlin DSL 使用 Gradle Kotlin-DSL 时,如何使用 flavorDimensions 为每个风味组合设置不同的 applicationId? - How to set different applicationId for each flavor combination using flavorDimensions when using Gradle Kotlin-DSL? 如何在模块中访问项目 build.gradle 的额外属性 - How to access extra properties from project build.gradle in modules 如何在 Android Studio Bumblebee 中将 build.gradle 从 Groovy 迁移到 Kotlin DSL? - How to Migrate build.gradle from Groovy to Kotlin DSL in Android Studio Bumblebee? 未解决的参考:android build failed with kotlin dsl(可在空项目中重现) - unresolved reference: android build failed with kotlin dsl (reproducible in an empty project) Gradle Kotlin DSL:在唯一位置定义 Android 构建工具版本 - Gradle Kotlin DSL: Define Android build tools version in unique place 如何打造味道? - How to build flavor? 如何创建Kotlin DSL - DSL语法Kotlin - How to create Kotlin DSL - DSL syntax Kotlin Android构建风味sqlite数据库 - Android build flavor sqlite database Gradle-如何为每种口味指定不同的Android * .apk构建位置? - Gradle - how to specify different Android *.apk build location for each flavor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM