在 gradle 中设置组合产品风味的 applicationId

Setting the applicationId in gradle for a combined product flavor

我的应用有两个 productFlavors:

flavorDimensions "type", "country"

productFlavors {
    free {
        dimension "type"
    }
    premium {
        dimension "type"
    }
    us {
        dimension "country"
    }
    de {
        dimension "country"
    }
}

这导致四个 buildVariants(忽略构建类型)。

现在我面临的挑战是为这四种变体设置不同的 applicationId,但我不知道该怎么做。 我发现实际更改 applicationId 的唯一方法是在 productFlavors 定义中,如下所示:

productFlavors {
    free {
        applicationId "some.application.id"
        dimension "type"
    }

但问题是,例如 flavor 实际上是 freeDe,我想为该组合设置 applicationId

我试过了:

productFlavors {
    freeDe {
        applicationId "some.random.id"
    }

productFlavors {
    free {
        de {
            applicationId "some.random.id"
        }
    }

和许多其他组合,但其中 none 似乎有效。

重要提示:我不能为此使用 pre/suffix,因为应用程序 ID 彼此完全不同:(

非常感谢帮助!

谢谢,祝一切顺利!

使用它

android.applicationVariants.all { variant ->
    def mergedFlavor = variant.mergedFlavor
    switch (variant.flavorName) {
        case "freeUs":
            mergedFlavor.setApplicationId("com.application.id.freeUs")
            break
        case "freeDe":
            mergedFlavor.setApplicationId("com.application.id.freeDe")
            break

    }
}