How Gradle Plugin Works

2021, Sep 03    

This article provides a summary on Android gradle plugin core use cases and talks about how the Android gradle plugin works via reading through the source code.

Screenshot 2022-07-01 at 12 02 07 PM

1. Basic UseCases

Taking reference from the official Android Studio User Guide, Android gradle plugin that extends the capabilities of a gradle project model and further provides the full sets of the function needed for Android build system, which:

  • helps to build the source set to Android application bundles, aars and in the process, which deeply integrates with Android development tool chain to
  • helps to organizes test cases for both unit testing and integration testing (UI automation testing).
  • helps to release & sign the build products.
  • support customized build configurations with concepts of build variants (which is indeed the combination of buildType and flavour).

Besides of that, since the android gradle plugin is based on gradle Project model, the plugin just provides extended android specific build character based on the gradle build system. For all the above use cases mentioned, they are well documented and explained in Android Studio User Guide

2. Core Concepts In Gradle

There are some critical concepts in Gradle system, the official gradle websites provides quite lengthy documentation, here I will try to make a summary:

  • Project: this is the core of the gradle build system, during compilation, each build.gradle will be correlated to a project model. all the blocks like allProjects , artifacts, subprojects are parsed and declare project capabilities, besides of those default blocks of Project model, default plugins provides extra DSL extensions like: publishing from publishing plugin, signing from signing plugins.
  • Script: during build phase, files like build.gradle and setting.gradle are mapped to a Script instance, which provides util methods to facilitate script execution. and the content of the build script are also binded to a gradle Project object as the first point I mention. To fully understand the gradle build lifecycle, can read the article here
  • Plugins: Plugin is the main entry for supporting customized build types. Developers can further provide customized DSL definition via plugins and relevant tasks.
  • Extension: support customized gradle DSL and define the operational models inside it.
  • Tasks: As the name indicates, the finest granularity of a reusable execution which can be chained, depended on.

3. How Android Plugin Works

To study how the gradle plugin works, the source code for Android gradle build plugin is downloaded from the project repo

There is also a faster way of downloading the source code to project, you can create a single jar module and introduce the android gradle plugin in the following way:


apply plugin: 'groovy' 
apply plugin: 'maven'  

dependencies {
    implementation gradleApi() //must
    implementation localGroovy() //must

    implementation 'com.android.tools.build:gradle:3.4.1'

}

repositories {
    mavenCentral() //must
}

And then in the external library directory you can directly browse the gradle android plugin source code:

The project is quite complicated and there are a lot details in it, but when just read the surface layer and we can draw out the module class diagram in a rough granularity depicted as following:

The base plugin and the base extention class provides the most part of the jobs including aggregation on tasks/buildOptions/sourceDir configurations. The subclasses of them just further declares the future bindings between the type of plugin and extensions.

The plugin has the following responsibility:

  • create tasks via task manager: based on flavours to create corresponding tasks.
  • create databinding builder: based on databinding characters generate dynamical databinding classes.
  • create android builder: the core class which builds the android architects.
  • create sdkHandler, locate and install dependencies
  • apply java.plugin as the base dependency.

The extension has the following responsibility:

  • declare all the relevant DSL extension point

4. References:

TOC