Gradle 安装
如果你只想运行现有的 Gradle 项目,那么如果你构建使用 Gradle Wrapper,则无需安装 Gradle。可以通过项目根目录中的 gradlew
或 gradlew.bat
文件的存在来识别这一点
. (1) ├── gradle │ └── wrapper (2) ├── gradlew (3) ├── gradlew.bat (3) └── ⋮
1 | 项目根目录。 |
2 | Gradle Wrapper. |
3 | 用于执行 Gradle 构建的脚本。 |
如果 gradlew
或 gradlew.bat
文件已存在于你的项目中,你无需安装 Gradle。但是你需要确保你的系统 满足 Gradle 的先决条件。
如果你想更新项目的 Gradle 版本,可以按照 升级 Gradle 部分 中的步骤进行操作。请使用 Gradle Wrapper 升级 Gradle。
Android Studio 附带了 Gradle 的工作安装,因此在仅在该 IDE 内工作时,您无需单独安装 Gradle。
如果您不满足上述条件并决定在您的机器上安装 Gradle,请首先通过在您的终端中运行 gradle -v
来检查 Gradle 是否已安装。如果命令没有返回任何内容,则表示 Gradle 未安装,您可以按照以下说明进行操作。
您可以在 发布页面 上找到所有 Gradle 版本及其校验和。
先决条件
Gradle 可以在所有主要操作系统上运行。它需要 Java 开发工具包 (JDK) 8 或更高版本才能运行。您可以查看 兼容性矩阵 以获取更多信息。
要检查,请运行 java -version
❯ java -version openjdk version "11.0.18" 2023-01-17 OpenJDK Runtime Environment Homebrew (build 11.0.18+0) OpenJDK 64-Bit Server VM Homebrew (build 11.0.18+0, mixed mode)
❯ java version "1.8.0_151" Java(TM) SE Runtime Environment (build 1.8.0_151-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
Gradle 使用它在您的路径中找到的 JDK、您的 IDE 使用的 JDK 或您的项目指定的 JDK。在此示例中,$PATH 指向 JDK17
❯ echo $PATH /opt/homebrew/opt/openjdk@17/bin
您还可以设置 JAVA_HOME
环境变量以指向特定的 JDK 安装目录。当安装了多个 JDK 时,这尤其有用
❯ echo %JAVA_HOME% C:\Program Files\Java\jdk1.7.0_80
❯ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
Linux 安装
Installing with a package manager
SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix-like systems (macOS, Linux, Cygwin, Solaris and FreeBSD). Gradle is deployed and maintained by SDKMAN!:
❯ sdk install gradle
Other package managers are available, but the version of Gradle distributed by them is not controlled by Gradle, Inc. Linux package managers may distribute a modified version of Gradle that is incompatible or incomplete when compared to the official version.
Installing manually
Step 1 - Download the latest Gradle distribution
The distribution ZIP file comes in two flavors:
-
Binary-only (bin)
-
Complete (all) with docs and sources
We recommend downloading the bin file; it is a smaller file that is quick to download (and the latest documentation is available online).
Step 2 - Unpack the distribution
Unzip the distribution zip file in the directory of your choosing, e.g.:
❯ mkdir /opt/gradle ❯ unzip -d /opt/gradle gradle-8.7-bin.zip ❯ ls /opt/gradle/gradle-8.7 LICENSE NOTICE bin README init.d lib media
Step 3 - Configure your system environment
To install Gradle, the path to the unpacked files needs to be in your Path.
Configure your PATH
environment variable to include the bin
directory of the unzipped distribution, e.g.:
❯ export PATH=$PATH:/opt/gradle/gradle-8.7/bin
Alternatively, you could also add the environment variable GRADLE_HOME
and point this to the unzipped distribution.
Instead of adding a specific version of Gradle to your PATH
, you can add $GRADLE_HOME/bin
to your PATH
.
When upgrading to a different version of Gradle, simply change the GRADLE_HOME
environment variable.
export GRADLE_HOME=/opt/gradle/gradle-8.7 export PATH=${GRADLE_HOME}/bin:${PATH}
macOS 安装
Installing with a package manager
SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix-like systems (macOS, Linux, Cygwin, Solaris and FreeBSD). Gradle is deployed and maintained by SDKMAN!:
❯ sdk install gradle
Using Homebrew:
❯ brew install gradle
Using MacPorts:
❯ sudo port install gradle
Other package managers are available, but the version of Gradle distributed by them is not controlled by Gradle, Inc.
Installing manually
Step 1 - Download the latest Gradle distribution
The distribution ZIP file comes in two flavors:
-
Binary-only (bin)
-
Complete (all) with docs and sources
We recommend downloading the bin file; it is a smaller file that is quick to download (and the latest documentation is available online).
Step 2 - Unpack the distribution
Unzip the distribution zip file in the directory of your choosing, e.g.:
❯ mkdir /usr/local/gradle ❯ unzip gradle-8.7-bin.zip -d /usr/local/gradle ❯ ls /usr/local/gradle/gradle-8.7 LICENSE NOTICE README bin init.d lib
Step 3 - Configure your system environment
To install Gradle, the path to the unpacked files needs to be in your Path.
Configure your PATH
environment variable to include the bin
directory of the unzipped distribution, e.g.:
❯ export PATH=$PATH:/usr/local/gradle/gradle-8.7/bin
Alternatively, you could also add the environment variable GRADLE_HOME
and point this to the unzipped distribution.
Instead of adding a specific version of Gradle to your PATH
, you can add $GRADLE_HOME/bin
to your PATH
.
When upgrading to a different version of Gradle, simply change the GRADLE_HOME
environment variable.
It’s a good idea to edit .bash_profile
in your home directory to add GRADLE_HOME
variable:
export GRADLE_HOME=/usr/local/gradle/gradle-8.7 export PATH=$GRADLE_HOME/bin:$PATH
Windows 安装
手动安装
步骤 1 - 下载 最新 Gradle 发行版
发行版 ZIP 文件有两种类型
-
仅二进制(bin)
-
完整(全部),包括文档和源
我们建议下载 bin 文件。
步骤 2 - 解压发行版
使用文件资源管理器创建一个新目录 C:\Gradle
。
打开第二个文件资源管理器窗口,并转到下载 Gradle 发行版的目录。双击 ZIP 存档以显示内容。将内容文件夹 gradle-8.7
拖动到您新创建的 C:\Gradle
文件夹中。
或者,您可以使用您选择的归档工具将 Gradle 发行版 ZIP 解压到 C:\Gradle
中。
步骤 3 - 配置您的系统环境
要安装 Gradle,解压后的文件路径需要在你的路径中。
在文件资源管理器中右键单击此电脑
(或计算机
)图标,然后单击属性
→高级系统设置
→环境变量
。
在系统变量
下选择路径
,然后单击编辑
。添加C:\Gradle\gradle-8.7\bin
的条目。单击确定
保存。
或者,你可以添加环境变量GRADLE_HOME
,并将其指向解压的发布版本。你可以将%GRADLE_HOME%\bin
添加到你的路径
中,而不是将 Gradle 的特定版本添加到你的路径
中。在升级到不同版本的 Gradle 时,只需更改GRADLE_HOME
环境变量即可。
验证安装
打开控制台(或 Windows 命令提示符)并运行gradle -v
来运行 gradle 并显示版本,例如。
❯ gradle -v ------------------------------------------------------------ Gradle 8.7 ------------------------------------------------------------ Build time: 2023-03-03 16:41:37 UTC Revision: 7d6581558e226a580d91d399f7dfb9e3095c2b1d Kotlin: 1.8.10 Groovy: 3.0.13 Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021 JVM: 17.0.6 (Homebrew 17.0.6+0) OS: Mac OS X 13.2.1 aarch64