本文共 2491 字,大约阅读时间需要 8 分钟。
前言:
使用工具:AndroidStudio 适用场景:打包时自动识别生产环境和测试环境的不同的地址 了解知识:Gradle的使用语言Groovydebug下:www.baidu.comrelease下:www.google.com // =================================================SERVER2 = "http://www.baidu.com"IMAGEREQUEST = "http://www.aaa.com:端口号"WEB = "http://dev.xxx.com:端口号" statistical = "https://dev.aaa.com:端口号/android/"
buildTypes{ release{ minifyEnabled true//开启混淆 proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'// 加载混淆配置文件 buildConfigField'String','SERVER2', getSERVER2('release') buildConfigField'String','IMAGEREQUEST', getIMAGEREQUEST('release') buildConfigField'String','WEB', getWEB('release') buildConfigField'String','statistical', getstatistical('release') signingConfig signingConfigs.releaseConfig // 签名配置;小科普:此行的调用需放到签名配置之后,否则编译会报异常 } debug{ buildConfigField'String','SERVER2', getSERVER2('debug') buildConfigField'String','IMAGEREQUEST', getIMAGEREQUEST('debug') buildConfigField'String','WEB', getWEB('debug') buildConfigField'String','statistical', getstatistical('debug') signingConfig signingConfigs.debugConfig // 签名配置 }}
// 获取配置文件内容的地址def getSERVER2(String str) { def SERVER2 def Properties properties = new Properties() def proFile = file('src/main/filters/' + str + '/config.properties') // 配置文件地址的路径 if (proFile.canRead()) { // 读取 properties.load(new FileInputStream(proFile)) // 加载 if (properties != null) { SERVER2 = properties['SERVER2'] // 赋值 } } SERVER2} def getIMAGEREQUEST(String str) { def IMAGEREQUEST def Properties properties = new Properties() def proFile = file('src/main/filters/' + str + '/config.properties') if (proFile.canRead()) { properties.load(new FileInputStream(proFile)) if (properties != null) { IMAGEREQUEST = properties['IMAGEREQUEST'] } } IMAGEREQUEST}