Currently, my android project loads two parameters from a local properties files to populate some Build.Config constants. The purpose of having the separate local.properties file is to keep it out of source control. (this file is ignored by 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. I also never want it to change just because a developer checks out a different git branch.
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)
My current build.gradle which reads from a properties file:
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:
#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.
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.
//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 + '"')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With