Android Gradle – Build Tool for Android Applications

Welcome back to DataFlair Android Tutorials. In this article, we’ll learn about Android Gradle plugin. We’ll see what what is Gradle in Android Studio and why it is necessary.

Android Gradle

Do you want to become an Android Professional? Join Android Telegram Channel

What is Android Gradle?

Gradle is actually an open-source build system. It is a tool that automates building, deployment, etc of a project. This tool is often used for Java Virtual Machine languages such as Java, Scala or Groovy. It is configured to do various tasks like testing, running, creation of documents, etc. Its strength is the flexibility that it provides to the developer.

For example, consider the task of copying a file from one directory to another. This task can be performed by Gradle build script even before the actual build process will happen.

What is a build tool?

There might be a question in your mind “what is this build tool”?

A build tool is nothing, but a set of programs that are used to automate the creation of executable applications from their source codes. Here creating the executable applications includes the task of compiling, testing, deploying, linking, and packing the codes into a usable form.
The build process here works as depicted in the following image:
Android App build process

What is the role of Gradle in Android?

See you know that every single android application development tool needs to compile resources, source codes, libraries and then combine them to an APK. Right? So, Android needs a build system to do these tasks and run the app. That is where the Android gradle.build system comes into play. It is the responsibility of the Gradle to perform the task of compilation, testing, deployment, and conversion of the code into a .dex file.

How Android Gradle works?

Another thing that would arise in your mind would be, “How does Gradle actually work in Android”?

So the answer to this is that the Android System supports Gradle as it builds automation tools out of the box. The build system of Android compiles the resources, the java source code and then packs them into an Application Package. This package can be tested, deployed, signed, and distributed by you.

Features of Gradle in Android

In Android, Gradle brings some really robust features to build android applications. A few of those powerful features are listed below:

  • Dependencies: These can be categorized as local or remote. Here local dependency is referring to the local file of the system. And remote dependency is referring to the remote server.
  • Sensible defaults: Gradle implements the concept of convention over configuration. That means Gradle has a set of sensible default configurations that are pre-defined.
  • Manifest Entries: Every project of Android Studio consists of a manifest.xml file that is associated with it. It has all the details about the application.
  • ProGuard Tool: Android Studio has support for the ProGuard tool. This tool optimizes the Java byte code and makes it efficient and powerful.

Gradle files

There are certain important files of Android Gradle that you must know. The following are a few of them.

Files Description
Gradle-wrapper.properties  It lets users build code, even if they don’t have Gradle installed.

It checks if the correct version is installed and downloads the required version if necessary.

settings.gradle It references all the modules that are required to make projects
local.properties

(in the SDK )

It tells the plugin where it can find the app SDK installation.
gradle.properties It contains the configuration information of the entire project.

Types of Gradle.build

Gradle.build scripts can be of two types, that are as follows:

  • Top-Level build.gradle
  • Module-level build.gradle

We’ll now see these one by one-

1. Top-Level build.gradle

This type supports various build configurations such as:

a. Build script: It configures the repositories and dependencies for Gradle.
 classpath ‘com.android.tools.build:gradle:3.0.1’
This line will add the plugin as a classpath dependency for Gradle 3.0.1.

b. All projects: In this block, you can configure all the third party libraries or plugins.

c. Task clean(type:Delete): Its task is to delete the file every time, the project runs.

2. Module-level Gradle.build

It supports the following types of configurations:

a. android: It configures the specific build option:

  • compileSdkVersion: It defines the API level of the application.

b. defaultConfig: It will have the following configurations details:

  • applicationId: It is the unique Id for app
  • minSdkVersion: It defines the minimum API version for the app.
  • targetSdkVersion: It defines the API level used while testing the app
  • versionCode: It defines the version code of the application.
  • versionName: It defines the version name of the application.

c. buildTypes(release): It has two things:

  • minifyEnabled: It enables code shrinking for release
  • proguardFile: It specifies the proguard setting files.

d. dependencies: It specifies the dependencies that we need for the project.

Summary

In this Android tutorial, we have learned a lot of things about Gradle in Android. Let us recall them in a nutshell. We saw what is Android gradle, why Gradle is required, its role and how Gradle works in Android. Then we saw the important files of Gradle. We also went through the features that it brings in Android. In the end, we saw the important types of gradle.build that can be used.

I hope you liked it and I hope that you understood it. Do not forget to share your feedback in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.