Reading Time: 4 minutes

Gradle has been the official Android build tool for quite some time now. It was introduced alongside with Android Studio. Before Android Gradle plugin, Android apps were built in Eclipse with ANT, another build tool. But even though, it’s one of our basic everyday tools as Android developers, sometimes we don’t know what is behind it. Let’s learn what is behind this powerful tool.

What is Gradle?

The first thing we need to define to understand how it works is what is Gradle. Let’s review the definition in Gradle’s official page:

Gradle is an open-source build automation tool focused on flexibility and performance. Gradle build scripts are written using a Groovy or Kotlin DSL. Read about Gradle features to learn what is possible with Gradle.
Photo by Jo Szczepanska on Unsplash

Build Automation

From the above, it is important to understand what is build automation. It is the complete process of building software, done by a program without our interaction. Some of the processes that happen in it are compiling your code into binary, packaging and, running automated tests.

There are two types of build automation tools.

  1. Build automation servers – As the name states, they are online tools that make builds on their own in a scheduled way or through manual triggering. You can commonly see these tools when working on big projects. For example, builds can run at night and in the morning you have a new version of all the merged changes.
  2. Build automation utilities – This is the group to which Gradle belongs, they are installed in a local machine and you create builds manually.

Why is it a Plugin?

Gradle explains it very well in their site:

Gradle at its core intentionally provides very little for real world automation. All of the useful features, like the ability to compile Java code, are added by plugins

In short, the Android Gradle plugin contains all the actions that are executed to build an Android app. If you didn’t have the plugin and gradle files, you would have to run a lot of commands one by one in the terminal. Also, if you did all those commands by hand you would take more time creating your project, as Gradle knows how to optimize and reuse this information.

What does the Android Gradle Plugin do in Android Studio?

 Basically, it runs the process that bundles all the resources (images, layout XML files, string resources, etc), your Kotlin or Java source code, any libraries added to your project.

Note: It is important to understand that Gradle in itself is not the compiler, linker or bundler itself. It is just the tool that supervises the compilation, and other processes that happen to get the final Android executable.

It turns everything into .dex files. Behind the scenes, Android uses a Virtual Machine called Dalvik. Dex stands for Dalvik Executable. The most recent versions of Android have migrated into a system called ART, short for Android Runtime. Although there are slight differences between Dalvik and ART, both run Dex files.

In the end, Gradle outputs an APK file, which is Android’s main executable. Inside that APK, you can find a bundle of Dex files and other resources used by Android. Similar to a zip file with a bunch of other files inside, but with Android’s file types.

Photo by Mike Petrucci on Unsplash

This may sound very simple, and it happens in a few minutes, but it really involves a lot of work and optimization to get to this point. If we didn’t have an efficient plugin, builds could take a lot of time and our development would slow down considerably.

One Settings.gradle File

This file is just for mapping all the ‘modules’ used in your app. By default when you create an app in Android Studio it has only one module called app, so usually the settings.gradle file looks like this:'EvanaApp'
include ':app'

In big applications, you end up breaking down your code into multiple modules, and probably they are not called “app”. In a case like that, you would end up having multiple includes in your settings.gradle file.

A common example where you may have several modules is when developing an app for a phone and another for a TV. In this case, each one may have its own module but they can both be included in the same project.

Photo by Bilal O. on Unsplash

Project-level build.gradle file

If you have developed an app for Android, it is likely you have noticed that most projects have two Gradle files. If you haven’t you can check the most basic example in my New Android Studio project for Kotlin and Java – Tutorial.

When adding libraries or dependencies, you sometimes add them to the App Gradle and sometimes to the Project Gradle.

This file is the one you usually find as build.gradle(Project: EvanaApp). For each project, regardless of if it has one or many modules, you will have only one project-level build.gradle. The configurations applied in this file will be inherited to every module listed in your settings.gradle file.

Module-level build.gradle file

This file is the one you find by default with the name build.gradle(Module: app). For each module inside of a project, you will have one. This means you will have as many module-level files, as you have modules listed in your settings.gradle file.

Wrapping up

This is the basic breakdown of the logic behind Gradle Plugin in Android. I hope you find this article useful. Let me know in the comments below if you have any questions. Join my email list at the bottom if you would like to receive more content like this!

Until next time!

Evana Margain Puig

(Visited 7 times, 1 visits today)