Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get input from user on gradle build in android studio

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?

like image 238
kande Avatar asked Sep 12 '25 06:09

kande


1 Answers

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/)

like image 163
drew Avatar answered Sep 15 '25 01:09

drew