Gralde 的生命周期LifeCycle 和回调Hook

Gradle 生命周期 三大阶段 Three phases

1. Initialization 初始化

Gradle starts up and locates the build files it must process. Crucial during this phase is the determination of whether the build is single-project or multi-project. If it is a single project build, Gradle identifies a single build file to pass to the next phase. If it is a multi-project build, Gradle locates potentially many build files for processing in the next phase.

Notes:

此阶段 会确认项目中所有模块的静态配置文件及Gradle 构建脚本文件。

 

2. Configaration 配置

During configuration, Gradle executes each build file as a Groovy script. The effect of configuration is not the actual execution of build actions—that comes next—but rather the creation of a directed acyclic graph (DAG) of task objects.1 It is during the construction of this graph that many of the hook methods run.

Notes:

此阶段首先会加载项目中所有定义的task,包括单项目工程和多项目工程。 然后会从Gradle的执行命令开始, 根据task之间的依赖关系,从所有tasks 中找到一个构建所需要执行的有向无循环图 (DAG)。

注意, 有些插件Plugin在构建DAG的过程中,在Groovy 层面检查本地的依赖配置文件,否则会强制出错。例如华为的HMS 插件配置,所以自定义task配置HMS本地文件(agconnect-services.json)的hook时机只能在构建gradle graph 之前,和Evaluated 之后。

 

3. Execution

During this phase, Gradle identifies the tasks in the task DAG that must be executed, and executes them in dependency order.2 All build activities (e.g., compiling code, copying files, uploading artifacts, etc.) occur during execution. Some build hooks are evaluated during execution.

 

主要的生命周期回调

Gralde 的生命周期LifeCycle 和回调Hook

 

Gralde 的生命周期LifeCycle 和回调Hook

 

参考 Reference