vourDimensions为每种风味组合设置不同的applic

vourDimensions为每种风味组合设置不同的applic

本文介绍了使用Gradle Kotlin-DSL时,如何使用flavourDimensions为每种风味组合设置不同的applicationId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过使用Kotlinscript文件将Android应用程序转换为Gradle Kotlin-DSL.

I am converting an Android app to the Gradle Kotlin-DSL by using Kotlinscript files.

我在转换applicationId逻辑时遇到问题.我们不会将defaultConfigurationapplicationId以及各种applicationIdSuffix用于我们的口味,而是使用自定义逻辑.该逻辑在此SO答案中进行了描述,这是常规代码:

I have a problem converting our applicationId logic. We don't use the defaultConfiguration with applicationId plus various applicationIdSuffix for our flavors but a custom logic. The logic is described in this SO answer, here is the groovy code:

flavorDimensions "price", "dataset"

productFlavors {
    free { dimension "price" }
    paid { dimension "price" }
    dataset1 { dimension "dataset" }
    dataset2 { dimension "dataset" }
}

android.applicationVariants.all { variant ->
    def mergedFlavor = variant.mergedFlavor
    switch (variant.flavorName) {
        case "freeDataset1":
            mergedFlavor.setApplicationId("com.beansys.freeappdataset1")
            break
        case "freeDataset2":
            mergedFlavor.setApplicationId("com.beansys.freedataset2")
            break
        case "paidDataset1":
            mergedFlavor.setApplicationId("com.beansys.dataset1paid")
            break
        case "paidDataset2":
            mergedFlavor.setApplicationId("com.beansys.mypaiddataset2")
            break
    }
}

使用kotlin,我无法像groovy一样更改mergedFlavorapplicationId.这是一个val,因此无法更改.

With kotlin I cannot alter the applicationId of the mergedFlavor like in groovy. It is a val and therefore can't be changed.

有什么优雅的解决方案可以解决这个问题吗?

Any elegant solution to solve this?

推荐答案

//更新:
当我在下面获得帮助时,它会发出警告:这可能会随着将来的更新而中断". Android Gradle Plugin 4.0.0也是如此.一种解决方法是使用AbstractProductFlavor而不是DefaultProductFlavor.

// Update:
When I got the help below it came with a warning: 'This may break with future updates'. And so it did with Android Gradle Plugin 4.0.0. A workaround is to use AbstractProductFlavor instead of DefaultProductFlavor.

//原始答案:
我在gradle Slack频道上获得了有关此问题的帮助,并获得了在此处共享它的权限:

// Original answer:
I got help on the gradle Slack channel with this problem and the permission to share it here:

诀窍是将mergedFlavor强制转换为DefaultProductFlavor,然后更改其applicationId:

The trick is to cast the mergedFlavor to DefaultProductFlavor and than change the applicationId for it:

flavorDimensions("price", "dataset")

productFlavors {
    create("free") { dimension = "price" }
    create("pro") { dimension = "price" }
    create("dataset1") { dimension = "dataset" }
    create("dataset2") { dimension = "dataset" }
}

android.applicationVariants.all {
    val applicationId = when(name) {
        "freeDataset1" -> "com.beansys.freeappdataset1"
        "freeDataset2" -> "com.beansys.freedataset2"
        "proDataset1" -> "com.beansys.dataset1paid"
        "proDataset2" -> "com.beansys.mypaiddataset2"
        else -> throw(IllegalStateException("Whats your flavor? $name!"))
    }
    (mergedFlavor as DefaultProductFlavor).applicationId = applicationId
}

任何清洁解决方案都值得赞赏!

Any cleaner solution is appreciated!

这篇关于使用Gradle Kotlin-DSL时,如何使用flavourDimensions为每种风味组合设置不同的applicationId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 13:24