Skip to main content
 首页 » 编程设计

android-studio之Android Studio gradle flavordimensions 构建变体无法正常工作

2025年04月02日23飞鱼

我有一个应用程序的两个维度,然后调用绿色和蓝色。只有这两个维度,但有无限数量的产品口味。这是我在 gradle 中设置它的方式

flavorDimensions "green", "blue" 
 
productFlavors { 
 
    one { 
        applicationId "com.app.green.one" 
        versionCode 1 
        versionName "1.0.0.1"; 
        flavorDimension = "green" 
    } 
    two { 
        applicationId "com.app.blue.two" 
        versionCode 6 
        versionName "1.0.1"; 
        flavorDimension = "blue" 
    } 
} 

但是在我同步 gradle 之后,在构建变体选项卡中我看到的只有 oneTwoDebug 和 oneTwoRelease,我应该看到 greenOneDebug greenOneRelease、blueTwoDebug、blueTwoRelease

理论上我想把它扩展成这样
one { 
    applicationId "com.app.green.one" 
    versionCode 1 
    versionName "1.0.0.1"; 
    flavorDimension = "green" 
} 
two { 
    applicationId "com.app.blue.two" 
    versionCode 6 
    versionName "1.0.1"; 
    flavorDimension = "blue" 
} 
three { 
    applicationId "com.app.green.three" 
    versionCode 1 
    versionName "1.0.0.1"; 
    flavorDimension = "green" 
} 
four { 
    applicationId "com.app.blue.four" 
    versionCode 6 
    versionName "1.0.1"; 
    flavorDimension = "blue" 
} 

在这种情况下,维度代表应用程序的“类型”,然后可以添加更多用于组织的风格。

**编辑我对 gradle 的设置有误,正如这里指出的那样,这是对我所拥有的更准确的描述
flavorDimensions "type", "organization" 
 
productFlavors { 
 
    blue { 
        applicationId "com.app.blue" 
        flavorDimension = "type" 
        versionCode 6 
        versionName "1.0.1"; 
    } 
    red { 
        applicationId "com.app.red" 
        flavorDimension = "type" 
        versionCode 1 
        versionName "1.0.0.1"; 
    } 
 
    company1 { 
        flavorDimension = "organization" 
    } 
    company2 { 
        flavorDimension = "organization" 
    } 
} 

到目前为止这是有效的,所以我可以为切换类型创建 java 源目录,但是如果我想要组织特定的配置文件,我是否也为每个组织创建 java 源目录?

请您参考如下方法:

我认为您误解了 flavor 维度的概念。

flavor 维度类似于 flavor 类别,来自每个维度的 flavor 的每种组合都会产生一个变体。

在您的情况下,您必须定义一个名为“type”的 flavorDimension 和另一个名为“organization”的维度。
它将为维度“组织”中的每个 flavor 产生所有可能的“类型”(或双重公式:对于每个“类型”,它将为每个组织产生一个变体)。

flavor 维度定义将用于生成变体的笛卡尔积。

编辑 :我会尝试用伪 gradle 代码来说明:

让我们定义一些“类型”:青铜、银和金

让我们定义一些组织:customerA、customerB、customerC

所有这些都是 productFlavors,但它们属于 2 个不同的维度:

flavorDimensions("type_line", "organization") 
productFlavors { 
 
    gold { 
        ... 
        dimension = "type_line" 
    } 
    silver { 
        ... 
        dimension = "type_line" 
    } 
    bronze { 
         ... 
        dimension = "type_line" 
    } 
 
     customerA { 
        ... 
        dimension = "organization" 
    } 
    customerB { 
        ... 
        dimension = "organization" 
    } 
    customerC { 
         ... 
        dimension = "organization" 
    } 
 
} 

此配置将产生 18 (3*3*2) 个变体(如果您有 2 种标准构建类型:调试和发布):

gold-customerA-debug ; gold-customerA-release ; gold-customerB-debug ; gold-customerB-release ; gold-customerC-debug ; gold-customerC-release ;

银-客户A-调试;银客户A-release ; silver-customerB-debug ; Silver-customerB-release ;银客户C-调试; Silver-customerC-release ;

...(青铜相同)

请注意,维度的名称是完全任意的,对变体名称没有影响。

flavor 维度非常强大,但是如果您使用太多它们:它会导致变体数量呈指数级增长(构建后清理任务可能有助于删除无用或无意义的变体)