This is the fourth day of my participation in Gwen Challenge
Preface:
Create an Android project using Groovy as Gradle’s language by default. We can also change to the familiar Kotlin language and write it as a script
Groovy DSL script files use the.gradle suffix, which kotlin needs to change to.gradle.kts for scripting languages
First, Gradle scripts are configuration scripts. When the script executes, it configures an object of a specific type. For example, when a build script executes, it configures an object of type Project. This object is called the script’s delegate object. The following table shows the delegates for each Gradle script type.
The script type | Proxy objects |
---|---|
Build script | Project |
Init script | Gradle |
Build script | Settings |
We use methods in the corresponding proxy object when configuring the script
Build scripts consist of multiple statements and script blocks. Statements can include method calls, attribute assignments, and local variable definitions. Script blocks are method calls with closures as arguments. A closure is considered a configuration closure that configures a number of delegate objects at execution time. The top-level script blocks are listed below.
function | role |
---|---|
allprojects { } | Configure this project and each of its subprojects |
buildscript { } | If the build script needs to use external libraries, you can add them to the classpath of the script that builds the script itself |
dependencies { } | Add project dependencies |
repositories { } | Project repositories such as JCenter |
sourceSets { } | Configure the source of the project |
subprojects { } | Configure the subproject of this project |
artifacts { } | Published configuration |
What is the difference between BuildScript repositories and AllProjects repositories?
The declarations in BuildScript are resources that gradle scripts need to use themselves. Resources that can be declared include dependencies, third-party plug-ins, Maven repository addresses, and more. The dependencies, repository addresses, and other information declared directly in the build.gradle file are resources needed by the project itself
Gradle project build life cycle
Gradle’s life cycle is divided into three phases: init, configration, and excute.
-
Initialization phase
Gradle determines which projects need to be initialized by setting.gradle, loads the build.gradle file for all the projects that need to be initialized, and creates a Project object for each Project
-
The configuration phase
Execute the build.gradle script under each section, complete the project configuration, and construct the Task dependency diagram to execute the code in the task according to the dependencies at execution time
3. Implementation phase
Through the Task dependency diagram of the configuration phase, the action code in the Task is executed sequentially, that is, the action code in the Task written in doFirst and doLast: the code that is executed only when the Task is called
Related articles
Gradle movement of a