1. Configure Gradle environment
Before installing Gradle, make sure that the system has been configured with a JDK environment, and the JDK version is required to be 1.7 or higher.
There are two forms of Gradle installation:
- Install through package management, such as Chocolatey and Scoop on the Window platform , MacPortsl and Homebrew on the Mac platform , etc.
- Manual installation.
For the package manager installation, you can check the official documentation . Here we mainly introduce manual installation. Download the binary-only version of Gradle you want in https://gradle.org/releases/ .
1.1 Windows platform installation
Similar to configuring the Java environment, add GRADLE_HOME to the system variable: then add %GRADLE_HOME%\bin to the path system variable, and run the gradle -v command to verify.
1.2 Mac platform installation
Proceed as follows:
- Open the terminal and run open -e .bash_profile, open the .bash_profile file to configure environment variables.
- In the .bash_profile file, add the gradle path to the path environment variable. For example, mine is:
Code
export PATH=$PATH:~/develop/gradle-4.10.1/bin
- Save the .bash_profile file and run source ~/.bash_profile in the terminal to update the .bash_profile file.
- Run the gradle -v command on the terminal to check whether the configuration is successful.
2. Implement Hello World
After configuring the Gradle environment, follow the usual practice to implement Gradle's Hello World. Here we take the Windows platform as an example.
build.gradle is the default build script file of Gradle. When running the Gradle command, it will look for the build.gradle file from the current directory to execute the build.
We first create a new directory, such as D:\Android\gradle_demo, create a new build.gradle file in this directory, and enter the following:
java
|
Then run the gradle -q hello build script in the directory where the file is located, and "Hello world!" will be printed.
Project construction is more complicated. In order for developers using various development languages to quickly build projects, experts have developed Gradle, a Groovy-based DSL. DSL (Domain Specifc Language) means a domain-specific language and is only used for a certain Specific areas. As long as we write according to Groovy's DSL syntax, we can easily build the project.
Task and action are important elements of Gradle. In the above code, task represents an independent atomic operation, such as copying a file and compiling the Java code once. Here we simply define a task named hello. doLast represents the last action executed by the task. Generally speaking, it will call back the code in doLast after the task is executed. In the above example, it will print "Hello world!"
The above example can be written more concisely. The operator << is a shortcut version of the doLast method. They do the same thing, as shown below.
java
|
3. Gradle tasks
In order to better explain the Gradle command line later, here is a brief introduction to Gradle tasks, including task creation, task dependencies, dynamically defined tasks, and task grouping and description.
3.1 Create task
In addition to the task creation method used in the Hello World example in Section 2, there are three other ways to create tasks.
1. Create directly with the task name.
java
|
2. Task name + task configuration creation.
java
|
The group is a task configuration item, which represents a grouping. For details about grouping, see section 3.4.
3. The create method of TaskContainer is created.
java
|
The previous method of creating tasks will eventually call the create method of tasks, where the tasks type is TaskContainer.
3.2 Task dependency
Task dependencies will determine the order in which tasks are run, and dependent tasks will be executed before the dependent tasks are defined. The dependencies between the creation tasks are as follows.
java
|
A task named go is added on the basis of the hello task, and the dependent task is specified as hello through dependsOn, so the go task runs after hello.
Run the gradle -q go build script, and the print result is as follows:
Hello world!
go for it
3.3 Dynamically define tasks
Dynamically defining a task refers to defining the name of the task at runtime, as shown below.
java
|
Groovy grammar is used here, and the Groovy grammar will be introduced in subsequent articles in this series. Times is a method extended by Groovy in java.lang.Number and is a timer. 3. Three new tasks are created in a loop in times. The values of the implicit variable number are 0, 1, and 2. The name of the task is composed of task plus the value of number, achieving the purpose of dynamically defining tasks.
Run the gradle -q task0 build script, and the print result is as follows:
task 0
3.4 Grouping and description of tasks
Gradle has the concept of task groups, which can be configured to group and describe tasks in order to better manage tasks and have good readability. Modify the example in section 3.2 to add grouping and description to the hello task.
java
|
You can also use other task creation methods in section 3.1 to add grouping and descriptions to tasks, as shown below.
java
|
4.Gradle log level
Like Android, Gradle also defines log levels.
level | Used for |
---|---|
ERROR | wrong information |
QUIET | Important information message |
WARNING | Warning message |
LIFECYCLE | Progress information message |
INFO | Informational message |
DEBUG | Debug message |
前面我们通过gradle -q +任务名称来运行一个指定的task,这个q是命令行开关选项,通过开关选项可以控制输出的日志级别。
开关选项 | 输出日志级别 |
---|---|
无日志选项 | LIFECYCLE及更高级别 |
-q或者 --quiet |
QUIET及更高级别 |
-i或者 --info |
INFO及更高级别 |
-d或者 --debug |
DEBUG及更高级别 |
5.Gradle 命令行
从命令行的角度,Gradle和Git类似,命令都可以用一些IDE、图形工具来代替,但是如果你对Gradle 命令行熟悉,会帮助你更好的理解Gradle,高效的运用Gradle。
5.1 获取所有任务信息
这一节的命令行以3.4小节的代码为例,此前我们通过gradle -q +任务名称来运行一个指定的任务,如果不知道任务的名称,可以通过运行gradle -q tasks命令来获取所有的任务信息,这样就不需要打开源码了。
java
|
默认情况下,只会显示那些被分组的任务的名称和描述。比如Build tasks(Build 任务组)中有我们定义的hello任务,Build Setup tasks中有init和wrapper,Help tasks有buildEnvironment 和components等等。
5.2 排除任务
如果我们不想运行go任务,可以运行gradle hello -x go命令:
java
|
可以看出,并没有运行go任务。
5.3 获取任务帮助信息
通过运行gradle -q help --task hello
命令来显示hello任务的帮助信息。
java
|
可以看到hello任务的路径、类型、描述和分组。
5.4 多任务调用
java
|
通过命令行一次执行多个任务,每个任务通常只会执行一次,无论是在命令行中指定任务还是任务依赖,上面的例子我们运行gradle helloWorld goForit,会先执行helloWorld任务后执行goForit任务。
5.5 任务名称缩写
It is possible to abbreviate tasks named with camel case. This feature is very useful for tasks with very long names. For example, the example in section 5.4 only needs to execute gradle hW gF , but one thing to note is that the abbreviation of the task name must be The only thing is, if the name of the second task in section 5.4 is helloWangshu, then an error will be reported.