Android Gradle Customizing Task description

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/zhaoyanjun6/article/details/76408024
This article is from "Zhaoyan's blog"

One: What is Gradle?
    • Gradle is a project automation building tool based on the Apache Ant and Apache maven concepts.
    • It uses a particular domain-specific language (DSL) based on groovy to declare the project settings, discarding the tedious configuration of XML-based. Java-oriented applications.
    • Currently its supported languages are limited to Java, Groovy, Kotlin, and Scala, and plans will support more languages in the future. Based on groovy scripting, its build script is written in the groovy language.

About Gradle related applications, you can: Android gradle use summary

II: What is Groovy

Groovy is a dynamic language that runs in Java virtual machines, similar to Java (a version of Java, but with a scripting language). When the groovy script is run, it is compiled into Java class bytecode and then executes the Java bytecode class from the JVM virtual machine.

To learn about groovy, go here: Groovy uses full parsing

III: Gradle's Project and Tasks

Each build (build) consists of at least one project, and one project consists of one or more tasks. Each task represents an atomic operation 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 Action

project                   -- task1 (Action1、Action2...)                  -- task2 (Action1、Action2...)                  
Four: Custom Task
    • Format:
 task 任务的名字 {    //do some things }
    • Example

Build.gradle

//定义 task , 名字 hello task hello{    println "hello world"}//定义 task,名字 hello task(hello2){    println "hello world2"}//定义 task,名字 hello3 task (‘hello3‘){    println "hello world3"}
    • Run the Gradle command at the terminal
//执行 hello taskgradlew hello//执行 hello2 taskgradlew hello2//执行 hello3 taskgradlew hello3
V: Create action

In the example above, an informal task is said to be informal because there is no action in the created task. Task is essentially a set of action objects that are executed sequentially, and the action is actually a block of code, similar to a method in Java.

Create an Action-related API
 //在Action 队列头部添加Action Task doFirst(Action<? super Task> action); Task doFirst(Closure action); //在Action 队列尾部添加Action Task doLast(Action<? super Task> action); Task doLast(Closure action);     //已经过时了,建议用 doLast 代替 Task leftShift(Closure action); //删除所有的Action Task deleteAllActions();
Small example

Build.gradle

//创建一个名字为hello的 task task hello {    //创建一个 Action , 添加到 Action 列表的头部   doFirst(new Action<Task>() {       @Override       void execute(Task task) {           println "action1++++++++++"       }   })    //创建一个 Action , 添加到 Action 列表的头部    doFirst {        println "action2++++++++++"    }}

Added two action to the action list, as shown in the action list:

Run the Hello task:gradle hello

Operation Result:

action2++++++++++action1++++++++++
Leftshift description

Leftshift, like Dolast, adds an action at the end of the action list, but is now obsolete, and the official suggests replacing it with Dolast. Here's a small example:

Build.gradle

task hello {    //在 Action 列表尾部添加一个 Action     leftShift {        println "+++++"    }}

Leftshift also has a concise notation, in << lieu, as follows:

Build.gradle

task hello <<{    //在 Action 列表尾部添加一个 Action     println "+++++"}

So the question is, when does the Action in the task execute?

Six: Gradle life cycle 1. Initialization phase

Will read the include information in Setting.gradle in the root project, decide which projects to join the build, create project instances, such as the following three projects: include ': App ', ': Lib1 ', ': Lib2.

2. Configuration phase

is going to execute Build.gradle scripts for all projects, configure Project objects, and one object consists of multiple tasks,
This phase also creates, configures, and related information for tasks.

3. Operation phase

Performs dependent tasks based on the task name passed over by the Gradle command. The Action of the Task is executed at this stage.

VII: Another way to create a Task

The basic way to create a task is explained above, in fact the Gradle API gives us other ways to create a task, and the other two methods are explained below.

    • Tasks

Build.gradle

//创建 hello2 tasktasks.create("hello2"){    doFirst {        println "hello2+++++"    }}
    • Custom Defaulttask Subclasses
class MyTask extends DefaultTask {        @TaskAction    void action(){        println "action1+++++"    }}//创建 hello3 tasktask hello3 (type: MyTask){    doLast{       println "action2+++++"    }}

Run Hello3 Task:gradlew hello3

Output

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

Eight: Task dependent 1, dependsOn

Build.gradle

task task1 << {    println "我是task1----"}task task2 << {    println "我是task2----"}//task2 依赖 task1, 执行task2之前先执行task1task2.dependsOn task1

Executive Task2

Gradlew Task2

Execution effect

I'm task1----
I'm task2----

2. Mustrunafter Two task dependencies
task task1 << {    println "我是task1----"}task task2 << {    println "我是task2----"}//task2 运行之前先运行task1task2.mustRunAfter task1
    • Execute Task1:gradlew Task1

      I'm task1----

    • Execute Task2:gradlew Task2

      I'm task2----

    • Simultaneous execution of Task1, Task2:gradlew Task1 task2

      I'm task1----
      I'm task2----

Three task interdependencies

Build.gradle

task task1 << {    println "我是task1----"}task task2 << {    println "我是task2----"}task task3 << {    println "我是task3----"}task2.mustRunAfter task1task3.mustRunAfter task1
    • Execute Gradlew task1 task2 task3

      I'm task1----
      I'm task2----
      I'm TASK3----

    • Execute Gradlew task1 task3 task2

      I'm task1----
      I'm TASK3----
      I'm task1----

In the case of grammatical contradictions, the dependency relationship forms a closed loop, and the compiler will error

task1.mustRunAfter task2task2.mustRunAfter task1

3, Shouldrunafter

Forming dependencies is optional.

Build.gradle

task task1 << {    println "我是task1----"}task task2 << {    println "我是task2----"}task1.shouldRunAfter task2

Run:gradlew task1 task2

I'm task2----
I'm task1----

In the case of grammatical contradictions, the dependency relationship forms a closed loop, and the closed loop is automatically broken. No error.

Nine: System default task

Gradle provides a number of tasks to use by default, such as copy, delete

1. Copy

Build.gradle

task 任务的名字 (type: Copy) {    //action }
    • API Introduction
//数据源目录,多个目录public AbstractCopyTask from(Object... sourcePaths)  //目标目录,单一public AbstractCopyTask into(Object destDir) //过滤文件 包含public AbstractCopyTask include(String... includes)//过滤文件 排除public AbstractCopyTask exclude(String... excludes)//重新命名,老名字 新名字public AbstractCopyTask rename(String sourceRegEx, String replaceWith)//删除文件 Project 接口boolean delete(Object... paths);
Small example:
    • Copy Picture: Single Data source
task copyImage(type: Copy) {    from ‘C:\\Users\\yiba_zyj\\Desktop\\gradle\\copy‘    into ‘C:\\Users\\yiba_zyj\\Desktop‘}
    • Copy pictures: 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‘}
    • Copying pictures: Filtering files

Only files with a. jpg suffix are copied

task copyImage(type: Copy) {    from ‘C:\\Users\\yiba_zyj\\Desktop\\gradle\\copy‘    into ‘C:\\Users\\yiba_zyj\\Desktop‘    include "*.jpg" }
    • Copy files: Filter files, rename
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")}

File Overwrite rules

Same file overwrite

Inheritance diagram for Copy class

Copy (类)   - AbstractCopyTask (抽象类)  (from、 into、 include、rename)      -ConventionTask(抽象类)       - DefaultTask (类)        - AbstractTask (抽象类)           - TaskInternal (接口)            - Task(接口)                      -Comparable<Task>, ExtensionAware(接口)                -Project(接口)    (delete 方法)
2. Delete
    • Remove Android Catalog AAA files

Build.gradle

task deleteFile(type: Delete) {    //删除Android 更目录的aaa 文件    delete ‘../aaa‘  }
    • Delete files on the desktop

Build.gradle

task deleteFile(type: Delete) {    //删除系统桌面 delete     delete "C:\\Users\\yiba_zyj\\Desktop\\gradle\\delete"}
Ten: Little tricks 1, the name of the custom task with the Hump name method

Build.gradle

task deleteFile{    //do some things}

Run

Gradlew DF equivalent Gradlew deletefile

When gradlew assembleRelease you are ready to run, you can simply writegradlew aR

2. Common Gradlew Commands

View all of the project's default self-contained tasks, excluding custom tasks

Gradlew Tasks

View All Tasks (default task + custom Task)

Gradlew Tasks--all

View information about a task that contains the path, type, description, and so on

Gradlew Help--task TaskName

View Gradle version

Gradlew-version

3. Add a description to the task description
task task1 << {    description = "这是一段描述信息"    println "我是task1----"}
11: Gradle Environment variable Configuration

In the above introduction, the way to run a task is gradlew to use, then how do we use it gradle . If you run at the terminal gradle , you will be promptedgradle 不是内部或外部命令,也不是可运行的程序或批处理文件。

‘gradle‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。

Official website Download: http://services.gradle.org/distributions/

After the download is complete, I unpacked the compressed package in the soft directory of the D-Drive.
Environment variables

    • Gradle_home

D:\soft\gradle-4.3-all

    • Path

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

Resources

1, Android Gradle necessary practical tips

2, Android gradle use summary

3. Groovy uses full parsing

4, in-depth understanding of Android Gradle Detailed

Personal number: zhaoyanjun125, welcome attention

Android Gradle Customizing Task description

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.