Anyone who has developed an Android application knows the importance of Gradle. As the default Android build tool, Android developers have to deal with Gradle on a daily basis. Most of gradle’s functions are extended by plugins. For example, the most commonly used plugin is the official Android plugin com.android.application, which is used to do some Android-related configurations.
What if we want to develop a Gradle plugin ourselves? Here I want to share with you two articles, this article mainly introduces the concept of Gradle plug-in, the next article with an example to show how to write a gradle plug-in step by step.
To understand what Gradle plug-ins do, first understand what Gradle is.
gradle
Gradle is a build tool that controls how code is compiled, built, and packaged, much like Make in C/C++ projects. Gradle executes a build in three steps:
- Initialization Initialization is performed
settings.gradle
The script file determines which subprojects are included in the current project. The Android project determines which modules are included in the project at this stage. - Configure Configuration executes in the project
build.gradle
Script file to create all tasks that need to be created. - Execute Execute executes the specified Task.
It seems that everything gradle wants to do is finally being passed on to tasks. Let’s see what a Task is.
Task
The basic unit of operation in our common programming languages is a block of code or method, executed in the order in which it is called. Gradle is different. Its work is executed by Task after Task. A Task can specify which Task it depends on, or that it should run before or after another Task.
Splitting the processing logic into different tasks has two advantages:
- Business decoupling helps maintain and improve code robustness.
- Incremental compilation, when the input/output of a Task does not change, it does not need to run again.
So how do you create a Task? There are two common ways:
- in
build.gradle
In. - Created through plug-ins.
Build. gradle usually creates simple tasks. Complex tasks are usually created by plug-ins. Otherwise, build.gradle files become bloated. Visible plug-ins are also an important part of Gradle. Let’s take a look at what plug-ins do.
The plug-in
Gradle’s core logic is simple and its rich build capabilities are extended by plug-ins. For example, the Android build logic is not the official Gradle code, but Android wrote the corresponding Gradle plugin to implement. This feature allows gradle to be flexible. For example, to compile in C, you only need to write a plugin.
As an Android developer, the two most common plug-ins are:
com.android.application
com.android.library
The usual way to use a plugin in a build.gradle file is this:
apply plugin: 'com.android.application'
Copy the code
Library means that the module is a library Module. The com.android.library plug-in does different things to this module. As for how to configure this, we won’t expand it here, but will cover it later when we talk about how to write a plug-in. When you apply a plug-in, you execute a string of code blocks.
What is the Gradle plugin? So why do we need to develop gradle plug-ins? Feeling has little to do with app development.
Why develop gradle plug-ins?
As an Android developer, I might think that all I have to do is make cool interfaces that switch seamlessly and don’t lag. Why bother with Gradle development?
From my own experience, learning to develop Gradle plug-ins has several benefits:
- Encapsulating a set of Gradle operations in a plug-in facilitates code reuse and avoidance
build.gradle
The file is bloated. - Third-party libraries encapsulate their Gradle operations in plug-ins and provide them to other projects in package-dependent form.
- Plug-ins can be written in Java/Kotlin to make development easier for developers unfamiliar with Gradle code.
- Improving your understanding of the Android build process will help you learn about Android hot pudding, plugins, and more.
Now that we understand what a Gradle plug-in is and why we want to create a Gradle plug-in, let’s click here for a second article to see how to create a Gradle plug-in step by step.