Introduction to Gradle and Android Gradle Plugin: Build System Basics
This article introduces the fundamentals of Gradle as a universal build automation tool and explains how the Android Gradle Plugin integrates with it, covering project structure, build files, tasks, the three build lifecycle phases, DSL syntax, and provides example Groovy/Kotlin scripts for configuring Android applications.
Welcome to the first article of the MAD Skills series on Gradle and the Android Gradle Plugin (AGP). This guide explains how the Android build system works and introduces the basics of Gradle.
Gradle is a general‑purpose automation build tool that can build Android projects as well as any other software. It supports single‑ or multi‑project builds, using build.gradle in each module and a settings.gradle file at the root to list included projects.
Tasks are the fundamental units of work in Gradle. You can view the list of tasks via the command line or the Android Studio Gradle panel. AGP defines its own set of tasks that are executed in a specific order when building Android apps.
Gradle build files are written using the Gradle DSL, which can be expressed in Groovy or Kotlin. The DSL parses the android block in build.gradle to create objects such as ApplicationExtension and BuildType .
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}The Android Gradle Plugin is configured with an android block where you define SDK versions, build types, signing configs, and other options.
android {
compileSdk 31
defaultConfig {
applicationId "com.example.myapp"
minSdk 21
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 are declared in the dependencies block, and Gradle can resolve them from Maven, Ivy, or local file repositories.
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}Gradle’s build lifecycle consists of three phases: Initialization (determines which projects are included), Configuration (evaluates all build scripts, applies plugins, registers tasks), and Execution (runs the required tasks). The Configuration phase runs for every task invocation, so time‑consuming work should be avoided there.
The article concludes by summarizing that Gradle and AGP provide many customization points, and the reader now understands tasks, build phases, AGP configuration, and basic DSL usage. The next article will explore extending builds with AGP’s Variant API.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.