The introduction

Last time, we looked at the Gradle life cycle

Gradle starts by initializing a Project, then configuring the topology of the task, and then executing each task based on the topology

In this stage, we will learn about projects in Gradle modules

concept

Project is an important concept in Gradle. It is the entry point of Gradle API

Here is a demo project directory as follows:

From our programmer’s perspective, we think of “DaviPlu” as engineering and “app” and “lifecycle-plugin” as modules

In Gradle, however, this is not the case. ‘DaviPlu’, ‘app’ and ‘lifecycle-plugin’ are all considered projects as shown below (after executing ‘./ Gradlew Projects’) :

We can see that executing the “./gradlew projects “command will print out the current Gradle Project and notice that it is organized as a tree (root Project is” DaviPlu “and other sub-projects).

Gradle: Gradle: gradle: gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle: Gradle

Each sub-project will have an output, for example: “app” is an application type, then the output is APK; The “lifecycle- Plugin” is a lib-like output that is an AAR or JAR

The Project API family

There are a lot of project-related apis, but they can be roughly divided into the following categories:

1) the task API

- Ability to add or modify tasks for the current projectCopy the code

2) Property API

- project Ability to add additional attributesCopy the code

3) Project API

- Ability to operate and manage parent and child projectsCopy the code

4) Gradle life cycle

- the last time with learning, see: https://juejin.cn/post/6960834289755750408Copy the code

5) the file API

- and began to study the basic groovy file file operations, see: https://juejin.cn/post/6959482085509693476#heading-8 - mainly operating current gradle relevant documentsCopy the code

6) other

- Relatively piecemeal, for example: Gradle adds dependencies/configurations/imports external files etcCopy the code

Project API

Mainly: the ability to operate and manage parent and child projects

Every project has a build.gradle file, which is then compiled into the bytecode of the project, so everything written inside the build.gradle file is actually written inside the project class

Suppose we have a demo project whose dependencies are:

DaviPlu

  • app
  • lifecycle-plugin

There are three projects, so there are three build.gradle files

Next, we will learn about the API in each of the three build.gradle files

1) Get all the projects

Code (same code in all 3 Gradle files) :

this.getAllprojects().eachWithIndex { Project entry, int i ->
        println "[getAllprojects().eachWithindex -->] + entry.name
        println "[getAllprojects().eachWithIndex-->" + i
    }
Copy the code

Execution Result:

【 DaviPlu 】

GetAllprojects ().eachWithindex --> DaviPlu getAllprojects().eachWithindex --> getAllprojects().eachWithindex --> DaviPlu getAllprojects().eachWithindex -->0GetAllprojects ().eachWithindex --> = getAllprojects().eachWithindex --> = getAllprojects().1GetAllprojects ().eachWithindex --> Lifecycle -plugin [getAllprojects().eachWithindex -->]2
Copy the code

“App”

GetAllprojects ().eachWithindex --> = getAllprojects().eachWithindex --> = getAllprojects().0
Copy the code

【 lifecycle – plugin 】

GetAllprojects ().eachWithindex --> Lifecycle -plugin [getAllprojects().eachWithindex -->]0
Copy the code

2) Obtain the parent project

Code (same code in all 3 Gradle files) :

 if (this.getParent() == null) {
        println "I am the root project, so I have no parent project!! "
    } else {
        println "Get parent project:" + this.getParent().name
    }
Copy the code

Execution Result:

【 DaviPlu 】

I am the root project, so I have no parent project!!Copy the code

“App”

Get the parent project: DaviPluCopy the code

【 lifecycle – plugin 】

Get the parent project: DaviPluCopy the code

3) Get the root Project

Code:this.getRootProject().name
Copy the code

4) Root Project manages apis related to sub-projects

if (this.getParent() == null) {
        println "I'm a project!! "

        project('app', { Project p ->
            println "Operation in root project:" + p.name
            // This is actually inside the gradle file in app, you can operate it freely
        })

        project('lifecycle-plugin', { Project p ->
            println "Operation in root project:" + p.name
            // This is actually inside the gradle file in app, you can operate it freely})}Copy the code

5) Configuration of the current node project and all sub-projects

 allprojects {
     println "[allprojects] project configuration and all sub-projects configuration -->" + project.name
 }
Copy the code

6) Configuration of all sub-projects (excluding the current project)

 subprojects {
   println "[subprojects] Configuration of all subprojects (excluding the current project) -->" + project.name
}
Copy the code

7) Whether xx plug-in exists in the current project

if (project.plugins.hasPlugin('com.android.application')) {
        println "[project. Plugins. HasPlugin 】 the application of the project have plug-ins.."
}
Copy the code

At the end

Haha, that’s all for this article (systematic learning and growing together)