Welcome to the first article of the MAD Skills series , Gradle and Android Gradle plugin API In this article, we will understand how the Android build system works and the basics of Gradle.
We will start from the build phase of Gradle, discuss how to customize your build using AGP (Android Gradle Plugin) configuration options, and discuss how to keep your build efficient. If you prefer to learn about this content through video, please view it here
By understanding the working principle of the build phase and the configuration method of configuring the Android Gradle plugin, we can help you customize the build based on the needs of the project. Let's go back to Android Studio and take a look at how the build system works.
Gradle Introduction
Gradle is a general-purpose automated build tool. Of course, you can use Gradle to build Android projects, but in fact you can use Gradle to build any type of software.
Gradle supports single or multi-project builds. If you want to configure your project to use Gradle, you need to add a build.gradle file in the project folder.
In a multi-project hierarchy, the root project will contain a settings.gradle file, which lists other projects included in the build. Android uses multi-project builds to help you modularize applications.
△ Android project structure and build.gradle and settings.gradle files
Thanks to the existence of plugins, Gradle can handle different types of projects, such as Android or Java. These plugins will contain predefined functions for configuring and building specific types of projects.
For example, in order to build an Android project, you need to configure your Gradle build file using the Android Gradle plugin. Regardless of whether the current Android project is an application or a dependent library, the Android Gradle plugin knows how to build and package it.
Task
Gradle's build process revolves around a unit of work called Task. You can view the Task list through the terminal, or view the tasks by enabling the Task list in the Android Studio Gradle panel.
△ Gradle Task list
These tasks can receive input, perform certain operations, and generate output based on the operations performed.
Android Gradle Plugin defines its own tasks and knows the order in which these tasks need to be executed when building an Android project.
The Gradle build file consists of many different parts. Gradle's configuration syntax is called Gradle DSL , which defines the way for developers to configure plugins. Gradle will parse build.gradl
e file and create AGP DSL objects, such as ApplicationExtension
and BuildType
.
A typical Android project will contain a top-level Gradle build file. Each module in the Android project has a Gradle build file. In the sample project, I only have one application module.
build.gradle
file in the module layer, I need to declare and apply the plug-ins needed to build the project. In order to let Gradle know that I am building an Android project, I need to apply the com.android.application
or com.android.library
plugin. These two plugins respectively define how to configure and build Android applications and dependent libraries. In this example, I want to build an Android application project, so I need to apply the com.android.application
plug-in. Since I need to use Kotlin, the kotlin.android
plug-in is also used in the example.
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
Android Gradle Plugin provides its own DSL, you can use it to configure AGP, and make this configuration apply to Task at build time.
To configure the Android Gradle Plugin, you need to use the android block. In this code block, you can define SDK version, tool version, application details, and other configurations for different build types (such as debug or release). To learn more about how gradle uses this information to create variants, and what other options you can use, see the build document :
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'
}
}
In the next section, you can define dependencies. Gradle's dependency management supports compatible with 161c9c6eae48b6 Maven and Ivy , as well as local binary files from the file system.
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'
}
build stage
Gradle evaluates and runs the build in three stages, namely Initialization, Configuration and Execution. For more information, please refer to Gradle document .
In the Initialization phase, Gradle will determine which projects are included in the build and will create Project instances for each project. In order to determine which projects will be included in the build, Gradle will first look for settings.gradle to determine whether this is a single-project build or a multi-project build.
In the Configuration phase, Gradle will evaluate all the build scripts included in the build project, then apply plugins, configure the build using DSL, and register Tasks at the end while lazily registering their inputs.
It should be noted that no matter which Task you request to execute, the configuration phase will be executed. In order to keep your build simple and efficient, please avoid performing any time-consuming operations during the configuration phase.
Finally, in the Execution phase, Gradle will execute the set of tasks required for the build.
In the next article, we will analyze these stages in depth when writing our own plugin.
Gradle DSL supports the use of Groovy and Kotlin scripts to write build files. So far, I have used Groovy DSL scripts to configure the construction of this project. You can see the same build files written by Kotlin and Groovy respectively below. Note that the suffix of the Kotlin script file name is ".kts".
△ Comparison of Kotlin and Groovy scripts
Migrating from Groovy to Kotlin or other methods of configuring scripts will not change the way you execute tasks.
summary
The above is the whole content of this article. Gradle and Android Gradle Plugin have many features that allow you to customize the build. In this article, you have learned the basics of Gradle Task, build phases, configure AGP, and build with DSL configuration.
Stay tuned for the next article, we will show you how to use AGP's Variant API to extend your build when writing your own plug-ins.
Welcome to click here to submit feedback to us, or share your favorite content or problems found. Your feedback is very important to us, thank you for your support!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。