Skip to content
Book Cover of Extending Android Builds

1-1: Gradle And the Android Gradle Plugin ​

The process of building an Android application is based on the Gradle framework. But what is the Android Gradle Plugin (AGP)? And why are these two key players consistently mentioned when discussing Android development?

Gradle is a sophisticated build automation tool inspired by Apache Ant and Apache Maven. It offers three primary capabilities:

  • Automated Dependency Management: Gradle manages dependencies effectively, drawing inspiration from Maven.
  • Automated Task Management and Deployment: Gradle provides a comprehensive task management system, borrowing insights from the Ant framework.
  • User-friendly Scripts: Unlike Maven, which requires verbose XML scripts, Gradle allows you to write scripts using familiar, Turing-complete languages such as Groovy and Kotlin.

The Gradle team has taken the lead in developing build Plugins for multiple JVM languages, such as Java, Groovy, Scala, etc., by leveraging this efficient tool. The concept of Gradle Plugins on Gradle differs from what we typically see on other plugin-supporting platforms, where the platform retains a number of features explicitly aimed at end users even without plugins. Similar to a gaming console without cartridges, Gradle's functionality is futile without Plugins. The "cartridge" layer, which describes how to create an Android app and adjust its build process, is crucial for the majority of Android developers. This cartridge is the AGP, after all.

Kotlin
// A classic way to import a plugin to the build classpath,
// we will also see another approach in subsequent chapters.
classpath("com.android.tools.build:gradle:8.0.0")

Above is the Group, Artifact, and Version (GAV) coordinate of AGP. It comprises several modules:

Kotlin
plugins{
  id(com.android.application)
//  id(com.android.library)
//  id(com.android.dynamic-feature)
//  ...
}

In essence, AGP is a suite of tasks that orchestrate the Android application's build process:

  • It combines all dependent code and resources using Gradle's dependency API.
  • It utilizes the Android SDK's AAPT2 to manage resource compilation and output.
  • It employs Java and Kotlin compilers for codebase compilation and the Android SDK's Dex tool for .dex generation and optimization.
  • It utilizes the JDK's keytools or the Android SDK's APKSigner to sign the final apk.
  • And more...

By clicking the Run button of the Android Studio or executing a command like ./gradlew assembleDebug during daily development, the entire AGP automation process can be accessed. Nevertheless, as your project grows and the architectural evolution needs amplification, AGP may not meet all developer requirements, such as:

  • How to modify a resource file, or AndroidManifest.xml from libraries when there is a merge conflict?
  • How can performance monitoring be incorporated into certain method calls without modifying the source code?

To address these issues with a custom Gradle Plugin, developers will leverage not only the fundamental Gradle capabilities but also the AGP APIs. This implies first understanding the build process, then enhancing AGP's current tasks, including minor modifications for scripts or the development of a complex plugin. The journey of extending Android builds starts from here.