Android 工具命令——Gradle 学习

本文链接:https://rainmonth.github.io/posts/A190918.html

Android Gradle 学习

这里对于gradle的安装以及环境变量的配置不做赘述,假定gradle环境已经设置好。

首先运行如下命令:

gradle —help

该命令用户查看帮助,会有如下输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
USAGE: gradle [option...] [task...]

-?, -h, --help Shows this help message.
-a, --no-rebuild Do not rebuild project dependencies.
-b, --build-file Specify the build file.
--build-cache Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds. [incubating]
-c, --settings-file Specify the settings file.
--configure-on-demand Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating]
--console Specifies which type of console output to generate. Values are 'plain', 'auto' (default) or 'rich'.
--continue Continue task execution after a task failure.
-D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue).
-d, --debug Log in debug mode (includes normal stacktrace).
--daemon Uses the Gradle Daemon to run the build. Starts the Daemon if not running.
--foreground Starts the Gradle Daemon in the foreground. [incubating]
-g, --gradle-user-home Specifies the gradle user home directory.
-I, --init-script Specify an initialization script.
-i, --info Set log level to info.
--include-build Include the specified build in the composite. [incubating]
-m, --dry-run Run the builds with all task actions disabled.
--max-workers Configure the number of concurrent workers Gradle is allowed to use. [incubating]
--no-build-cache Disables the Gradle build cache. [incubating]
--no-daemon Do not use the Gradle Daemon to run the build.
--no-scan Disables the creation of a build scan. (https://gradle.com/build-scans) [incubating]
--offline Execute the build without accessing network resources.
-P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue).
-p, --project-dir Specifies the start directory for Gradle. Defaults to current directory.
--parallel Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. [incubating]
--profile Profile build execution time and generates a report in the <build_dir>/reports/profile directory.
--project-cache-dir Specify the project-specific cache directory. Defaults to .gradle in the root project directory.
-q, --quiet Log errors only.
--recompile-scripts Force build script recompiling.
--refresh-dependencies Refresh the state of dependencies.
--rerun-tasks Ignore previously cached task results.
-S, --full-stacktrace Print out the full (very verbose) stacktrace for all exceptions.
-s, --stacktrace Print out the stacktrace for all exceptions.
--scan Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans) [incubating]
--status Shows status of running and recently stopped Gradle Daemon(s).
--stop Stops the Gradle Daemon if it is running.
-t, --continuous Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. [incubating]
-u, --no-search-upward Don't search in parent folders for a settings.gradle file.
-v, --version Print version info.
-w, --warn Set log level to warn.
-x, --exclude-task Specify a task to be excluded from execution.

-b 用于指定gradle配置文件

-x 用于排除指定的任务的执行

-v 用户查看当前gradle版本信息

Gradle 执行多个任务的时候,每个任务只执行一次,如下:

build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
task compile {
doLast {
println 'compiling source'
}
}

task compileTest(dependsOn:compile) {
doLast {
println 'compiling unit tests'
}
}

task test(dependsOn:[compile, compileTest]) {
doLast {
println 'running unit tests'
}
}

task dist(dependsOn:[compile, test]) {
doLast {
println 'building the distribution'
}
}

执行命令 gradle dist test,输出如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> Task :compile
compiling source

> Task :compileTest
compiling unit tests

> Task :test
running unit tests

> Task :dist
building the distribution


BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed

执行命令 gradle test test,输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
> Task :compile
compiling source

> Task :compileTest
compiling unit tests

> Task :test
running unit tests


BUILD SUCCESSFUL in 0s
3 actionable tasks: 3 executed

执行命令 gradle dist -x test,输出如下:

1
2
3
4
5
6
7
8
9
10

> Task :compile
compiling source

> Task :dist
building the distribution


BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed