I'm trying to get my gradle builds to prompt at the console for a password using examples from stack overflow
When I have a statment such as:
def password = System.console().readLine("Enter keystore password ")
When I run I get the error
Cannot invoke method readLine() on null object
It seems console is coming out as null
. What I've read this requires java 6 which if I go to a command prompt and type java -version
I'm running Java(TM) SE Runtime Environment (build 1.6.0_27-b07).
This issue is being tracked in Gradle's Github repo: Can't use System.console() with the Gradle Daemon.
For some reason, running gradle in daemon mode causes a null console object. If you specify the appropriate command line flag,
./gradlew assembleRelease --no-daemon
it'll work.
Ok, the reason why this didn't work was silly, but just in case anyone else comes across it I thought I'd post.
I was running the task through android studio and didn't realise that the console object would always be null. When running from the command line the "command" object isn't null and it works ok.
Executing
System.getConsole()
from Gradle whenorg.gradle.daemon
property istrue
, or when it's executed from an IDE like IntelliJ or Android Studio it returnsnull
. So for example doSystem.console().readLine()
becomes not possible.Furthermore starting from Gradle 3.0
gradle.daemon
is turned on by default.Then instead of a workaround to use
System.getConsole()
I purpose an alternative, useant.input
like so:In this case
ant.input
shows themessage
and adds the user input inant.properties
using as a property name the value defined inaddProperty
. If there is no user input then the value defined indefault
attribute is used.Once executed you can get the user input using
ant.properties.yourProperty
orant.properties['yourProperty']
.You can check the rest of the
ant.input
attributes here.Note: If you want to use
ant.input
multiple times take in account that you cannot override and existing property soaddProperty
attribute must be different for each one.You can execute your script also with:
-Dorg.gradle.daemon=false
I found a solution here at https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files and slightly modified it. Nevertheless, all credits go to Tim Roes!
Somewhere in the some gradle file, you have the configuration for the release signing defined.
(Don't forget to
import groovy.swing.SwingBuilder
.)Regarding the second part, you may also have a look at How to create a release signed apk file using Gradle?
Take a look at this blog post (https://www.timroes.de/2013/09/22/handling-signing-configs-with-gradle/).
It describes multiple ways to handle signing configs and one of them is exactly your question regarding console input for the password.