[英]How can I create a default properties file if it doesn't exist in Android Gradle Build
Currently, my android project loads two parameters from a local properties files to populate some Build.Config
constants.目前,我的 android 项目从本地属性文件加载两个参数来填充一些Build.Config
常量。 The purpose of having the separate local.properties
file is to keep it out of source control.拥有单独的local.properties
文件的目的是使其不受源代码控制。 (this file is ignored by git). (此文件被 git 忽略)。 The values in it are of no value to production builds and may be frequently changed by a developer.其中的值对生产构建没有价值,开发人员可能会经常更改。 I don't want a change of these values to constitute a change in build.gradle
.我不希望这些值的变化构成build.gradle
的变化。 I also never want it to change just because a developer checks out a different git branch.我也不希望它仅仅因为开发人员签出不同的 git 分支而改变。
My problem is that since this property file is not in source control, a fresh clone and checkout will fail to build because the file does not exist.我的问题是,由于此属性文件不在源代码管理中,新的克隆和检出将无法构建,因为该文件不存在。 In the case where the file does not exist, I want the script to create it and save default parameters to it.在文件不存在的情况下,我希望脚本创建它并将默认参数保存到它。
ERROR: C:\\Users\\Me\\AndroidStudioProjects\\MyAwesomeApp\\app\\local.properties (The system cannot find the file specified)错误:C:\\Users\\Me\\AndroidStudioProjects\\MyAwesomeApp\\app\\local.properties(系统找不到指定的文件)
My current build.gradle
which reads from a properties file:我当前的build.gradle
从属性文件中读取:
Properties properties = new Properties()
properties.load(project.file('local.properties').newDataInputStream())
def spoofVin = properties.getProperty('spoof.vin', '12345678901234567')
def spoofId = properties.getProperty('spoof.id', '999999999999')
buildConfigField("String", "SPOOF_VIN", '"' + spoofVin + '"')
buildConfigField("String", "SPOOF_ID", '"' + spoofId + '"')
Example app/local.properties
file:示例app/local.properties
文件:
#Change these variables to spoof different IDs and VINs. Don't commit this file to source control.
#Wed Nov 18 12:13:30 CST 2020
spoof.id=999999999999
spoof.vin=12345678901234567
I'm posting my own solution below, to hopefully help someone else out if they have the same needs.我在下面发布了我自己的解决方案,希望可以帮助其他有相同需求的人。 I'm not a Gradle pro, so if you know an even better way to do this, post your solution.我不是 Gradle 专家,因此如果您知道更好的方法,请发布您的解决方案。
The following code is what I've found works to accomplish this task.以下代码是我发现可以完成此任务的代码。 The properties.store
method conveniently lets me add a string comment to the top of the properties.local file too. properties.store
方法也方便地让我在 properties.local 文件的顶部添加字符串注释。
//The following are defaults for new clones of the project.
//To change the spoof parameters, edit local.properties
def defaultSpoofVin = '12345678901234567'
def defaultSpoofId = '999999999999'
def spoofVinKey = 'spoof.vin'
def spoofIdKey = 'spoof.id'
Properties properties = new Properties()
File propertiesFile = project.file('local.properties')
if (!propertiesFile.exists()) {
//Create a default properties file
properties.setProperty(spoofVinKey, defaultSpoofVin)
properties.setProperty(spoofIdKey, defaultSpoofId)
Writer writer = new FileWriter(propertiesFile, false)
properties.store(writer, "Change these variables to spoof different IDs and VINs. Don't commit this file to source control.")
writer.close()
}
properties.load(propertiesFile.newDataInputStream())
def spoofVin = properties.getProperty(spoofVinKey, defaultSpoofVin)
def spoofId = properties.getProperty(spoofIdKey, defaultSpoofId)
buildConfigField("String", "SPOOF_VIN", '"' + spoofVin + '"')
buildConfigField("String", "SPOOF_ID", '"' + spoofId + '"')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.