[英]Including Android keystore properties file in Gradle, only in release variant
I have been following this guide to allow loading the secrets for my Android keystore properties from a dedicated file.我一直在遵循本指南,以允许从专用文件加载我的 Android 密钥库属性的秘密。
Basically, the build.gradle
file contains:基本上, build.gradle
文件包含:
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.只要keystore.properties
文件存在,这就有效。 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.我只能在文件exists()
,但是storeFile
中的file()
调用仍然会失败,因为它没有定义。
You can use the getProperty(key, default)
method to define a default value if it is not present.如果默认值不存在,您可以使用getProperty(key, default)
方法定义默认值。
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.这会将默认的release.keystore
传递给该属性,如果您实际上没有构建发布 APK,这并不重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.