Gradle in Android: Essential Insights for Android Developer

Manish Kumar
4 min readOct 5, 2024

--

Introduction: What is Gradle?

Gradle is a powerful build automation tool for Android. It compiles your code, manages dependencies, and packages your app. Every time you click the Run button in Android Studio, Gradle resolves dependencies, compiles code, and generates the APK or AAB for your app. You can consider it similar to Sourcetree providing UI for git commands.

Gradle Migration: From Groovy to Kotlin DSL

Gradle now supports Kotlin DSL, allowing you to write your build scripts in Kotlin instead of Groovy. If you’re interested in migrating from Groovy to Kotlin DSL, you can refer to the official Android developer site for guidance: Migrate from Groovy to Kotlin DSL.

Gradle Folder Structure

In your Android project, there’s a Gradle folder that contains two important files:

  1. gradle-wrapper.jar:
  • This is a JAR file that allows you to run Gradle builds without needing to install Gradle on your machine.
  • It ensures everyone on the team uses the same Gradle version, so there are no differences in builds.

2. gradle-wrapper.properties:

  • This file tells your project which version of Gradle to use.
  • Example:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
  • This line means the project should use Gradle version 7.4 and will download it from the provided URL if it isn’t already installed.

What is the Gradle Wrapper?

So, Gradle Wrapper is like the Gradle SDK that you need, just like you need the JDK for Java. It helps you manage Gradle easily, ensuring everyone on your team is using the same version. This means:

  • Dynamic Management: You don’t have to worry about everyone installing Gradle themselves; the wrapper does it for you.
  • Consistency: All team members will build the project with the same version, reducing potential issues.

How to Use the Gradle Wrapper

When you click the Run button in Android Studio, it runs the command:

./gradlew build

This command builds your project using the version of Gradle specified in the gradle-wrapper.properties file.

Understanding Gradle in Android: The Three Core Files

Gradle in Android projects revolves around three main configuration files:

  1. app-level build.gradle.kts: Defines module-specific settings such as Android configurations, plugins, and dependencies.
  2. project-level build.gradle.kts: Manages global plugins and dependencies across all modules.
  3. settings.gradle.kts: Configures project structure and repositories.

Additionally, the libs.versions.toml file helps manage dependency versions in a central, organized way.

Breaking Down the App-Level build.gradle.kts

The app-level build.gradle.kts is where you configure everything specific to a module. It consists of three main sections(configuration blocks):

  • Plugins: Plugins are Third party libs that gradle uses to build your android project, such as Android and Kotlin.
  • Dependencies: While dependency contains third party libs that are used for app development for that particular module.
  • Android Block: Set Android-specific configurations like compile SDK, min SDK, and app versions.

Here’s an example:

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
compileSdk = 33
defaultConfig {
applicationId = "com.example.app"
minSdk = 21
targetSdk = 33
}
}
dependencies {
implementation("androidx.core:core-ktx:1.7.0")
}

Decoding the Project-Level build.gradle.kts

The project-level build.gradle.kts file defines settings that apply globally to all modules. It’s useful for managing shared configurations or plugins.

plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
}

This configuration specifies global plugins using aliases from libs.versions.toml without applying them directly to any module.

Breaking Down settings.gradle.kts

The settings.gradle.kts file handles project structure and repository management.

rootProject.name = "MyApp"
include(":app")

It also manages dependency repositories:

pluginManagement {
repositories {
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
}
}

Here, you define the repositories Gradle will use to fetch dependencies and plugins.

PluginManagementSpec- we can find a new function called repositories, it provides a RepositoryHandler argument inside. That is the place we place our repositories. From given repositories, Gradle will try to find libraries that are required by our project, and it will download the ones it finds.

dependencyResolutionManagement — provides the list of repositories where Gradle will search for dependencies that are necessary for our project. So app level gradle file if mentions any depdencies that will be searched in repository mentioned here.

Using libs.versions.toml for Version Management

The libs.versions.toml file is used to centralize version management for your dependencies. This approach keeps your versions consistent across multiple modules.

Example libs.versions.toml:

[versions]
kotlin = "1.7.10"
coroutines = "1.6.1"

[libraries]
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }

This simplifies your build configuration by allowing you to reference versions in one place, making updates easier.

Gradle’s Kotlin DSL brings type safety and improved readability to your Android project configurations. By understanding and organizing your build.gradle.kts files and leveraging libs.versions.toml, you can simplify dependency management and boost your project’s maintainability.

--

--

Manish Kumar
Manish Kumar

Written by Manish Kumar

Software Engineer with 9 years of experience in building mobile applications using Android, Java, Kotlin, Spring Boot and Flutter.

No responses yet