Android Gradle Learning Series (5) -Gradle Life Cycle Exploration

Foreword

We have really learned Gradlesince this article . We have been explaining Groovygrammar before for our better and faster learning.Gradle

1. GradleExplain basic concepts

GradleIt is a project automation construction tool based on Apache Antand Apache Mavenconcept. It uses a Groovydomain-specific language based on ( DSL) to declare project settings, abandoning the XMLvarious tedious configurations based on . At the same time, it is also a development framework. Based on the Groovylanguage, let ’s look at Gradlethe composition.
Insert picture description here
It has its own grammar Groovyand corresponding api, so we can treat it Gradleas a development framework rather than just a construction tool Flexibly according apito the requirements in the realization of the construction process provided by it , write the construction rules of the project like writing a script. We know that the previous Androidproject construction tool is Antthat all eclipsethe students who have used it should have used it. AntThe compilation rules are based xmlon, xmlwe cannot write if/elselogical judgments like this.

2. GradleThe execution flow

Insert picture description here

2. GradleLife cycle monitoring

We know that through the above Scheme gradleimplementation process in three steps, then we can not monitor every step of the completion status of it? The answer is yes, here corresponding to the three methods beforeEvaluate, afterEvaluate,buildFinished

  • beforeEvaluate: Configuration phase before the start of callback listener, namely: Initialzationand Configurationbetween
  • afterEvaluate: After the configuration phase is complete callback listener, namely: in Configurationand Executionbetween
  • buildFinished: The callback of the gradle execution is completed, that is: Executionafter

Let's create a new Androidproject here . In the
Insert picture description here
next step, we will build.gradleadd the three methods we just wrote to our project , and output a sentence in each method.

Insert picture description here

We settings.gradlealso print a sentence in, we will say later in this file

Insert picture description here
OK, after the configuration is complete, we execute a simple command.We
Insert picture description here
want to test beforeEvaluatethis method. We need to cooperate with the subproject app, we first add monitoring to our appsubprojectbuild.gradle

Insert picture description here
Then we projectare build.gradlein together with a piece of code
Insert picture description here
and then the next we execute gradle -q
Insert picture description here
us through another set of methods can monitor the life cycle

// 与 this.beforeEvaluate {} 一样
this.gradle.beforeProject {}
// 与 this.afterEvaluate {} 一样
this.gradle.afterProject {}

The usage is similar, so I won't write examples here, readers can verify it by themselves

Published 87 original articles · Like 319 · Visit 1.49 million +

Guess you like

Origin blog.csdn.net/Greathfs/article/details/102809350