我有gradle项目,build.gradle的简化android部分看起来像这样:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
def mapPlacesApiKeys = [dev_1: [api_key: 'key_1', sender_id: 'sender_id_1'], dev_9: [api_key: 'key_2', sender_id: 'sender_id_2'],prod_1: [api_key: 'key_3', sender_id: 'sender_id_3']]
if (project.hasProperty('env')) {
if (mapPlacesApiKeys.get(env) == null) {
def keyset = mapPlacesApiKeys.keySet()
throw new StopExecutionException("Value '$env' is not a valid environment value. Valid environments: $keyset. You can set an environment by passing the -Penv=<env> parameter to your gradle build.")
}
}
buildConfigField "String", "SENDER_ID", "\"${mapPlacesApiKeys.get(env).get('sender_id')}\""
manifestPlaceholders = [maps_places_api_key: mapPlacesApiKeys.get(env).get('api_key')]
}
}
当我从命令行运行它时,我使用环境参数运行它,例如-Penv = dev_1。您可以在脚本中看到两件事: a)在androidmanifest中将值“maps_places_api_key”替换为指定环境的实际键 b)在BuildConfig java类中添加带有SENDER_ID的String。
从命令行可以正常工作。我的问题是,在intellij(或android studio,应该是相同的)当我导入gradle项目时,我得到以下错误:
-Penv is missing. You can set an environment by passing the -Penv=<env> parameter to your gradle build.
我宣布的例外情况。所以我的问题是我如何构造这个代码,以便IDE在加载项目时不会尝试运行它。
请注意,我不想将“env”参数替换为任何类型的flavor或buildTypes,因为大约有15种不同的环境,它会变得一团糟,但是如果还有其他建议会让你觉得免费分享。
答案 0 :(得分:1)
android{
defaultConfig {
applicationId 'com.xx'
minSdkVersion 14
}
productFlavors {
def path="./channel.txt"
file(path).eachLine { line->
def words = line.split(':')
def name = words[0]
def sender_id = words[1]
def api_key = words[2]
"$name" {
buildConfigField "String", "SENDER_ID", sender_id
manifestPlaceholders = [maps_places_api_key: api_key]
}
}
}
}