Gradle is a Groovy-based DSL for building projects. The advantage over Ant and Maven is that it is a language that can do specific logic and is much more flexible

Gradle life cycle

The Gradle lifecycle is divided into three phases:

1. Initialization phase: Generate project objects

2. Configuration phase: Task objects are generated and the task topology is completed

3. Execution phase: Execute tasks and all dependent tasks

Lifecycle related apis:

BeforeEvalute {//doSomething} // After the configuration is complete, that is, after the task topology is generated. This. afterEvalute{//doSomething} // After the execution of gradle is complete, After the task to perform this. Gradle. BuildFinished {/ / doSomething} / / equivalent to this. BeforeEvaluate enclosing gradle. BeforeProject {} AfterEvaluate this.gradle.afterproject {} // The gradle script content is executed at the initialization stageCopy the code

Project

In AndroidStudio, each Module is a Project, and each Module has a corresponding build.gradle file

The Project related API

GetSubProjects () // getRootProject this.getrootproject () // get parent Project Project Project ('app') {Project Project -> println project.name apply plugin: 'com.android.application' android{// related configuration}} // Configure all Project allprojects {// Configure all Project group 'com.breeze' version Subprojects' 1.0.0} {the apply the from 'common. Gradle} / / judge whether library engineering project. The plugins. HasPlugin (' com. Android. Library)Copy the code

The Project attribute API

Each Project has an Ext to hold its own defined attributes

writing

Ext {Version = '1.0.0' compileVersion = 28Copy the code

Note: The default subproject attributes of the root Project are inherited, so when you want to configure attributes for all projects, you only need to use the root Project configuration in the Project. You can extract the configuration into a file. Import the file in the root Project and extract the configuration to common.gradle on Android

ext { android = [ compileSdkVersion : 28, applicationId : "com.sq.sqpackartifact", minSdkVersion : 19, targetSdkVersion: 28, versionCode: 1, versionName: "1.0"] libs = [kotlinStdLib: "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version", supportAppCompatLib : "Com. Android. Support: appcompat - v7:28.0.0", okhttpLib: "com. Squareup. Okhttp3: okhttp: 3.12.0", okhttpLogLib: 'com. Squareup. Okhttp3: logging - interceptor: 3.8.1', gsonLib: "com. Google. Code. Gson: gson: 2.8.5", retrofitLib: "Com. Squareup. Retrofit2: retrofit: 2.5.0", retrofitRxJavaLib: "Com. Squareup. Retrofit2: adapter - rxjava: 2.5.0", retrofitGsonLib: "Com. Squareup. Retrofit2: converter - gson: 2.5.0", rxAndroidLib: IO. Reactivex: rxandroid: 1.2.1, rxKotlinLib: "IO. Reactivex: rxkotlin: 1.0.0"]}Copy the code

Import the file in the root project

apply from: 'common.gradle'
Copy the code

Used in sub-project:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation rootProject.ext.libs.kotlinStdLib
    implementation rootProject.ext.libs.supportAppCompatLib
    //okhttp
    implementation rootProject.ext.libs.okhttpLib
    implementation rootProject.ext.libs.okhttpLogLib

    //gson
    implementation rootProject.ext.libs.gsonLib
    //retrofit
    implementation rootProject.ext.libs.retrofitLib
    implementation rootProject.ext.libs.retrofitRxJavaLib
    implementation rootProject.ext.libs.retrofitGsonLib
    //rx
    implementation rootProject.ext.libs.rxAndroidLib
    implementation rootProject.ext.libs.rxKotlinLib
    //download
    implementation project(path: ':download')
}
Copy the code

Task

Define the Task

Method one:

Task helloTask (group:'breeze', description:'task study') {println 'hello'}Copy the code

Task (‘taskName’, closure); task(TaskContainer); task(” TaskContainer “)

    this.tasks.create(name: 'helloTask2') {
        setGroup('breeze')
        setDescription('task study'')
        println 'i am task'
    }
Copy the code
Task Life Cycle
task A {
    println 'task a config'
    doFirst {
        println 'task a doFirst'
    }
    doLast {
        println 'task a doLast'
    }
}
Copy the code

The order of execution in the above code is config->doFirst->doLast, and config is executed when the Task is configured in the second step of the project lifecycle

Task dependent on

This is A DependsOn method of A task executed by Gradle when executing build. Task B will be executed first if task A is executed using DependsOn

DependsOn :'B' {doLast{println 'task B execute'}} Task B {doLast{println 'task B execute'}} task B (dependsOn:'B') {doLast{println 'task B execute'}  A { doLast { println 'task a execute' } } task B { doLast{ println 'task b execute' } } project.afterEvaluate { tasks.getByName('A').dependsOn('B') }Copy the code

MustDoAfter, ShouldDoAfter, and DependsOn only affect the execution order

task A {
    doLast {
        println 'task a execute'
    }
}

task B {
    shouldRunAfter 'A'
    doLast {
        println 'task b execute'
    }
}
Copy the code

Task A and task B can still be executed separately, but shouldRunAfter should not execute task A before task B, but when all tasks are executed, the order of execution will change. MustRunAfter is similar to shouldRunAfter when running gradlew A B and shouldRunAfter when running gradlew B B. The only difference is that shouldRunAfter is not so strict and can be ignored when running gradlew A B