I am using Android Studio IDE and I need to have the option to assign a variable in my code with a value that will be entered on running the apk or while generating an apk.
I understand that I cannot use System.console
, is there any other solution?
It's possible to do this either using a command line input or a swing dialog within Android Studio. The sample task below will display a dialog if the gradle is running as a daemon and as a command line prompt if it's not.
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}"
}
(With thanks to Tim Roes for the groovy solution https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With