使用CSV/TXT文件中的名称列表(带有属性)动态生成productFlavors和sourceSet [英] Dynamically generating productFlavors and sourceSets using a list of names (with properties) in a CSV/TXT file

本文介绍了使用CSV/TXT文件中的名称列表(带有属性)动态生成productFlavors和sourceSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我要回答的其他问题的延续,我想进一步加以改进.

This question in continuation of my other question, which i want to improve further.

我可以使用以下代码在sourceSets下对风味(具有通用配置)进行分组:

I am being able to group flavors (having common configuration) under sourceSets with the following code :

(是从上面链接的问题中的天才那里得到的)

import com.android.build.gradle.api.AndroidSourceSet
android {
    sourceSets {
        [flavor2, flavor4].each { AndroidSourceSet ss ->
            ss.assets.srcDirs = ['repo-assets/flavor2']
            ss.res.srcDirs = ['repo-res/flavor2']
        }
    }
}

现在,我想知道列表[flavor2, flavor4]是否可以从以下任意一项中提取:

Now, I was wondering if the list [flavor2, flavor4] could be pulled from any of the following:

  • 一个XML文件,我可以在该XML文件上迭代以获取所有风味(将放置在其中)
  • 我可以在其上迭代和获取值的CSV文件.
  • 一个自定义类,我可以在一个单独的文件中编写该类,并从该类的静态成员中获取数据.

除了风味名称之外,我还打算将以下内容存储在外部源中(上述之一):

In addition to the flavor name, I intend to store the following in the external source (one of above):

  • 应用程序ID(我将其拉至productFlavors)
  • 广告单元ID(每种口味两个)
  • 一些其他自定义值,例如category等.

目的:我想编写一段通用代码来迭代并动态创建productFlavorssourceSets.我将sourceSets概化为将近90%,现在所有口味的一个块就足够了.

PORPOSE: I want to write a generic piece of code to iterate and dynamically create the productFlavors and sourceSets. I have generalized sourceSets to almost 90% and one block is now sufficing for all flavors.

现在看起来像这样:

sourceSets {
    [flavor1, flavor2, flavor3 ...].each { AndroidSourceSet ss ->
                ss.assets.srcDirs = ['repo-assets/' + ss.name.split('_')[0]]
                ss.res.srcDirs = ['repo-mipmap/' + ss.name.split('_')[0] , 'repo-strings/' + ss.name]
            }
}

还希望对productFlavors做同样的事情.

Also want to do the same thing for productFlavors as hinted above.

卡住::从外部来源获取上面代码中的列表[flavor2, flavor4](以及上面列出的每种口味的一些其他字段).

STUCK AT: getting the list [flavor2, flavor4] in the code above from an external source (along with a few additional fields per flavor as listed above).

我看到类似

productFlavors.add()
productFlavors.addAll()

,但不确定如何使用这些功能.只要有这些方法,我就可以做我想做的事情.

but am not quite sure about how to go about using these. As the methods are available I'm sure it is possible to do what I'm trying to.

有人这样做并且有一些指针吗?

Has anyone done this and has some pointers?

推荐答案

我终于使它以这种方式工作:

I finally got it to work this way:

build.gradle中创建了一个自定义类MyFlavor,并将csv文件中的每种样式添加到MyFlavorArrayList

Created a custom class MyFlavor within build.gradle and added each flavor from the csv file to an ArrayList of MyFlavor

class MyFlavor {
    public String flavorname;
    public String basename;
    public String appid;
    public String bannerid;
    public String interstitialid;
    public String category;
}

def ArrayList<MyFlavor> myFlavors = new ArrayList<>();

new File('app/flavors.csv').eachLine {
    String[] values = "$it".split(',');
    MyFlavor f = new MyFlavor();
    f.flavorname = values[0];
    f.basename = values[0].split('_')[0];
    f.appid = values[1];
    f.bannerid = values[2];
    f.interstitialid = values[3];
    if(values[0].contains('_')) f.category= "state"
    else f.category = "country";
    myFlavors.add(f);
}

然后遍历ArrayList以动态创建productFlavorssourceSets,如下所示:

Then iterated over the ArrayList to dynamically create the productFlavors and sourceSets as follows:

productFlavors {
    myFlavors.each { MyFlavor f ->
        "$f.flavorname" {
            applicationId = "$f.appid"
            buildConfigField 'String', 'BANNER_ID', "\"" + "$f.bannerid" + "\""
            buildConfigField 'String', 'INTERSTITIAL_ID', "\"" + "$f.interstitialid" + "\""
            buildConfigField 'String', 'CATEGORY', "\"" + "$f.category" + "\""
        }
    }
}
sourceSets {
    myFlavors.each { MyFlavor f ->
        "$f.flavorname" {
            assets.srcDirs = ['repo-assets/' + "$f.basename"]
            res.srcDirs = ['repo-mipmap/' + "$f.basename" , 'repo-strings/' + "$f.flavorname"]
        }

    }
}

希望这对某人有帮助.我成功摆脱了数千行代码(因为我有很多口味)并将其降低到您在此处看到的这十行代码.

Hope this helps someone. I was successful in getting rid of 1000s of lines of code (because i have a lot of flavors) and bring it down to these 10s of lines you see here.

这篇关于使用CSV/TXT文件中的名称列表(带有属性)动态生成productFlavors和sourceSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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