[Android Pro] Gradle Tips#1-tasks

Source: Internet
Author: User

Reference to:http://trickyandroid.com/gradle-tip-1-tasks/

http://blog.csdn.net/lzyzsd/article/details/46934187

Today's talk is about the configuration and operation of Gradle tasks and task. Perhaps some readers do not yet understand Gradle task, and it should be easier to understand with real examples. The following code shows three Gradle tasks, which will explain the differences later.

    task MyTask {        println "Hello, world!"    }    Task MyTask {        Dolast {            println "Hello, world!"        }    }     < < {        "Hello, world!"    }

My goal is to create a task that will print out "Hello, world!" when it executes. When I first created the task, I guess it should be written like this:

Task MyTask {        println "Hello, world!"    }

Now, try to execute this mytask, enter Gradle mytask on the command line, and print as follows:

    user$ gradle mytask    Hello, world!    : MyTask up-to-date

This task looks like it's working. It prints "Hello, world!".
But it's not exactly what we expected. Now let's see why. Enter Gradle tasks on the command line to see all available tasks.

    user$ gradle Tasks    Hello, world!    : Tasks    ------------------------------------------------------------ All    tasks runnable from root project    ------------------------------------------------------------    Build Setup Tasks    -----------------    init-initializes a new Gradle build. [incubating]    ..........

Wait, why "Hello, world!"? Did you print it out? I just want to see what tasks are available and do not perform any custom task!
The reason is simple, Gradle task has two main phases in its life cycle: the configuration and execution phases.
Maybe my wording is not very precise, but it does help me to understand tasks.

Gradle the task before executing the task. So the question is, how do I know what code in my task was executed during the configuration process and what code was running when the task was executed? The answer is that the code at the top of the task is the configuration code, such as:

    Task MyTask {        def name = "Pavel"//<  This line of code executes        println "Hello, world!" in the configuration phase <--This line of code will also be executed during the configuration phase    }

That's why when I perform gradle tasks, I print out "Hello, world!" -Because the configuration code was executed. But that's not the effect I want, I Want "Hello, world!." It only prints when I explicitly call MyTask. The simplest way to achieve this effect is to use the Task#dolast () method.

    Task MyTask {        def text = ' Hello, world! '//configure my task        dolast {            println text//this is executed when My task is called        }    }

Now, "Hello, world!." It will only be printed when I execute Gradle mytask. Cool, now I know how to configure and make the task do the right thing. One more question, in the first example, what does the << symbol of the third task mean?

    < < {        "Hello, world!"     }

This is actually just a syntactic sugar version of Dolast. It has the same effect as the following:

    Task MyTask {        Dolast {            println ' Hello, world! '//this is executed when my task is called        }    }

However, all of the code in this notation is in the execution section, with no configuration part of the code, so it is better suited to tasks that are simply small and do not need to be configured. Once your task needs to be configured, you still want to use the Dolast version.

[Android Pro] Gradle Tips#1-tasks

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.