Android release apk keystore相關參數從環境變數或local.properties傳入build.gradle

Android release apk keystore相關參數從環境變數或local.properties傳入build.gradle


build.gradle

方法如下:

android {
signingConfigs {
release {
Properties properties = new Properties()
def customKeystoreFilePath = System.getenv("KEYSTORE_FILEPATH")
if (customKeystoreFilePath == null) {
if (project.rootProject.file('local.properties').exists()) {
properties.load(project.rootProject.file('local.properties').newDataInputStream())
customKeystoreFilePath = properties.getProperty('customKeystoreFilePath')
}
}
def customKeystorePassword = System.getenv("KEYSTORE_PASSWORD")
if (customKeystorePassword == null) {
if (project.rootProject.file('local.properties').exists()) {
properties.load(project.rootProject.file('local.properties').newDataInputStream())
customKeystorePassword = properties.getProperty('customKeystorePassword')
}
}
def customKeyAlias = System.getenv("KEY_ALIAS")
if (customKeyAlias == null) {
if (project.rootProject.file('local.properties').exists()) {
properties.load(project.rootProject.file('local.properties').newDataInputStream())
customKeyAlias = properties.getProperty('customKeyAlias')
}
}
def customKeyPassword = System.getenv("KEY_PASSWORD")
if (customKeyPassword == null) {
if (project.rootProject.file('local.properties').exists()) {
properties.load(project.rootProject.file('local.properties').newDataInputStream())
customKeyPassword = properties.getProperty('customKeyPassword')
}
}
storeFile file(String.valueOf(customKeystoreFilePath))
storePassword customKeystorePassword
keyAlias customKeyAlias
keyPassword customKeyPassword
}
}


....

buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles 'proguard.cfg'
signingConfig signingConfigs.release
}
}


build.gradle部分完成。


接著看需求:
1.從外面傳入以下四個參數

KEYSTORE_FILEPATH

KEYSTORE_PASSWORD

KEY_ALIAS

KEY_PASSWORD

2. 或定義在local.properties

customKeystoreFilePath=C\:\\your key store path\\your.keystore
customKeystorePassword=yourpassword
customKeyAlias=youralias
customKeyPassword=yourpassword




PS:

若password key 錯會噴如下錯誤:




Caused by: java.io.IOException: Keystore was tampered with, or password was incorrect

at com.android.ide.common.signing.KeystoreHelper.getCertificateInfo(KeystoreHelper.java:186)

... 31 more

Caused by: java.security.UnrecoverableKeyException: Password verification failed

... 32 more 

張貼留言

0 留言