Gradle from the Command Line

Posted by admin at 9:54 AM on Jul 8, 2016


While Android Studio provides a nice GUI for interacting with gradle, there are times when a command-line approach is more helpful. You might want to do this in order to automate builds or build on a remote server. Here, we’ll take a look at some ways to do common tasks without opening up Android Studio at all.

The basic format for all gradle commands is:

gradle [option(s)] <task>

In this case, ‘root’ is where the gradle wrapper is. When you create the wrapper, gradle will look for a build.gradle file in the current directory and use that to make the wrapper. So the root of the project is where your target build.gradle file is.

Some common tasks are listed in the table below:

Command Description
creates gradle wrapper
assembles all debug builds
assembles all release builds
deletes build directory
builds and tests current project
view list of tasks for this project

A helpful option is the -q flag, which suppresses a lot of the extra output of what gradle is doing. This is especially useful when calling ‘gradle tasks’, because ‘gradle -q tasks’ will suppress the build related output, and just show the task list (with descriptions).

If you want to continue build in the event of failure, so you can see everything that went wrong at once rather than one error at a time, use the --continue option.

If you’ve listed an older version of gradle in your dependencies, you can build the wrapper with a specific version of gradle from the command line by specifying it after the wrapper command:

gradle wrapper –gradle-version-2.0

You can also specify gradle version in your build.gradle file in the wrapper task section:

task wrapper(type: Wrapper) {
    gradleVersion = '2.0'

If you have a device connected, you have some more relevant tasks:

Command Description
installs all debug builds
installs all release builds
uninstalls all debug builds
uninstalls all release builds
uninstalls all builds

If the project has multiple build flavors defined in build.gradle, you can specify which flavor you want to assemble in the following task format:

Command Description
assembles debug build for the specified flavor
assembles release build for the specified flavor
assembles all builds for the specified flavor
installs debug build for the specified flavor
uninstalls debug build for the specified flavor
installs release build for the specified flavor
uninstalls release build for the specified flavor

Note that there is no flavor-specific version of the build task. If you’re working with different flavors, you have to assemble and install them with two separate tasks.

If your build.gradle specifies a version of build-tools that is not installed, you will get an error when you try to call gradle for any task (even creating the gradle wrapper). You can fix this by either specifying an installed version of build-tools in build.gradle, or installing the specific version you need.

To install an old version of build-tools from the command line, first get an indexed list of available packages:

android list sdk –all

then to install the desired version:

android update sdk -u -a -t <index number of the desired version>

Specifying release signing information can be done in build.gradle as follows:

defaultConfig { ... }
signingConfigs {
    release {
        storeFile file("myreleasekey.keystore")
        storePassword "password"
        keyAlias "MyReleaseKey"
        keyPassword "password"
buildTypes {
    release {
        signingConfig signingConfigs.release

If you are more security-minded, you might prefer not to specify all of this in your build.gradle file. You can prompt the user to input this information with the following in your build.gradle, using System.console().readLine:

signingConfigs {
	release {
		storeFile file(System.console().readLine("\n\$ Enter keystore path: "))
		storePassword new String(System.console().readPassword("\n\$ Enter keystore password: "))
		keyAlias System.console().readLine("\n\$ Enter key alias: ")
		keyPassword new String(System.console().readPassword("\n\$ Enter key password: "))

Alternatively, you can store the information in your

RELEASE_STORE_FILE={path to your keystore}

You can specify a for your whole machine (~/.gradle/, or you can specify it in the project build directory, and you can access these in your build.gradle:

signingConfigs {
	release {
		storeFile file(RELEASE_STORE_FILE)

Common commands for managing your keystore from the command line:

Generate a Java keystore and key pair:

keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -keysize 2048

Generate a certificate signing request (CSR) for an existing Java keystore:

keytool -certreq -alias mydomain -keystore keystore.jks -file mydomain.csr

Import a root or intermediate CA certificate to an existing Java keystore:

keytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks

Import a signed primary certificate to an existing Java keystore:

keytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks

Generate a keystore and self-signed certificate:

keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048

Delete a certificate from a Java Keytool keystore:

keytool -delete -alias mydomain -keystore keystore.jks

Loading Conversation