gpt4 book ai didi

android - 如何在 Kotlin Gradle DSL 中更新 applicationVariants 中的 manifestPlaceholders?

转载 作者:行者123 更新时间:2023-12-04 00:21:58 24 4
gpt4 key购买 nike

我尝试将其转换为 Kotlin:

applicationVariants.all { variant ->
def flavor = variant.productFlavors[0]
def mergedFlavor = variant.getMergedFlavor()
mergedFlavor.manifestPlaceholders = [applicationLabel: "${variant.buildType.appName[flavor.name]}"]
}

但是 manifestPlaceholder 是一个 val 并且不能重新分配,所以这会导致错误:
applicationVariants.forEach {variant->
val flavor = variant.productFlavors[0]
val mergedFlavor = variant.mergedFlavor
variant.mergedFlavor.manifestPlaceholders = mapOf("applicationLabel" to "${variant.buildType.appName[flavor.name]}")
}

通常我可以在 buildTypes 闭包中设置它,但我不能在这里设置,因为 appName 是 buildTypes 中的映射,其中键是 flavor 名称,因此 applicationLabel 取决于构建类型和 flavor 。而且我认为您无法访问 buildTypes 中的风格,这就是您需要 applicationVariants 的原因。

最佳答案

我必须更改上面的一些内容才能使其正常工作:

  • flavor 映射(此处为 appLabelMap)必须进入 applicationVariants,因此您可以立即使用它。
  • ManifestPlaceHolders 确实是一个 val,但您可以替换 map 中的值。 :)
  • applicationVariants.forEach 不会被执行,所以你必须使用 applicationVariants.all。但请注意,它与 koltin.collections.all() 发生冲突(有点),因此您必须使用执行 Action 的闭包而不是闭包。

  • 这是最终结果:
    applicationVariants.all {
    val appLabelMap = when (this.buildType.name) {
    "debug" -> mapOf("flavorA" to "FlavorA Debug", "flavorB" to "FlavorB Debug")
    ...
    else -> mapOf("flavorA" to "FlavorA", "flavorB" to "FlavorB")
    }
    val flavor = this.productFlavors[0]
    this.mergedFlavor.manifestPlaceholders["applicationLabel"] = "${appLabelMap[flavor.name]}"
    }

    您还必须在 android.defaultConfig 中为 applicationLabel 设置默认值:
    android.defaultConfig { manifestPlaceholders["applicationLabel"] = "FlavorA"}

    这是 AndroidManifest.xml 的相关部分,以防万一不清楚:
    <application
    android:label="${applicationLabel}"
    ...
    <activity
    ...>
    ...
    </activity>
    ...
    </application>

    一旦你知道怎么做,它看起来很容易!

    关于android - 如何在 Kotlin Gradle DSL 中更新 applicationVariants 中的 manifestPlaceholders?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59856354/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com