Gradle tutorial description User Guide 1 ~ Chapter 6 gradle User Guide

Source: Internet
Author: User

Gradle tutorial description User Guide 1 ~ Chapter 6 gradle User Guide

To use Android Studio, you need this build tool.

This document is a rough example of the notes in the first six chapters of the official user guide.


Download the latest Gradle version (Currently 2.2) and decompress it. Configure the bin directory of Gradle to environment variables so that you can use the gradle command in shell.


Create a directory (a directory named builds is created here), cd, and create a build. gradle file. The following example is edited in build. gradle.

Note:> followed by the gradle command. -The q Parameter disables the log information in some commands, such as gradle-q hello.


Define a task and execute

Task hello {
DoLast {
Println 'Hello world! \ Nadmin'
}
}

> Gradle hello


Define a quick task

Task hello2 <{
Println 'admin \ nHello world! '

}

> Gradle hello2

<Represents doLast {}


Use Groovy

Task upper <{
String someString = 'my _ name'
Println "Original:" + someString
Println "Upper case:" + someString. toUpperCase ()
}

> Gradle upper


Loop Traversal

Task foreach <{
10. times {println "the times is $ it ."}
}

> Gradle foreach

10. times, cyclic condition <10; $ it indicates cyclic variable


Dependent on other tasks

Task intro (dependsOn: hello) <{
Println "I'm Gradle --- intro"
}

> Gradle intro

The dependent task hello will call hello before executing the current task


The dependent task is defined after the current task.

Task taskX (dependsOn: 'tasky ') <{
Println 'taskx'
}
Task <{
Println 'tasky'
}

> Gradle taskX

Dependencies are defined in subsequent tasks. You must add single quotation marks to the task name. In the previous example, you can add a single quotation mark.


Dynamic Task

4. times {counter->
Task "task $ counter" <{
Println "I'm task number $ counter"
}
}

> Gradle test [0-4]

Four tasks are generated in four cycles. The task names are task0, task1, task2, and task3.


Dynamic Tasks depend on Dynamic Tasks

Task0.dependsOn task2, task3

> Gradle task0

Of course, dynamic dependency on static resources and static dependency on Dynamic resources are acceptable.


Symbol <and doFirt, doLast

Task hello3 <{
Println 'Hello global'
}
Hello3.doFirst {
Println 'Hello Venus'
}
Hello3.doLast {
Println 'Hello Mars'
}
Hello3 <{
Println 'Hello Jupiter'
}

> Gradle hello3

There can be multiple doFirst and doLast (or <). DoFirtst is executed first, and doLast is followed. The order here should be: Venus, Earth, Mars, Jupiter


Use external task attributes

Task myTask {
Ext. myProperty = "myValue"
}
Task extraProps <{
Println myTask. myProperty
}

> Gradle extraProps


Use the ant task loadfile1 <{
Def files = file ('../builds'). listFiles (). sort ()
Files. each {File file->
If (file. isFile ()){
Ant. loadfile (srcFile: file, property: file. name)
Println "*** $ file. name ***"
Println "$ {ant. properties [file. name]}"
}
}
}

> Gradle loadfile1

Call the ant. properties task. The file content is output here.


The ant task is used and the Method is called.

Task checksum <{
FileList ('../builds'). each {File file->
Ant. checksum (file: file, property: "cs _ $ file. name ")
Println "$ file. name Checksum: $ {ant. properties [" cs _ $ file. name "]}"
}
}
Task loadfile2 <{
FileList ('../builds/'). each {File file->
Ant. loadfile (srcFile: file, property: file. name)
Println "I'm fond of $ file. name"
}
}
File [] fileList (String dir ){
File (dir). listFiles ({file-> file. isFile ()} as FileFilter). sort ()
}

> Gradle loadfile2

> Gradle checksum

Declared method ---- fileList ()


Default task

DefaultTasks 'clean', 'run'

Task clean <{
Println 'default Cleaning! '
}
Task run <{
Println 'default Running! '
}
Task other <{
Println "I'm not a default task! "
}

> Gradle

Execute the default task without following the Task Name


Configure by DAG

Task distribution <{
Println "We build the zip with version = $ version"
}
Task release (dependsOn: 'distribution ') <{
Println 'we release now'
}
Gradle. taskGraph. whenReady {taskGraph->
If (taskGraph. hasTask (release )){
Version = '1. 0'
} Else {
Version = '1. 0-SNAPSHOT'
}
}

> Gradle distribution

If there is no release-task in the running task graph, the output We build the zip with version = 1.0-SNAPSHOT

> Release

The running task graph contains release-task, and the output is We build the zip with version = 1.0.

Related Article

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.