我正在使用Android Studio IDE,我需要选择在我的代码中为变量分配一个值,该值将在运行apk或生成apk时输入。
我知道我不能使用127.0.0.1:6379> config set maxclients 10000
,还有其他解决方案吗?
答案 0 :(得分:2)
可以使用Android Studio中的命令行输入或swing对话框来执行此操作。如果gradle作为守护程序运行,则下面的示例任务将显示一个对话框,如果不是,则显示命令行提示符。
import groovy.swing.SwingBuilder
task ask << {
def keyPassPhrase
def console = System.console()
if (console) {
keyPassPhrase = console.readLine('> Please enter key pass phrase: ')
} else {
new SwingBuilder().edt {
dialog(modal: true,
title: 'Enter credentials',
alwaysOnTop: true,
resizable: false,
locationRelativeTo: null,
pack: true,
show: true
) {
vbox {
label(text: "Please enter key passphrase:")
input = passwordField()
button(defaultButton: true, text: 'OK', actionPerformed: {
keyPassPhrase = input.password;
dispose();
})
}
}
}
}
print "Key pass phrase: ${keyPassPhrase}"
}
(感谢Tim Roes提供的常规解决方案https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files/)
答案 1 :(得分:2)
@ drew的解决方案很棒,但它只接受密钥库和密钥的单个密码。我需要一个允许两个不同密码的解决方案,一个用于密钥库,另一个用于密钥。
不熟悉Groovy,需要一些反复试验才能将其扩展到两个输入字段 - 这就是我最终的结果:
import groovy.swing.SwingBuilder
...
/**
* Ask for keystore/key passwords on the command line or popup UI
*/
task askForPasswords << {
def console = System.console()
def storePw
def keyPw
if (console) {
// Must create String because System.readPassword() returns char[]
// (and assigning that below fails silently)
storePw = new String(console.readPassword("\nKeystore password: "))
keyPw = new String(console.readPassword("Key password: "))
} else {
// Gradle is running as a daemon - prompt user to enter passwords via popup UI
new SwingBuilder().edt {
dialog(modal: true, title: 'Enter credentials', alwaysOnTop: true, resizable: false,
locationRelativeTo: null, pack: true, show: true
) {
vbox {
label(text: "Keystore passphrase:")
textField id: "storeText", input = passwordField()
label(text: "Key passphrase:")
textField id: "keyText", input = passwordField()
button(defaultButton: true, text: 'OK', actionPerformed: {
storePw = storeText.text;
keyPw = keyText.text;
dispose();
})
}
}
}
}
android.signingConfigs.release.storePassword = storePw
android.signingConfigs.release.keyPassword = keyPw
}
......看起来像这样:
请参阅https://github.com/OneBusAway/onebusaway-android/blob/master/onebusaway-android/build.gradle#L291的相关工作示例。