gradle plug-in and execution process analysis

gradle plug-in and execution process analysis


In Android Studio, projects are built using Gradle, so how does the Gradle plugin we usually use execute?

Gradle execution steps

Simply put, gradle is a build tool used to control the process of compiling, building, and packaging code, a bit like the Make tool in a C/C++ project. Gradle executes a build and can be divided into three steps in total:

1. Initialization phase

Read the include information in setting.gradle in the root project, determine which projects in the current project are added to the build, and create a project instance.

For example:

include ':app', ':moudle1', ':moudle2'

When building Gradle, it will read the include information in setting.gradle in the root project, decide which projects to join in the build, and create project instances. For example, in the example, three projects are involved in the build.

2. Configuration phase

Execute the build.gradle script file in the project to configure the project object. An object consists of multiple tasks. This stage will also create and configure tasks and related information.

3. Implementation phase

According to the task name passed by the gradle command, execute related dependent tasks.

How to use the Gradle plugin

Gradle plugins are generally used in the following ways:

  • Write directly in the gradle file in the project. The disadvantage of this method is that the plug-in code cannot be reused, and the code has to be copied again in other projects.
  • Write the plug-in in an independent project, then publish it to the central warehouse, and then directly reference it. The advantage is that it can be reused.

project 和 tasks

Let's look at two important concepts: project and tasks.

The two important concepts in grade are project and tasks. Each build is completed with at least one project. Note that the project here and the project in Android studio are not a concept. Each project has at least one tasks. Each build.grade file represents a project. tasks are defined in build.gradle. When initializing the build process, gradle will gather all projects and tasks based on the build file. A task contains a series of actions, and then they will be executed in order. An action is a piece of executed code, which is somewhat similar to the concept of a method. .

Task

The work of gradle is executed by each Task. A Task can specify the Task it depends on, or that it should run before or after another Task.

Dividing processing logic into different Tasks has two advantages:

  1. Business decoupling helps maintain and improve code robustness.
  2. Incremental compilation, when the input/output of the Task does not change, it does not need to be run again, but can be reused directly.

There are two common ways to create a Task:

  1. Created directly in build.gradle.
  2. Created by a plugin.

Tasks with simple functions are generally created in build.gradle, and tasks with complex logic are usually created by plug-ins, otherwise the build.gradle file will be bloated. It can be seen that the plug-in is also an important part of gradle, let's take a look at what the plug-in does.

plug-in

The core logic of gradle is relatively simple, and the rich building functions are extended through plug-ins. For example, Android's construction logic is definitely not built into the official gradle code, but Android has written a corresponding gradle plug-in to implement it. This feature guarantees the flexibility of gradle functionality.

In Android, the two most common plugins are:

  1. com.android.application
  2. com.android.library

So how to use the plugin? It is usually used in the build.gradle file:

apply plugin: 'com.android.application'

This means that the module is an app module, and using the com.android.library plug-in means that the module is a library module, and the two plug-ins will configure the module differently.

Well, we have understood the main theoretical knowledge of gradle and gradle plug-in. In the next chapter, we will analyze its use in Android studio.


**PS: For more exciting content, please check --> "Android Performance Optimization"
**PS: For more exciting content, please check --> "Android Performance Optimization"
**PS: For more exciting content, please check- -> "Android Performance Optimization"

Guess you like

Origin blog.csdn.net/u011578734/article/details/113438443