我在https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files找到了一个解决方案并稍作修改。尽管如此,所有的功劳都归于 Tim Roes!
gradle.taskGraph.whenReady { taskGraph ->
if(taskGraph.hasTask(':app:assembleRelease')) {
def storePass = ''
def keyPass = ''
if(System.console() == null) {
new SwingBuilder().edt {
dialog(modal: true, title: 'Enter password', alwaysOnTop: true, resizable: false, locationRelativeTo: null, pack: true, show: true) {
vbox { // Put everything below each other
label(text: "Please enter store passphrase:")
def input1 = passwordField()
label(text: "Please enter key passphrase:")
def input2 = passwordField()
button(defaultButton: true, text: 'OK', actionPerformed: {
storePass = input1.password;
keyPass = input2.password;
dispose();
})
}
}
}
} else {
storePass = System.console().readPassword("\nPlease enter store passphrase: ")
keyPass = System.console().readPassword("\nPlease enter key passphrase: ")
}
if(storePass.size() <= 0 || keyPass.size() <= 0) {
throw new InvalidUserDataException("You must enter the passwords to proceed.")
}
storePass = new String(storePass)
keyPass = new String(keyPass)
android.signingConfigs.release.storePassword = storePass
android.signingConfigs.release.keyPassword = keyPass
}
}
在某个 gradle 文件的某处,您已经定义了发布签名的配置。
android {
...
signingConfigs {
...
release {
storeFile file(System.getProperty("user.home")+"\\android-key")
storePassword ''
keyAlias "standard"
keyPassword ''
}
}
...
}
(别忘了import groovy.swing.SwingBuilder
。)
关于第二部分,你也可以看看How to create a release signed apk file using Gradle?