在Gradle中配置文件的构建flavor

时间:2016-02-03 05:47:18

标签: android json gradle android-productflavors

我在 build.gradle 中定义了一些产品风格。对于每个变体,我定义了一个更新属性的任务。如果增加构建变体计数或添加新参数以进行更改,则可能难以管理这些任务。我像这样创建了json

[{
    "server": "url1",
    "name": "name1",
    "language": "en"
},
{
    "server": "url2",
    "name": "name2",
    "language": "es"
}]

如何使用json文件在循环中配置我的构建fariants?这是我当前的 build.gradle

    productFlavors {
    flavor1 {
    }
    flavor2 {
    }
    }


   tasks.whenTaskAdded { task ->
   if (task.name == 'generateFlavor1DebugBuildConfig') {
   task.dependsOn updateSettingsOnFlavor1Build
   }
   if (task.name == 'generateFlavor2DebugBuildConfig') {
   task.dependsOn updateSettingsOnFlavor2Build
   }

   task updateSettingsOnFlavor1Build << {
    ant.propertyfile(file: file('./src/main/assets/settings.properties')) {
      entry(key: "server", value: "myserver.com")
      entry(key: "name", value: "name1")
    }
   }
   task updateSettingsOnFlavor2Build << {
    ant.propertyfile(file: file('./src/main/assets/settings.properties')) {
      entry(key: "server", value: "myserver2.com")
      entry(key: "name", value: "name2")
    }
   }

0 个答案:

没有答案