Foreword
We have really learned Gradle
since this article . We have been explaining Groovy
grammar before for our better and faster learning.Gradle
1. Gradle
Explain basic concepts
Gradle
It is a project automation construction tool based on Apache Ant
and Apache Maven
concept. It uses a Groovy
domain-specific language based on ( DSL
) to declare project settings, abandoning the XML
various tedious configurations based on . At the same time, it is also a development framework. Based on the Groovy
language, let ’s look at Gradle
the composition.
It has its own grammar Groovy
and corresponding api
, so we can treat it Gradle
as a development framework rather than just a construction tool Flexibly according api
to 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 Android
project construction tool is Ant
that all eclipse
the students who have used it should have used it. Ant
The compilation rules are based xml
on, xml
we cannot write if/else
logical judgments like this.
2. Gradle
The execution flow
2. Gradle
Life cycle monitoring
We know that through the above Scheme gradle
implementation 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:Initialzation
andConfiguration
betweenafterEvaluate
: After the configuration phase is complete callback listener, namely: inConfiguration
andExecution
betweenbuildFinished
: The callback of the gradle execution is completed, that is:Execution
after
Let's create a new Android
project here . In the
next step, we will build.gradle
add the three methods we just wrote to our project , and output a sentence in each method.
We settings.gradle
also print a sentence in, we will say later in this file
OK, after the configuration is complete, we execute a simple command.We
want to test beforeEvaluate
this method. We need to cooperate with the subproject app
, we first add monitoring to our app
subprojectbuild.gradle
Then we project
are build.gradle
in together with a piece of code
and then the next we execute gradle -q
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