Reprint please indicate the source: blog.csdn.net/zhaoyanjun6… This article is from [Zhao Yanjun’s blog]

What is Gradle

  • Gradle is a project automation build tool based on Apache Ant and Apache Maven concepts.
  • It uses a Groovy-based domain-specific language (DSL) to declare project Settings, ditching the tedious XML-based configuration. Mainly for Java applications.
  • Currently, support is limited to Java, Groovy, Kotlin, and Scala, with plans for more languages in the future. Built based on Groovy scripts, its build scripts are written in groovy language.

For more information about gradle use, you can go to: Android Gradle use summary

Two: What is Groovy

Groovy is a dynamic language that is similar to Java (an improved version of Java, but with scripting features) and runs inside the Java Virtual machine. When a Groovy script is run, it is compiled into Java class bytecode, which is then executed by the JVM virtual machine.

For groovy, step here: Groovy uses full parsing

Gradle projects and Tasks

Each build consists of at least one project, and a project consists of one to multiple tasks. Each task represents an atomic action in the build process, such as compiling, packaging, generating javadoc, publishing, and so on.

Gradle: A project contains multiple tasks, and a task contains multiple actions

Project -- Task1 (Action1, Action2... -- Task2 (Action1, Action2...) -...Copy the code

4. Customize tasks

  • Format:
{//do some things}Copy the code
  • example

build.gradle

Hello task hello{println "hello world"} Name hello task(hello2){println "hello world2"} // define task, name hello3 task('hello3'){println "hello world3"}Copy the code
  • Run the gradle command on your terminal
Hello2 task gradlew hello2 task gradlew hello3 task gradlew hello3Copy the code

Create an Action

In the example above, is an informal task, informal because there is no action in the task being created. A Task is essentially a set of actions that are executed sequentially. An Action is a block of code, similar to a Method in Java.

Create the Action API

// Add Action Task doFirst(Action<? super Task> action); Task doFirst(Closure action); // Add Action Task doLast(Action<? super Task> action); Task doLast(Closure action); // Closure Action for Task leftShift(doLast) instead of Task leftShift(Closure Action); // Delete all Action Task deleteAllActions();Copy the code

Small example

build.gradle

Task hello {// Create an Action, DoFirst (new Action<Task>() {@override void execute(Task Task) {println "action1++++++++++"}}) DoFirst {println "action2++++++++++"}}Copy the code

Add two actions to the Action list, as shown below:

Run Hello Task: gradle Hello

Running results:

action2++++++++++
action1++++++++++Copy the code

LeftShift instructions

The function of leftShift is the same as doLast, adding an action to the end of the action list, but it is now obsolete. The official recommendation is to use doLast instead. Here’s a quick example:

build.gradle

Task hello {// Add an Action leftShift {println "+++++"}}Copy the code

LeftShift can also be succinct by using << instead, as shown below:

build.gradle

Task hello <<{// add an Action println "+++++" to the end of the Action list}Copy the code

When is an Action executed in a task?

Gradle life cycle

1. Initialization phase

Build a project instance: include ‘:app’, ‘:lib1’, ‘:lib2’.

2. Configuration phase

Gradle script of all projects will be executed to configure the project object. An object consists of multiple tasks. Tasks and related information will be created and configured at this stage.

3. Running phase

Execute dependent tasks based on the task name passed by the gradle command. The Task Action is executed in this phase.

Another way to create tasks

The gradle API provides other ways to create tasks. Here are the other two ways.

  • tasks

build.gradle

Tasks. create("hello2"){doFirst {println "hello2+++++"}}Copy the code
  • Custom subclasses of DefaultTask
Class MyTask extends DefaultTask {@taskAction void Action (){println "action1+++++"}} // Create task task Hello3 (type: MyTask){ doLast{ println "action2+++++" } }Copy the code

Run hello3 Task: gradlew hello3

The output

action1+++++ action2+++++

Task dependence

1, dependsOn

build.gradle

Task task1 << {println "I am task1----"} task task2 << {println" I am task2----"} DependsOn task1 before executing task2Copy the code

Perform task2

gradlew task2

perform

I’m Task1 — — I’m Task2 —

2, mustRunAfter

Two Task dependencies

Task task1 << {println "I am task1----"} task task2 << {println" I am task2----"} //task2 run task1 task2.mustRunAfter task1Copy the code
  • Run task1: gradlew task1

    I am a task1 – –

  • Run task2: gradlew task2

    I am a task2 – –

  • Run task1 and task2 simultaneously: gradlew task1 task2

    I’m Task1 — — I’m Task2 —

The three tasks depend on each other

build.gradle

Task task1 << {println "I am task1----"} task task2 << {println" I am task2----"} task task3 << {println "I am task3----"} task2.mustRunAfter task1 task3.mustRunAfter task1Copy the code
  • Run gradlew task1 task2 task3

    I’m Task1 — I’m Task2 — I’m Task3 —

  • Run gradlew task1 task3 task2

    I’m Task1 — I’m Task3 — I’m Task1 —

In the case of syntactic inconsistencies, the dependencies form a closed loop and the compiler reports an error

task1.mustRunAfter task2
task2.mustRunAfter task1Copy the code

3, shouldRunAfter

Forming a dependency is optional.

build.gradle

Task task1 << {println "I am task1----"} task task2 << {println" I am task2----"} task1. ShouldRunAfter task2Copy the code

Run: gradlew task1 task2

I’m Task2 — — I’m Task1 —

In the case of grammatical inconsistencies, dependencies form a closed loop that automatically breaks the closed loop. Not an error

Nine: System default task

Gradle provides many tasks to use by default, such as copy and delete

1, copy

build.gradle

Task Task name (type: Copy) {//action}Copy the code
  • The Api is introduced
Public AbstractCopyTask from(Object... Public AbstractCopyTask into(Object destDir) public AbstractCopyTask include(String... Public AbstractCopyTask exclude(String... Excludes) // Rename, Public AbstractCopyTask rename(String sourceRegEx, String replaceWith) // Delete files Project interface Boolean delete(Object... paths);Copy the code

Example:

  • Copy images: single data source
task copyImage(type: Copy) {
    from 'C:\\Users\\yiba_zyj\\Desktop\\gradle\\copy'
    into 'C:\\Users\\yiba_zyj\\Desktop'
}Copy the code
  • Copy images: Multiple data sources
task copyImage(type: Copy) {
    from 'C:\\Users\\yiba_zyj\\Desktop\\gradle\\copy' , 
         'C:\\Users\\yiba_zyj\\Desktop\\gradle\\copy'

    into 'C:\\Users\\yiba_zyj\\Desktop'
}Copy the code
  • Copy pictures: Filter files

Only files with the suffix.jpg will be copied

task copyImage(type: Copy) {
    from 'C:\\Users\\yiba_zyj\\Desktop\\gradle\\copy'
    into 'C:\\Users\\yiba_zyj\\Desktop'
    include "*.jpg" 
}Copy the code
  • Copy files: Filter and rename files
task copyImage(type: Copy) {
    from 'C:\\Users\\yiba_zyj\\Desktop\\gradle\\copy'
    into 'C:\\Users\\yiba_zyj\\Desktop'
    include "*.jpg"
    exclude "image1.jpg"
    rename("image2.jpg","123.jpg")
}Copy the code

File overwriting rules

Same file overwrite

Inheritance diagram of the Copy class

Copy (class) - AbstractCopyTask (Abstract class) (from, into, include, rename) -ConventionTask(Abstract class) - DefaultTask (class) - AbstractTask (Class) - TaskInternal - Task -Comparable<Task>, ExtensionAware -Project(delete)Copy the code

2, the Delete

  • Delete aaa files in the Android directory

build.gradle

Task deleteFile(type: Delete) {// Delete aaa file from Android directory Delete '.. /aaa' }Copy the code
  • Delete files from the desktop

build.gradle

Task deleteFile(type: Delete) {// Delete system Desktop Delete Delete "C:\ Users\ yiba_zyj\\Desktop\ gradle\ Delete "} task deleteFile(type: Delete) {// Delete system DesktopCopy the code

Ten: Tips

1. Name your task with a hump

build.gradle

task deleteFile{
    //do some things
}Copy the code

run

The equivalent gradlew dF is gradlew deleteFile

Run gradlew assembleRelease when you package, which can be abbreviated to gradlew aR

2. Common gradlew command

View all default built-in tasks of a project, excluding custom tasks

gradlew tasks

View all tasks (default task + custom Task)

Gradlew tasks – all

View information about a task, including the path, type, and description of the task

Gradlew help – a task taskName

View gradle version

gradlew -version

3, Add description to task

Task task1 << {description = "this is a description" println "I am task1----"}Copy the code

Gradle environment variable configuration

Gradlew is used to run tasks. Gradlew is used to run tasks. Running Gradle on a terminal will prompt you that Gradle is not an internal or external command, nor is it a runnable program or batch file.

'Gradle' is not an internal or external command, nor is it a runnable program or batch file.Copy the code

The official website to download: services.gradle.org/distributio…

After downloading, I decompressed the package and put it in the soft directory of DISK D. The environment variable

  • GRADLE_HOME

D: \ soft \ gradle – 4.3 – all



  • Path

D: \ soft \ gradle – 4.3 – all \ gradle – 4.3 \ bin

The resources

1. Android Gradle

2, Android Gradle use summary

Groovy uses full parsing

4, In-depth understanding of Android Gradle details


Personal wechat account: ZHAOyanjun125, welcome to follow