What is the Android Gradle Plugin

W

Gradle has been the official Android build tool for quite some time now. It was introduced alongside Android Studio. Before the 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.

Photo by Bia Andrade on Unsplash

What is Gradle?

The first thing we need to define and understand how Gradle works. 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.

https://docs.gradle.org/current/userguide/userguide.html

Build Automation

From the above, it is important to understand what build automation is. 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.
Photo by Jo Szczepanska on Unsplash

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.

https://docs.gradle.org/current/userguide/plugins.html

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.
Photo by Mike Petrucci on Unsplash

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.

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:

rootProject.name='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 Hello I’m Nik ? 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.

Photo by Bilal O. on Unsplash

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.

Until next time!

Evana Margain Puig

About the author

Evana Puig

Add Comment

By Evana Puig

Evana Puig

Get in touch

Mobile Developer expert in Android and iOS, with 10 years of experience. Visit me at evanapuig.com. Author, and topic master at raywenderlich.com