The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Gradle and Tasks

A build.gradle file teaches Gradle how to execute tasks, such as how to compile an Android project. Outside of a Gradle-aware IDE like Android Studio, you use Gradle itself to run these tasks. If you have installed your own copy of Gradle, you would use the gradle command; if you are relying upon a trusted copy of the Gradle Wrapper, you would use the ./gradlew script in your project root.

For the purposes of this book, the gradle command will be shown – just substitute ./gradlew where you see gradle if you are using the Gradle Wrapper script.

Key Build-Related Tasks

To find out what tasks are available to you, you can run gradle tasks from the project directory. That will result in output akin to:

> Task :tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for each variant.
sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleRelease - Assembles all Release builds.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'MyApplication'.
components - Displays the components produced by root project 'MyApplication'. [incubating]
dependencies - Displays all dependencies declared in root project 'MyApplication'.
dependencyInsight - Displays the insight into a specific dependency in root project 'MyApplication'.
dependentComponents - Displays the dependent components of components in root project 'MyApplication'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'MyApplication'. [incubating]
projects - Displays the sub-projects of root project 'MyApplication'.
properties - Displays the properties of root project 'MyApplication'.
tasks - Displays the tasks runnable from root project 'MyApplication' (some of the displayed tasks may belong to subprojects).

Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallRelease - Uninstalls the Release build.

Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintRelease - Runs lint on the Release build.
lintVitalRelease - Runs lint on just the fatal issues in the release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
testReleaseUnitTest - Run unit tests for the release build.

This list is dynamically generated based on the contents of build.gradle, notably including tasks defined by the com.android.application plugin.

In principle, you are supposed to specify the entire task name when running that task. However, you can use shorthand, so long as it uniquely identifies the task.

Probably the most common task that a developer will use, at least in the short term, is installDebug (or iD for short). This will build a debug version of the app and install it on an available device or emulator. This roughly corresponds to ant install debug for those familiar with legacy Ant-based command-line builds.

Just as there is installDebug, there can also be installRelease. The Debug and Release portions of the task are not hard-coded, but rather are derived from the “build types” defined in the build.gradle file. The concept, role, and usage of build types will be covered in the next chapter. However, installRelease is not available by default, because installing an app requires that the APK be signed, and the Android Gradle Plugin does not know how to sign it. We will address this in the next chapter as well.

If you just want to build the app, without installing it, assembleDebug (aD) or assembleRelease (aR) will accomplish that aim. If you want to uninstall the app from a device or emulator, uninstallDebug (uD) and uninstallRelease (uR) should work.

Discussion of other tasks, such as the “check” tasks, will be covered in later chapters.

Results

All build output goes into a build/ directory. Specifically, your APKs will go into build/outputs/apk, with different APK editions based upon whether you did a debug or release build.

Note that Gradle has a clean task that wipes out the build/ directory.