• admin

Gradle from the Command Line

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:

CommandDescription

wrapper

creates gradle wrapper

assembleDebug

assembles all debug builds

assembleRelease

assembles all release builds

clean

deletes build directory

build

builds and tests current project

tasks

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:

CommandDescription

installDebug 

installs all debug builds

installRelease

installs all release builds

uninstallDebug

uninstalls all debug builds

uninstallRelease 

uninstalls all release builds

uninstallAll

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:


CommandDescription

assemble[flavorName]Debug

assembles debug build for the specified flavor

assemble[flavorName]Release

assembles release build for the specified flavor

assemble[flavorName]

assembles all builds for the specified flavor

install[flavorName]Debug

installs debug build for the specified flavor

uninstall[flavorName]Debug

uninstalls debug build for the specified flavor

install[flavorName]Release

installs release build for the specified flavor

uninstall[flavorName]Release

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 gradle.properties:

RELEASE_STORE_FILE={path to your keystore}
RELEASE_STORE_PASSWORD=password
RELEASE_KEY_ALIAS=MyReleaseKey
RELEASE_KEY_PASSWORD=password

You can specify a gradle.properties for your whole machine (~/.gradle/gradle.properties), 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)
		storePassword RELEASE_STORE_PASSWORD
		keyAlias RELEASE_KEY_ALIAS
		keyPassword RELEASE_KEY_PASSWORD
	}
}

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
201 views

About Us

Blog

Subscribe

Privacy Policy

SDG Systems

330 Perry Hwy.

Suite 200

Harmony, PA 16037

 

+1.724.452.9366

info@sdgsystems.com

© 2020 by SDG Systems, LLC. All rights reserved.