在項目開發過程當中,通常由程序員完成開發工做後,將代碼提交到git上,測試 同窗pull代碼後,項目build.gradle中設置以下代碼android
buildTypes {
release {
def env = project.hasProperty('ENV')?ENV:"PRODUCT"
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
buildConfigField("String", "BUILD_ENV", "\"$env\"")
printf('------>ENV = '+env)
zipAlignEnabled true
signingConfig signingConfigs.release
}
debug {
def env = project.hasProperty('ENV')?ENV:"TEST"
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
buildConfigField("String", "BUILD_TIME", "\"${buildTime()}\"")
buildConfigField("String", "BUILD_ENV", "\"$env\"")
zipAlignEnabled true
signingConfig signingConfigs.release
}
}
複製代碼
代碼裏可獲取BUILD_ENV的值git
if("PRODUCT".equals(BuildConfig.BUILD_ENV)){
//生產環境
}else if("TEST".equals(BuildConfig.BUILD_ENV)){
//測試環境
}
複製代碼
打包命令程序員
gradle clean assemblerelease -PENV=PRODUCT
gradle clean assemblerelease -PENV=TEST
複製代碼
gradle clean assembledebug -PENV=TEST
gradle clean assembledebug -PENV=TEST
複製代碼