1

How can I define a property and set it in a specific buildType?

I have already tried this:

android {
  project.ext.set("apis", "[]")

  buildTypes {
      release {
      ...
        project.apis = [
                    [
                            name   : "aaaa",
                            apiJson: "aaaa",
                            baseUrl: "aaaa"
                    ],
                    [
                            name   : "bbbb",
                            apiJson: "bbbb",
                            baseUrl: "bbbb"
                    ]
                 ]
      }

    debug{
      ...
        project.apis = [
                    [
                            name   : "cccc",
                            apiJson: "cccc",
                            baseUrl: "cccc"
                    ],
                    [
                            name   : "dddd",
                            apiJson: "dddd",
                            baseUrl: "dddd"
                    ]
                 ]
      }

   }

  func(apis)
}

From this, "api", always has the values of the last block. How can I dynamically set a property related to my current buildType until Gradle builds?

4

2 回答 2

1

我以这种方式解决了它:

task config << {
    def apis
    switch (project.gradle.startParameter.taskNames[0]){
      case "app:assembleRelease":
          project.apis = [
                [
                        name   : "aaaa",
                        apiJson: "aaaa",
                        baseUrl: "aaaa"
                ],
                [
                        name   : "bbbb",
                        apiJson: "bbbb",
                        baseUrl: "bbbb"
                ]
             ]
         break;
      case "app:assembleDebug":
       apis = [
                [
                        name   : "cccc",
                        apiJson: "cccc",
                        baseUrl: "cccc"
                ],
                [
                        name   : "dddd",
                        apiJson: "dddd",
                        baseUrl: "dddd"
                ]
             ]
        ]
      break;
    }
    func(apis)
}
preBuild.dependsOn config
于 2015-08-24T06:34:01.117 回答
-1

尝试使用buildConfigField. 铁:

buildConfigField 'String', 'API_URL', '\"http://my.api.com\"'
buildConfigField 'boolean', 'LOG_ENABLED', 'true'

在您的代码中:

String api = BuildConfig.API_URL;
if (BuildConfig.LOG_ENABLED){
...
}
于 2015-08-21T09:44:31.160 回答