如何定义属性并将其设置为特定的buildType?
我已经尝试过这个:
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)
}
由此,“api”始终具有最后一个块的值。在Gradle构建之前,如何动态设置与当前buildType相关的属性?
答案 0 :(得分: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
答案 1 :(得分:-1)
尝试使用buildConfigField
。 F.e:
buildConfigField 'String', 'API_URL', '\"http://my.api.com\"'
buildConfigField 'boolean', 'LOG_ENABLED', 'true'
在您的代码中:
String api = BuildConfig.API_URL;
if (BuildConfig.LOG_ENABLED){
...
}