动态生成的产品口味 [英] Dynamically generating product flavors

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

问题描述

我创建了一个Android应用程序需要被建立在很多(30+)的味道。

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

我的想法是,以产生不同的 productFlavors 直接从我的的src 目录下的文件夹结构中,由于配置总是很相似(基本上只是另一种的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

如果我不得不创建摇篮性质用手它看起来在某种程度上是这样的:

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 配置它的创作后,我得到任何错误或修改/添加被忽略。
这可能是一个引起了我的问题,因为我的摇篮/ 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
    }

}

注意:我知道这个问题基本上是一个<一href="http://stackoverflow.com/questions/20183638/add-flavors-to-gradle-programmatically">duplicate一个老问题,那可悲的是从来没有回答。由于摇篮是一种新的机器人世界,我希望能得到更多的答案,因为自上次有人问,因为越来越多的开发人员正在使用摇篮。

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:**

** Variant 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
            }
        }
    }

}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆