[Android] Getting Started with Gradle

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:

  1. Install through package management, such as Chocolatey and Scoop on the Window platform , MacPortsl and Homebrew on the Mac platform , etc.
  2. 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.
VZvEKf.png

1.2 Mac platform installation

Proceed as follows:

  1. Open the terminal and run open -e .bash_profile, open the .bash_profile file to configure environment variables.
  2. 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
    
  3. Save the .bash_profile file and run source ~/.bash_profile in the terminal to update the .bash_profile file.
  4. Run the gradle -v command on the terminal to check whether the configuration is successful.

VZvVr8.png

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

task hello {
    doLast {
        println 'Hello world!'
    }
}

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

task hello << {
    println 'Hello world!'
}

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

def Task hello=task(hello)
hello.doLast{
	 println "hello world"
}

2. Task name + task configuration creation.

java

def Task hello=task(hello,group:BasePlugin.BUILD_GROUP)
hello.doLast{
	 println "hello world"
}

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

tasks.create(name: 'hello') << {
    println "hello world"
}

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

task hello << {
    println 'Hello world!'
}
task go(dependsOn: hello) << {
    println "go for it"
}

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

3.times {number ->
    task "task$number" << {
        println "task $number"
    }
}

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

task hello {
	group = 'build'
	description = 'hello world'
    doLast {
    	println "任务分组: ${group}"
        println "任务描述: ${description}"
    }
}
task go(dependsOn: hello) << {
    println "go for it"
}

You can also use other task creation methods in section 3.1 to add grouping and descriptions to tasks, as shown below.

java

def Task hello=task(hello)
hello.description ='hello world'
hello.group=BasePlugin.BUILD_GROUP
hello.doLast{
	println "任务分组: ${group}"
    println "任务描述: ${description}"
}
task go(dependsOn: hello) << {
    println "go for it"
}

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
-----------
hello - hello world

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root projec
t 'gradle_demo'.
components - Displays the components produced by root project 'gradle_demo'. [in
cubating]
dependencies - Displays all dependencies declared in root project 'gradle_demo'.

dependencyInsight - Displays the insight into a specific dependency in root proj
ect 'gradle_demo'.
dependentComponents - Displays the dependent components of components in root pr
oject 'gradle_demo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradle_demo'. [incubat
ing]
projects - Displays the sub-projects of root project 'gradle_demo'.
properties - Displays the properties of root project 'gradle_demo'.
tasks - Displays the tasks runnable from root project 'gradle_demo'.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

默认情况下,只会显示那些被分组的任务的名称和描述。比如Build tasks(Build 任务组)中有我们定义的hello任务,Build Setup tasks中有init和wrapper,Help tasks有buildEnvironment 和components等等。

5.2 排除任务

如果我们不想运行go任务,可以运行gradle hello -x go命令:

java

> Task :hello
任务分组: build
任务描述: hello world

Deprecated Gradle features were used in this build, making it incompatible with
Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.1/userguide/command_line_interface.html#sec:com
mand_line_warnings

BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed

可以看出,并没有运行go任务。

5.3 获取任务帮助信息

通过运行gradle -q help --task hello命令来显示hello任务的帮助信息。

java

Detailed task information for hello

Path
     :hello

Type
     Task (org.gradle.api.Task)

Description
     hello world

Group
     build

可以看到hello任务的路径、类型、描述和分组。

5.4 多任务调用

java

task helloWorld << {
    println 'Hello world!'
}
task goForit<< {
    println "go for it"
}

通过命令行一次执行多个任务,每个任务通常只会执行一次,无论是在命令行中指定任务还是任务依赖,上面的例子我们运行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.

Guess you like

Origin blog.csdn.net/xfb1989/article/details/110082320