I have been following this guide to allow loading the secrets for my Android keystore properties from a dedicated file.
Basically, the build.gradle
file contains:
def keystorePropertiesFile = rootProject.file("testandroidpacketcapture/keystore.properties");
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android {
// ...
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
release {
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
}
}
This works as long as the keystore.properties
file exists. But for debug builds, or for developers who do not have access to the keystore, I would like to make this loading of the properties optional.
How could I define that variable only depending on whether that file is available?
I could load the contents only if the file exists()
, but then the file()
call in storeFile
will still fail, because it is not defined.
You can use the getProperty(key, default)
method to define a default value if it is not present.
So, above, use:
def keystorePropertiesFile = rootProject.file("testandroidpacketcapture/keystore.properties");
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
And below, use:
release {
storeFile file(keystoreProperties.getProperty('storeFile', 'release.keystore'))
storePassword keystoreProperties.getProperty('storePassword')
keyAlias keystoreProperties.getProperty('keyAlias')
keyPassword keystoreProperties.getProperty('keyPassword')
}
This will pass the default release.keystore
to the property, which does not matter if you're not actually building a release APK.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.