动态生成产品风味

时间:2023-02-12
本文介绍了动态生成产品风味的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个需要以多种 (30 多种) 风格构建的 Android 应用.

I've created an Android App which needs to be build in many (30+) flavors.

我的想法是直接从我的 src 目录中的文件夹结构生成不同的 productFlavors,因为配置总是非常相似(基本上只是另一个 packageName,一个新的启动器图标和一些字符串更改).

My idea was to generate the different productFlavors directly from the folder structure in my src directory, since the configuration is always very similar (basically just another packageName, a new launcher icon and some strings change).

src 文件夹如下所示:

└── src
    ├── flavor1
    │   ├── flavor2.keystore
    │   ├── res
    ├── flavor2
    │   ├── res
    │   ├── flavor2.keystore    
    └── main
        ├── AndroidManifest.xml
        ├── java
        └── res

如果我必须手动创建 gradle 属性,它会看起来像这样:

If I had to create the gradle properties by hand it would look somehow like this:

android {

    ....

    productFlavors {
        flavor1 {
            packageName 'com.example.flavor1'
        }
        flavor2 {
            packageName 'com.example.flavor2'
        }
    }

}

每次我尝试在创建后更改 productFlavors 配置时都​​会收到错误消息,或者更改/添加会被忽略.
这可能是我造成的问题,因为我的 Gradle/Groovy 经验非常有限,或者这是不可能的.

Everytime I try to change the productFlavors configuration after its creation I get either an error or the changes / additions are ignored silently.
This could a be problem caused by me, because my Gradle / Groovy experience is very limited, or this isn't possible.

我经常遇到错误,说 GroupableProductFlavorDsl_Decorated 无法按照我想要的方式进行操作.

I mostly get error, saying that GroupableProductFlavorDsl_Decorated could not be manipulated the way I want.

我要归档的内容应该如下所示:

What I'm trying to archive should somehow look like this:

android {

    ....

    def flavors = getMyFlavorsFromFileSystem()

    productFlavors {

    }

    flavors.each { name, config ->
        productFlavors[name] << config
    }

}

注意:我知道这个问题基本上是重复a> 的一个较老的问题,遗憾的是从未得到回答.由于 Gradle 对 Android 世界来说是一种新事物,我希望自上次提出问题以来得到更多答案,因为现在越来越多的开发人员正在使用 Gradle.

Note: I know this question is basically an duplicate of an older question, which sadly was never answered. Since Gradle is kind of new to the Android world, I'm hoping to get more answers as since the last time the question was asked, because more developers are using Gradle now.

更新:

我尝试了一些非常简单的方法:

Here some very simple approaches I tried:

变体 1:

android {

    productFlavors {

    }

    productFlavors['flavor1'] << {
        packageName "com.example.flavor1"
    }

    productFlavors['flavor2'] << {
        packageName "com.example.flavor2"
    }
}

/*

A problem occurred evaluating root project 'MyProject'.
> GroupableProductFlavorDsl with name 'flavor1' not found.

*/

变体 2:

android {

    productFlavors {

    }

    productFlavors['flavor1'] = {
        packageName "com.example.flavor1"
    }

    productFlavors['flavor2'] = {
        packageName "com.example.flavor2"
    }
}

/*

no error, but does not work

*/

变体 3:

android {

    productFlavors {

    }

    productFlavors['flavor1'] = [packageName: "com.example.flavor1"]

    productFlavors['flavor2'] = [packageName: "com.example.flavor2"]
}

/*

no error, but does not work

*/

所有这些都是要点.

推荐答案

通过反复试验解决:

android {

    // let's assume these are return by a function which reads the filesystem
    def myFlavors = [
        flavor1: [
            packageName: "com.example.flavor1"
        ],
        flavor2: [
            packageName: "com.example.flavor2"
        ]
    ]

    productFlavors {
        myFlavors.each { name, config ->
            "$name" {
                packageName config.packageName
            }
        }
    }

}

这篇关于动态生成产品风味的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:NoSuchFieldError:在使用 MapFragment 和 Play Services 6.5 时 下一篇:从 gradle 构建 Android 项目时找不到参数的方法 a

相关文章

最新文章