APP在正式上线前,会在测试环境进行一系列的业务功能测试,而开发人员应该要提供一个测试环境的apk给测试人员,在测试人员完整测试完毕后,开发人员又去修改参数,调整为正式环境后,再进行打包。
有人会问,这不很简单吗?全局有一个管理环境变量的类,打包前去修改这个类的内容就好啦。当然,这没有错,但是,如果两个环境下不仅仅是一个请求接口URL的差异呢?如果包含了各种第三方SDK(极光推送,友盟统计等)的key等信息,切换环境就会特别麻烦,而且需要非常地谨慎,一旦有一个key错了,那问题就严重了。为了避免每次打包都要进行这样繁琐的操作,我们可以利用gradle的特性,将常量存放在BuildConfig中,能够优雅地区分测试环境和正式环境。
环境的变量BuildConfig
请求的URL,第三方SDK的密钥等信息,一般会作为静态变量,存放于一个所谓的Constants类里面,需要引用的时候直接调用这个类即可。但是就像文章开头所说的,变量太多的时候,修改起来就会比较麻烦。我们可以利用一个叫BuildConfig的类,这个类会在APP编译时生成,我们经常利用BuildConfig.DEBUG==true来识别当前是否为调试环境。根据这个思路,那么有没有办法将我们的URL,密钥等信息存放于BuildConfig呢?利用gradle的特性,即可实现。
buildTypes
在app module 的build.gradle中修改buildTypes节点:分别对应debug调试环境、areaTest测试环境、release正式环境,debug环境可与devTest环境配置一致。
applicationIdSuffix
在debug或者测试环境下设置applicationIdSuffix,即可更改APP的包名,以下代码会自动将当前包名更改为默认的包名后加上.test
debug{
applicationIdSuffix:".test"
}
it.buildConfigField
如下面的代码块,it.buildConfigField 主要用来配置BuildConfig的静态变量,写入成功后通过BuildConfig调用,那么在三个环境内配置不同的变量属性即可区分三个环境。
manifestPlaceholders
AndroidManifest文件下的占位标识
AndroidManifest下的节点设置属性=${PAKAGE_NAME},
<meta-data
android:name="UMENG_CHANNEL"
android:value="${PAKAGE_NAME}"
/>
那么在manifestPlaceholders配置对应字段即可
manifestPlaceholders = [ PAKAGE_NAME:"com.***.sample"]
在编译打包的过程中,会将AndroidManifest的占位标识自动替换。对于一些sdk只能在AndroidManifest去设置key的情况,那么这种方式就可以用于区分各个环境了。
buildTypes {
release {
it.buildConfigField 'String', 'BASE_URL', "\"http://sample.com:8080/\"";
it.buildConfigField 'String', 'API_URL', "\"dlpmobile/mobile/\"";
it.buildConfigField 'String', 'UPLOAD_FILE_URL', "\"dlpfilesystem/utils/imageService/\"";
it.buildConfigField 'String', 'E_CMS_URL', "\"dlpecms/ecms/\""; it.buildConfigField 'String', 'JPUSH_APPKEY',"\"adadasd\"";
it.buildConfigField 'String', 'MIPUSH_APPKEY',"\"2123\"";
it.buildConfigField 'String', 'MIPUSH_APP_ID',"\"312312\"";
it.buildConfigField 'String', 'BUGLY_APP_ID',"\"312312\"";
manifestPlaceholders = [
APP_NAME:"测试用例",
PAKAGE_NAME:"****.sample",
JPUSH_PERMISSION:"***.sample.permission.JPUSH_MESSAGE",
MIPUSH_PERMISSION:"***.sample.permission.MIPUSH_RECEIVE",
UMENG_CHANNEL: "sample",
JPUSH_PKGNAME: "***.sample",
JPUSH_APPKEY : "adads21312", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL: "tencent" //用户渠道统计的渠道名称
] signingConfig signingConfigs.config minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
debug {
applicationIdSuffix ".test"
it.buildConfigField 'String', 'BASE_URL', "\"http://172.16.17.152:8080/\"";
it.buildConfigField 'String', 'API_URL', "\"dlpmobile/mobile/\"";
it.buildConfigField 'String', 'UPLOAD_FILE_URL', "\"dlpfilesystem/utils/imageService/\"";
it.buildConfigField 'String', 'E_CMS_URL', "\"dlpecms/ecms/\"";
it.buildConfigField 'String', 'JPUSH_APPKEY',"\"adasdasd\"";
it.buildConfigField 'String', 'MIPUSH_APPKEY',"\"dadasd\"";
it.buildConfigField 'String', 'MIPUSH_APP_ID',"\"dasdasd\"";
it.buildConfigField 'String', 'BUGLY_APP_ID',"\"dasdasd\"";
manifestPlaceholders = [
APP_NAME:"sample测试版",
PAKAGE_NAME:"****.sample.test",
JPUSH_PERMISSION:"****.dlp.sample.test.permission.JPUSH_MESSAGE",
MIPUSH_PERMISSION:"****.dlp.sample.test.permission.MIPUSH_RECEIVE",
UMENG_CHANNEL: "sample",
JPUSH_PKGNAME: "****.sample.test",
JPUSH_APPKEY : "dasdasdasd", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL: "tencent" //用户渠道统计的渠道名称
] }
devTest{ applicationIdSuffix ".test"
it.buildConfigField 'String', 'BASE_URL', "\"http://172.16.17.152:8080/\"";
it.buildConfigField 'String', 'API_URL', "\"dlpmobile/mobile/\"";
it.buildConfigField 'String', 'UPLOAD_FILE_URL', "\"dlpfilesystem/utils/imageService/\"";
it.buildConfigField 'String', 'E_CMS_URL', "\"dlpecms/ecms/\"";
it.buildConfigField 'String', 'JPUSH_APPKEY',"\"dadasdasd\"";
it.buildConfigField 'String', 'MIPUSH_APPKEY',"\"dasdadasd\"";
it.buildConfigField 'String', 'MIPUSH_APP_ID',"\"dasdasd\"";
it.buildConfigField 'String', 'BUGLY_APP_ID',"\"dadasd\"";
manifestPlaceholders = [
APP_NAME:"sample测试版",
PAKAGE_NAME:"****.sample.test",
JPUSH_PERMISSION:"****.sample.test.permission.JPUSH_MESSAGE",
MIPUSH_PERMISSION:"****.sample.test.permission.MIPUSH_RECEIVE",
UMENG_CHANNEL: "sample",
JPUSH_PKGNAME: "****.sample.test",
JPUSH_APPKEY : "dasdasdasd", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL: "tencent" //用户渠道统计的渠道名称
] signingConfig signingConfigs.config minifyEnabled false }}
打包
发布正式环境时,选择release编译打包即可,测试环境则选择devTest