在项目中,我们有时会需要根据不同的项目环境,使用不同的项目变量,
举例:
在测试环境中需要app访问的服务器地址是测试服务器
正式发布下的app需要访问正式服务器。
可以使用下面三种技术来满足要求:
使用 gradle 在编译时动态设置 Android resValue
修改 res value 的方式,比如修改 strings.xml 文件中的 AppName 的值
在你的 gradle 文件 buildTypes 或者 productFlavors 下面,如 release 体内写上类似:
resValue "string", "AppName", "app1"
意为把名为 AppName 的 string value 值改为 app1
使用 gradle 在编译时动态设置 Android BuildConfig
在你的 gradle 文件 buildTypes 或者 productFlavors 下面,如 release 体内写上类似:
buildConfigField "String", "ENDPOINT", "\"http://example.com\""
gradle sync 一下后,BuildConfig.ENDPOINT 就会被赋值为 http://example.com 就可以