Install Gradle
Gradle is a build package manager that helps dependency management when building android apps. Compare it with Ant, maven, npm, rake etc.
Download and Install
Download the binary from here . It is an archive, so extract the contents to a folder on your machine.
Below is where it looks after extracted. The version being used is 2.7, however download the latest in your case. There are not too many differences that will affect your android app build
Set Gradle shell variables
Add the below to your ~/.profile so that gradle binary is available in $PATH and $GRADLE_HOME is available when building Android app
Android Studio with Gradle
Android Studio comes bundled with gradle and uses its own internally. If you are developer, you might find it more comfortable to use in-built so that you can do have to manage yet another tool.
Since we want the flexibility to do with an IDE and do it in the CI way (headless), we will point Android Studio to use the gradle we downloaded previously as below
Quick Commands
gradle --help
will give you the options available
Gradle Tasks
Gradle works by executing tasks. There can be
- in-built tasks: comes by default with gradle
- custom tasks: craft your own tasks
For now, we will use in-built tasks to start with and it is beyond the scope of this tutorial to go into details of writing custom tasks
In-built tasks are categorized into
- Android Tasks
- Build Tasks
- Build Setup Tasks
- Help Tasks
- Install Tasks
- Verification Tasks
- Other Tasks
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task
Build app
Generally gradle looks for a build.gradle file in the current directory and parses the file to follow instructions
In this book we will build the app using the following command that runs inside the Android project root directory
gradle clean assembleDebug
We will go into details of building app from project in subsequent section
Note If you do not want to worry about managing gradle versions (downloading, extracting and ensuring the right version is used), you can use gradle wrapper. Gradle wrapper comes bundled with gradle. The only change is instead of using "gradle" binary, use "gradlew" binary in the same folder