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

时间:2018-08-09 11:22:44

标签: android gradle build android-productflavors

我有一个带有两个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"
        }
    }

{{1}}

和许多其他组合,但似乎都不起作用。

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

非常感谢您的帮助!

谢谢,祝一切顺利!

1 个答案:

答案 0 :(得分:3)

使用

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

    }
}