Android Gradle

In this article, we will understand Android Gradle. Android Gradle is a set of scripts that help you manage and automate the app-building process. Android Gradle is like a toolkit that consists of scripts that are essential for any android application.

Through this article, we will try to understand every aspect of android Gradle and focus on its roles during android development.

What is Android Gradle?

Android Gradle is a toolkit containing a set of scripts that help manage and automate the build process of an android application. Android Gradle is an open-source tool and helps in the following operations:

  • Building
  • Testing
  • Deploying
  • Automating
  • Managing, etc

So, Gradle is considered as a core file for any android development process. Grande’s flexibility allows you to do several tasks.

Why is Gradle needed?

Whatever we write while coding an android application is written in Kotlin(or Java) and XML. We say that as the source code of the application. Now, we need to convert this source code to an executable application. To do so, we need a tool, more precisely a build tool, that can automate the process to build the application from source code.

So, Gradle is the tool that helps in converting our .kt(or. java) and .xml files to a single executable APK file. Gradle is a build tool and consists of several tools that help convert the source to an intermediate .dex file and finally bind everything into a single executable APK file.

Role of Gradle in Android

  • It is like a JVM-based build system that helps in automating several tasks.
  • It plays a vital role in binding the source code, assets, XML files, and resources into a single APK file.
  • Gradle allows compilation, testing, deploying, and conversion of source code to .dex files.
  • It allows you to draw properties from external libraries and repositories.

What is a Build Tool?

The build tool is a collection of scripts that are ready to automate the process of converting the source code to an executable application. Build tool carries out many processes like compilation, testing, deploying, linking, and binding the source codes and resources together. These processes help in converting the source code to a runnable application.

One good example of a build tool is your Gradle.

To understand the processes the build tool carries out, see the below image.

Working of Android Gradle

Android Gradle works very much similar to how the build tool works. The below points show the working of Gradle more precisely.

  • First and foremost, the Kotlin Source code, resources, and AIDL(Android Interface Definition Language) files are bound together and compiled.
  • Simultaneously the Gradle file binds the several dependencies (Library Modules, AAR Libraries, JAR libraries) and makes it ready for compilation.
  • After the files are successfully compiled, a .dex file is generated, sent to the APK Packager.
  • The APK Packager finally combines everything and generates an executable APK file.

The whole process is automated by the Gradle build tool, and finally, when the Gradle build finishes, you have an executable APK.

Features of Android Gradle File

I hope until now you came to know what the Gradle file is and how it works. Now, let’s spend some time figuring out the features provided by a Gradle file in Android.

1. Build Tool – As discussed before, Gradle is a build tool and helps automate converting source code to an executable APK file.

2. Repositories – Gradle File draws many properties from the repositories. Some of the well-known android repositories are jcenter() and mavenCentral().

3. Dependencies – Dependencies are the libraries and modules from where Gradle draws many elements and functionalities. Gradle files allow you to have specific app-level dependencies. These dependencies are usually the library modules, AAR libraries, and JAR Libraries. However, Gradle also allows you to keep third-party libraries as dependencies.

4. Plugin Based System – Gradle is popularly known as a plugin-based system due to applying several types of plugins.

5. Default Configurations – Gradle scripts in android come with some by default configurations that are not to be altered until and unless highly required.

6. ProGuard Tool – Gradle build tool usually comes up with ProGuard tool that helps optimize, minify, and maintain the application’s security.

Types of Gradle File in Android

Now let’s see some of the crucial Gradle files that are present in any android project.

File Name Description
gradle-wrapper.properties The gradle-wrapper.properties check whether a proper SDK version is installed or not. If not, then the user is asked to download the required one. 
settings.gradle The settings.gradle contains the list of all the modules that are required by the project.
local.properties (in the SDK ) The local.properties task is to update the plugins with the SDK installation path. 
gradle.properties The gradle.properties define the configuration for the entire application. 

Types of Gradle.Build File

We have two types of gradle.build files present in Android, which are shown as follows;

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

Now, let’s see each of the gradle.build files in detail.

1. Top-Level build.gradle

The top-level build.gradle file is commonly known as the project-level gradle file. It is located in the parent directory of your project files, and it defines the configurations for all the modules to be used in the project.
It draws properties from some well-known android repositories. Below is an example of the Top-Level build.gradle file.

Code: Top_Level_Build_Gradle

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Below are the build configurations that are present in the Top-Level build.gradle file.

1. buildscript: The buildscript holds the repositories and dependencies of the Gradle tool. Please note that you shouldn’t include any application dependencies here. Those dependencies are required to be present at the Module Level Gradle file.

2. task clean(type: Delete): When the project is executed, this block removes the directory. It’s pretty important to maintain clean codes even after changes are made to configuration files.

2. Module-Level build.gradle

Module-Level build.gradle file is popularly known as the application level Gradle file. It is also located in the parent directory of the project files. It allows you to add third-party libraries, application dependencies, override settings, and declare the SDK versions.

Below is an example of the Module-Level Gradle File:

Code: Module_Level_Build_Gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.techvidvan.techvidvansqlite"
        minSdk 22
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Below are the build configurations for the Module Level build.gradle file.

a. android: It sets up the following build option:

compileSdkVersion: This specifies the application’s API level.

b. defaultConfig: This configuration will have the following details:

  • applicationId: It’s the app minSdk’s unique identifier. Version: It specifies the app’s minimum API version.
  • minSdkVersion: The minSdkVersion stores the minimum SDK version with which the application will be compatible.
  • targetSdkVersion: The targetSdkVersion stores the target SDK version on which we are building the application.
  • versionName: The version name is used to define the version of the app and is incremented whenever the app is updated.

c. buildTypes(release):

  • minifyEnabled: When set to true, shrinks the size of the APK file when Gradle builds it.
  • proguardFiles: The proguard files specify the setting for the proguard tool.

d. dependencies: The dependencies consist of module dependencies and libraries that are essential to building up your project.

Summary

Through this article, you came across Android Gradle and learned what it is and the role it plays in android development. Later on, you saw about the build tool and the working of a Gradle file in android. Moving further, you came across the features of the Gradle file and the types of Gradle files available in android. Finally, you saw the two types of build.gradle files along with their descriptions.