What is Gradle in Android Studio?

Gradle is a build system running on Android Studio.

In other languages for example:

  • Ant and Maven of Java
  • Rake of Ruby
  • A-A-P of C
  • NAnt of .NET
  • Make in Linux

It's the new build tool that Google wants to use for Android. It's being used due to it being more extensible, and useful than Ant. It is meant to enhance developer experience.

You can view a talk by Xavier Ducrohet from the Android Developer Team at Google I/O here.

There is also another talk on Android Studio by Xavier and Tor Norbye, also during Google I/O here.


Short Answer

Gradle is a build system.

Long Answer

Before Android Studio you were using Eclipse for your development purposes, and, chances are, you didn't know how to build your Android APK without Eclipse.

You can do this on the command line, but you have to learn what each tool (dx and AAPT) does in the SDK. Eclipse saved us all from these low-level, but important, fundamental details by giving us their own build system.

Now, have you ever wondered why the res folder is in the same directory as your src folder?

This is where the build system enters the picture. The build system automatically takes all the source files (.java or .xml), then applies the appropriate tool (e.g., takes .java class files and converts them to .dex files), and groups all of them into one compressed file - our beloved APK.

This build system uses some conventions: an example of one is to specify the directory containing the source files (in Eclipse it is \src folder) or resources files (in Eclipse it is \res folder).

Now, in order to automate all these tasks, there has to be a script; you can write your own build system using shell scripting in Linux or batch files syntax in Windows. Got it?

Gradle is another build system that takes the best features from other build systems and combines them into one. It is improved based off of their shortcomings. It is a JVM-based build system. That means you can write your own script in Java, which Android Studio makes use of.

One cool thing about Gradle is that it is a plugin-based system. This means if you have your own programming language and you want to automate the task of building some package (output like a JAR file for Java) from sources, then you can write a complete plugin in Java or Groovy (or Kotlin, see here), and distribute it to the rest of the world.

Why did Google use it?

Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little-to-no learning curve, and without learning Groovy or any other new language. So they wrote the Android plugin for Gradle.

You must have seen build.gradle file(s) in your project. That is where you can write scripts to automate your tasks. The code you saw in these files is Groovy code. If you write System.out.println("Hello Gradle!"); then it will print on your console.

What can you do in a build script?

A simple example is that you have to copy some files from one directory to another before the actual build process happens. A Gradle build script can do this.


Here is a detailed explanation of what Gradle is and how to use it in Android Studio.

Exploring the Gradle Files

  1. Whenever you create a project in Android Studio, the build system automatically generates all the necessary Gradle build files.

Gradle Build Files

  1. Gradle build files use a domain-specific language or DSL to define custom build logic and to interact with the Android-specific elements of the Android plugin for Gradle.

  2. Android Studio projects consists of one or more modules, which are components that you can build, test, and debug independently. Each module has its own build file, so every Android Studio project contains two kinds of Gradle build files.

  3. Top-Level Build File: This is where you'll find the configuration options that are common to all the modules that make up your project.

  4. Module-Level Build File: Each module has its own Gradle build file that contains module-specific build settings. You'll spend most of your time editing module-level build file(s) rather than your project's top-level build file.

To take a look at these build.gradle files, open Android Studio's Project panel (by selecting the Project tab) and expand the Gradle Scripts folder. The first two items in the Gradle Scripts folder are the project-level and module-level Gradle build files

Top-Level Gradle Build File

Every Android Studio project contains a single, top-level Gradle build file. This build.gradle file is the first item that appears in the Gradle Scripts folder and is clearly marked Project.

Most of the time, you won't need to make any changes to this file, but it's still useful to understand its contents and the role it plays within your project.

Module-Level Gradle Build Files

In addition to the project-level Gradle build file, each module has a Gradle build file of its own. Below is an annotated version of a basic, module-level Gradle build file.

Other Gradle Files

In addition to the build.gradle files, your Gradle Scripts folder contains some other Gradle files. Most of the time you won't have to manually edit these files as they'll update automatically when you make any relevant changes to your project. However, it's a good idea to understand the role these files play within your project.

gradle-wrapper.properties (Gradle Version)

This file allows other people to build your code, even if they don't have Gradle installed on their machine. This file checks whether the correct version of Gradle is installed and downloads the necessary version if necessary.

settings.gradle

This file references all the modules that make up your project.

gradle.properties (Project Properties)

This file contains configuration information for your entire project. It's empty by default, but you can apply a wide range of properties to your project by adding them to this file.

local.properties (SDK Location)

This file tells the Android Gradle plugin where it can find your Android SDK installation.

Note: local.properties contains information that's specific to the local installation of the Android SDK. This means that you shouldn't keep this file under source control.

Suggested reading - Tutsplus Tutorial

I got a clear understanding of Gradle from this.