如何使用 flavorDimensions 为每种风味组合设置不同

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

问题描述

我有一个旧的 android 应用程序,我正在尝试迁移到 android gradle 构建系统.该应用程序当前构建在多项目设置中,并作为四个不同的应用程序发布(包括两个不同的数据集和两个数据集的免费/付费版本).通过使用 flavorDimensions(以前称为 flavorGroups),我设法摆脱了多项目设置,但我不知道如何为每种风味组合设置不同的 applicationId.

I have and old android app that I am trying to migrate to the android gradle build system. The app is currently built in a multi project setup and published as four different apps (two different data sets included and free/paid versions for both datasets). I have managed to get away from the multi project setup by using flavorDimensions (previously called flavorGroups), but I can not figure out how to set a different applicationId for each flavor combination.

由于应用程序版本已经发布,我需要保持与当前相同的应用程序 ID.由于我最初的包命名是如何完成的,我不能简单地使用带有packageNameSuffix"的flavor-buildtype组合(如果它是一个未发布的应用程序,这将是一个很好的选择).

Since the app versions are already published, I need to keep the same applicationid as they currently have. Because of how my original package naming was done, I can not simply use flavor-buildtype combination with "packageNameSuffix" (which would have been a great option if it was an unpublished app).

https://stackoverflow.com/a/20956353/4177090 正在回答如何使用不同的源文件夹来调味组合,而不是如何(如果可能的话)为构建文件中的每个组合设置特定配置.

https://stackoverflow.com/a/20956353/4177090 is answering how to use different source folders for flavor combinations, but not how (if even possible) to set specific configuration for each combination in the build file.

Gradle 构建文件片段:

Gradle build file snippet:

flavorDimensions "price", "dataset"

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

我想在我的 gradle 构建文件中包含以下内容(注意我的命名是多么不合逻辑,这就是我不能使用 packageNameSuffix 的原因):

I would like to have something like the following in my gradle build file (notice how unlogic my naming is, which is why I cannot use packageNameSuffix):

freeDataset1 { applicationId "com.beansys.freeappdataset1" }
freeDataset2 { applicationId "com.beansys.freedataset2" }
paidDataset1 { applicationId "com.beansys.dataset1paid" }
paidDataset2 { applicationId "com.beansys.mypaiddataset2" }

推荐答案

我终于设法解决了这个问题.我认为解决方案很优雅(尽管实际代码很可能由具有 groovy 知识的人编写得更好).

I finally managed to solve this. I think the solution is elegant (although the actual code could most likely be written a lot nicer by someone with groovy knowledge).

为每种组合风味设置特定 applicationId 的解决方案:

Solution for setting a specific applicationId for each combined flavor:

flavorDimensions "price", "dataset"

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

android.variantFilter { variant ->
    def flavorString = ""
    def flavors = variant.getFlavors()
    for (int i = 0; i < flavors.size(); i++) {
        flavorString += flavors[i].name;
    }
    if(flavorString.equalsIgnoreCase("freeDataset1")) {
        variant.getDefaultConfig().applicationId "com.beansys.freeappdataset1"
    }
    if(flavorString.equalsIgnoreCase("freeDataset2")) {
        variant.getDefaultConfig().applicationId "com.beansys.freedataset2"
    }
    if(flavorString.equalsIgnoreCase("paidDataset1")) {
        variant.getDefaultConfig().applicationId "com.beansys.dataset1paid"
    }
    if(flavorString.equalsIgnoreCase("paidDataset2")) {
        variant.getDefaultConfig().applicationId "com.beansys.mypaiddataset2"
    }
}

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

上一篇:使用 Gradle 组装时如何更改 Android 应用程序包名称 下一篇:错误:任务 ':ProjectName:mergeDebugResources' 执行

相关文章

最新文章