Gradle Tutorial Description User Guide 1~6 Chapter

Source: Internet
Author: User

To use Android Studio, you need this build tool.

This article is a rough official user's Guide to the first 6 chapters of the notes, as a sample drive.


After downloading the latest version of Gradle (currently 2.2), unzip. Configure the Bin directory of the Gradle to an environment variable so that the Gradle command can be used in the shell.


Create a new directory (I've built a directory called builds), CD in, create a new Build.gradle file, and the following examples are all edited in Build.gradle.

Description:> followed by the Gradle command. The-q parameter closes 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 shortcut task

Task Hello2 << {
println ' Admin\nhello world! '

}

> Gradle Hello2

<< Rep dolast{}


using the Groovy language

Task Upper << {
String somestring = ' My_name '
println "Original:" + somestring
println "Upper case:" + somestring.touppercase ()
}

> Gradle Upper


Looping through

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

> Gradle foreach

10.times, cyclic conditions <10; $it representing loop variables


rely on other tasks

Task Intro (Dependson:hello) << {
println "I ' m Gradle---intro"
}

> Gradle Intro

Dependent task Hello, call Hello before executing the current task


a dependent task is defined after the current task

Task Taskx (dependsOn: ' Tasky ') << {
println ' taskx '
}
Task Tasky << {
println ' Tasky '
}

> Gradle taskx

Depending on the tasks that are defined in the later task, you need to add single quotes to the task name. In the previous example, the addition of a single quotation mark can


Dynamic Tasks

4.times {counter
Task "Task$counter" << {
println "I ' m task number $counter"
}
}

> Gradle test[0-4]

Loops four times, generates 4 tasks, and task names are TASK0,TASK1,TASK2,TASK3.


dynamic tasks rely on dynamic tasks

Task0.dependson Task2, Task3

> Gradle task0

Of course dynamic dependent static, static dependent dynamic is possible


symbols << and dofirt, Dolast

Task Hello3 << {
println ' Hello Earth '
}
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, dolast behind. Here the order should be: Venus, Earth, Mars, Jupiter


using properties of external tasks

Task MyTask {
Ext.myproperty = "MyValue"
}
Task Extraprops << {
println Mytask.myproperty
}

> Gradle extraprops


using ant TasksTask 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

invokes the task ant.properties. The contents of the file are output here


using the ant task and calling the method

task checksum << {
    fileList (' ... /builds '). Each {file file->
        Ant.checksum (File:file, property: "Cs_$file.name")
& nbsp       println "$file. Name Checksum: ${ant.properties[" Cs_$file.name "]}"
   }
}
Task Loadfile2 << {
    fileList ('.. /builds/'). Each {file file->
        Ant.loadfile (Srcfile:file, Property:file.name)
& nbsp       println "I ' m fond of $file. Name"
   }
}
file[] fileList (String dir) {
  & nbsp 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

Does not follow the task name to perform the default task


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

Run task graph without release-task, output We build the zip with Version=1.0-snapshot

> Release

Run task graph with Release-task, output We build the zip with version=1.0

Gradle Tutorial Description User Guide 1~6 Chapter

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.