Gradle 安裝

如果您只想執行現有的 Gradle 專案,那麼如果組建使用 Gradle Wrapper,則不需要安裝 Gradle。這可以透過專案根目錄中是否存在 gradlewgradlew.bat 檔案來辨識

.   (1)
├── gradle
│   └── wrapper (2)
├── gradlew         (3)
├── gradlew.bat     (3)
└── ⋮
1 專案根目錄。
2 Gradle Wrapper.
3 執行 Gradle 組建的腳本。

如果專案中已經存在 gradlewgradlew.bat 檔案,則您不需要安裝 Gradle。但您需要確保您的系統符合 Gradle 的先決條件

如果您想更新專案的 Gradle 版本,可以遵循 升級 Gradle 區段 中的步驟。請使用 Gradle Wrapper 升級 Gradle。

Android Studio 附帶 Gradle 的工作安裝,因此您在僅在該 IDE 中工作時不需要另外安裝 Gradle

如果您不符合上述條件並決定在您的機器上安裝 Gradle,請先透過在終端機中執行 gradle -v 來檢查是否已安裝 Gradle。如果命令沒有傳回任何內容,則表示尚未安裝 Gradle,您可以遵循下列說明進行操作。

您可以在 Linux、macOS 或 Windows 上安裝 Gradle 組建工具。安裝可以手動進行,或使用像 SDKMAN!Homebrew 等套件管理員進行。

您可以在 版本頁面 上找到所有 Gradle 版本及其校驗和。

先決條件

Gradle 可在所有主要作業系統上執行。它需要版本 8 或更高版本的 Java Development Kit (JDK) 才能執行。您可以查看 相容性矩陣 以取得更多資訊。

要檢查,請執行 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

Gradle 支援 KotlinGroovy 作為主要的建置語言。Gradle 附帶自己的 Kotlin 和 Groovy 函式庫,因此不需要安裝它們。Gradle 會忽略現有的安裝。

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)

  • 完整版 (all),包含文件和原始碼

我們建議下載 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

您可以下載 SHA-256 檔案(可從 發行頁面 取得)並遵循這些 驗證說明 來驗證 Gradle 發行版的完整性。