我们常常需要在Android工程里面放一些比较敏感的信息,比如某些三方服务的API Key之类的信息。但是设置到工程中每次手工修改代码又很麻烦,于是我查看了一些资料,实现了在local.properties这个文件中保存敏感信息。同时因为local.properties这个文件默认被gitignore了,相对来说还是比较安全。
比如MyTMDBApiKey=xxxx
找到android.buildTypes
,在这个后面加上
buildTypes.each {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def myTMDBApiKey = properties.getProperty('MyTMDBApiKey')
it.buildConfigField 'String', 'TMDB_API_KEY', myTMDBApiKey
}