在 Gradle for Android 中,像这样定义发布构建的签名配置似乎是常见的做法:
android {
signingConfigs {
debug {
storeFile file("debug.keystore")
}
myConfig {
storeFile file("other.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
buildTypes {
foo {
debuggable true
jniDebugBuild true
signingConfig signingConfigs.myConfig
}
}
}
问题是,我想将我的 build.gradle 文件保留在版本控制中,并且在某些设备上我的 keystore 密码(这与我用于其他东西的密码相同,愚蠢,我知道)感觉不太好混帐服务器。
有没有办法从我硬盘上某处的外部文件加载 signingConfig?
最佳答案
我用的是这样的。
我的应用程序根文件夹中有一个 signing.properties
。
STORE_FILE=xxxx
STORE_PASSWORD=xxx
KEY_ALIAS=xxx
KEY_PASSWORD=xxx
此文件不受版本控制。 当然,您可以更改文件夹。
然后在你的 build.gradle
中你可以使用这样的东西:
android {
signingConfigs {
release
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
}
def Properties props = new Properties()
def propFile = file('../signing.properties')
if (propFile.canRead()){
props.load(new FileInputStream(propFile))
if (props!=null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
} else {
android.buildTypes.release.signingConfig = null
}
}else {
android.buildTypes.release.signingConfig = null
}
如果你改变文件夹,你必须改变这一行:
def propFile = file('../signing.properties')
关于Android Gradle - 从外部文件加载签名配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24263715/