Gradle build process (gradle life cycle)

Just after the Spring Festival, I have been decadent for more than ten days. I feel that I don’t know anything anymore. I should learn something to enrich myself
. ...

There are three steps in total:

1. Initialization phase

Probably gradle needs to collect the projects and sub-projects built this time, increase the hierarchy and create project instances for each project (the most related to this is the setting.gradle file)

2. Configuration stage
This stage is to execute the build.gradle file under each project

3. Execution phase

The main thing is to execute the task dependency graph generated in the second stage

How do we need to prove this?
Open a project and find the setting.gradle file in the root directory

gradle comes with life cycle code

gradle.addBuildListener(new BuildListener() {
    @Override
    void settingsEvaluated(Settings settings) {
        println("[left-cycle]初始化阶段已经完成")
    }

    @Override
    void projectsEvaluated(Gradle gradle) {
        println("[left-cycle]配置阶段已经完成")
    }

    @Override
    void buildFinished(BuildResult buildResult) {
        println("[left-cycle]工程构建结束")
    }
    @Override
    void buildStarted(Gradle gradle) {

    }
    @Override
    void projectsLoaded(Gradle gradle) {

    }
})

First prove the first point, let's print a log

 To prove the second point, we add another log to build.gradle in the app directory

 At this point we use the command to execute

 It can be seen that in the first stage, the code in the setting is indeed executed first. When the code in the setting is executed, the initial stage is completed, and the second stage is started. The code in the build.gradle under the app is executed and then the configuration stage it's over

So the third stage?

We need to create a task to demonstrate

 Then execute the gradle command again to go through the process

It can be seen that the third point will be implemented after the reconfiguration phase is completed.

I personally write a blog because I am afraid that I will forget what I have learned, so I will record it here.

Guess you like

Origin blog.csdn.net/lwh1212/article/details/122865476
Recommended