The introduction
In the last stage, we learned a simple introduction to Groovy, gradle’s basic language.
Gradle series: Variable definitions for Groovy syntax
2) Gradle Series: Groovy
Gradle Series: Groovy programming Basics 2
Next, we will talk about groovy language foundation, to carry out our gradle learning journey ~
Gradle overview
Gradle is mainly used to build applications
For example, android’s default build uses Gradle
Gradle can be thought of as a programming framework consisting of the following parts:
1Groovy is the main language in Gradle. For example, Java/Kotlin is the main language for android development2Build script blocks are unique to Gradle3Gradle API This is the Gradle API, such as: Task /setting etcCopy the code
As you can see above, Gradle has its own language and API, so you can think of Gradle as a programming framework
Gradle has an advantage over ant/ Maven, which can only be configured in XML, but gradle has its own API that allows us to customize our development and create a unique build process
For example, gradle can write if-else statements, but Maven cannot
Gradle advantage
(1) Good flexibility
Ant/Maven builds cannot be modified during build. Gradle can
For example, when building android APK, you can use Gradle to build apK with the specified name. Maven, etc., cannot, can only be changed into APK, and then renamed to change
(2) Finer granularity, easy adjustment (refined to each task)
When ant/ Maven builds, the build script is independent and does not know what is going on inside the tool. Finally, the code/resources etc. are packaged to produce an executable.
However, Gradle goes from compiling source code, compiling resources, to producing the final product (e.g. Apk), are generated through a series of tasks, one after another, and the source of the task is open source, as long as you understand these, you can make the code compilation process clear, while you can modify the task in the process of dynamic change construction process
For example, the Tinker framework of wechat generates patch files in the process of apK construction through Gradle technology
(3) High scalability, support plug-in mode
Plugin support means that you can reference existing Gradle plug-ins, just as your code can directly reference third-party libraries
For example: apply plugin: ‘com.android.application’
(4) Good compatibility
Gradle is compatible with all the features of Ant/Maven builds. In other words, Gradle takes advantage of other build tools
Gradle life cycle
The general process is as follows:
1(Initialization Initialization phase2) Configuration Phase3It's an Execution stageCopy the code
During Initialization, all projects in the whole Project are analyzed and their corresponding Project objects are constructed
In the Configuration phase, tasks in all projects objects are parsed and topologies of all tasks are constructed, as shown in the following:
The Execution of a specific task or its dependent task is executed in the order of completing the dependent task before executing itself
Listen for gradle’s life cycle
1) Before the Configuration phase
this.beforeEvaluate {
println "BeforeEvaluate: Configure phase before >>>>"
}
Copy the code
2) After the Configuration phase
this.afterEvaluate {
println "AfterEvaluate: After the configuration phase is complete, all tasks form a topology and execute >>>> one by one during the execution phase."
}
Copy the code
3) Gradle execution is complete
this.gradle.buildFinished {
println "BuildFinished: Gradle completes execution >>>>>>"
}
Copy the code
PS: Because there are many life cycles, I don’t want to expand them here. For details, see Gradle API
At the end
Haha, that’s all for this article (systematic learning and growing together)