手动安装 Gradle 并在 Android Studio 中使用

我正在使用 Android Studio。如何在 Android Studio中手动安装和使用 Gradle

我已经从 http://www.gradle.org/downloads版本 gradle-2.1-all.zip 下载了 Gradle

When I open the zipped file I can see bin, docs etc, but I don't know where to copy it. And even after copying how to use it within Android..

312415 次浏览

Unpack it where ever you like. In Android Studio under Settings is category Gradle where you can specify external gradle location if you want. It probably makes sense to put gradle bin folder into your path.

Android Studio will automatically use the Gradle wrapper and pull the correct version of Gradle rather than use a locally installed version. If you wish to use a new version of Gradle, you can change the version used by studio. Beneath your Android Studio's project tree, open the file gradle/wrapper/gradle-wrapper.properties. Change this entry:

distributionUrl=http\://services.gradle.org/distributions/gradle-2.1-all.zip

https://services.gradle.org/distributions/

Download The Latest Gradle Distribution File and Extract It, Then Copy all Files and Paste it Under:

C:\Users\{USERNAME}\.gradle\wrapper\dists\

Step 1: Go to the download site from Gradle: https://gradle.org/releases/

Step 2: Extract the downloaded zip file into a directory.

Step 2: Hit Ctrl + Alt + S (mac: ⌘ + ,) in Android studio/Intellij IDEA

Step 3: Goto: Build, Execution, Deployment >> Build Tools >> Gradle (Or just type in the searchbar Gradle)

Step 4: Select: (X) Use local gradle distribution and set Gradle home to your extracted Gradle directory. Click on apply.

Step 5: Get rid of your unnecessary gradle files delete:
- MyApp/gradle/
- gradlew
- gradlew.bat

in first step you should go to http://www.gradle.org/downloads and then choose your Gradle version and follow download link in GitHub and then download Gradle from GitHub. after your download is completed, you should extract the downloaded file in a directory (every directory that you want). finally open extracted folder and just run gradlew.bat and wait several minutes for completing download. and after completing download files, Gradle install itself automaticly. Gradle will work correctly for next times. i resolve this problem with this way and i tested it and every things are ok, so every one can test this way, it should working well. Good Luck.

If you are on LINUX follow the steps below:

  • Go to the download https://gradle.org/ and download the latest zip file (currently 3.1 is the latest version)

  • Extract the downloaded zip file into /opt/android-studio/gradle/.

  • Open your project in Android Studio and hit Ctrl + Alt + S

  • Search for Gradle in the Search bar of the setting window

  • Select: (X) Use local gradle distribution and set Gradle home to /opt/andriod-studio/gradle/gradle.x.x (where x.x is the vesrion of the gradle zip file you just downloaded.). Then click on apply.

  • Try to sync Gradle again if the sync has already failed.If not close your project and reopen your project.

    Hope this solves your problem.

Assuming this is a windows system and we are using gradle-2.1-all [Applicable to any gradle version, just change the version as we need]

All the gradles can be found in http://services.gradle.org/distributions/


Check the <Your Project>\gradle\wrapper\gradle-wrapper.properties Make sure the distributionUrl is

distributionUrl=http\://services.gradle.org/distributions/gradle-2.1-all.zip

This is enough as it will automatically download the gradle.

After doing the above stuff, if you have downloaded the zip, paste the gradle-2.1-all.zip in

C:\Users\<username>\.gradle\wrapper\dists\gradle-2.1-all\<some_hash_key>\

It will save the extra downloading time.

1.Download the Gradle form gradle distribution

2.Extract file to some location

3.Open Android Studio : File > Settings > Gradle > Use local gradle distribution navigate the path where you have extracted the gradle.

4.click apply and ok

Done

EDIT 1: In the latest version of AS, the option is called "Use Gradle from: Specified location, thanks @Adam Burley for suggestion

Step 1. Download the latest Gradle distribution

Step 2. Unpack the distribution

Microsoft Windows users

Create a new directory C:\Gradle with File Explorer.

Open a second File Explorer window and go to the directory where the Gradle distribution was downloaded. Double-click the ZIP archive to expose the content. Drag the content folder gradle-4.1 to your newly created C:\Gradle folder.

Alternatively you can unpack the Gradle distribution ZIP into C:\Gradle using an archiver tool of your choice.

Step 3. Configure your system environment

Microsoft Windows users

In File Explorer right-click on the This PC (or Computer) icon, then click Properties -> Advanced System Settings -> Environmental Variables.

Under System Variables select Path, then click Edit. Add an entry for C:\Gradle\gradle-4.1\bin. Click OK to save.

Step 4. Verify your installation

1.Install gardle as per the given link http://services.gradle.org/distributions/ 2.Extract this downloaded file in C:\Gradle\gradle-4.5 location 3.set the environment of gradle This PC\properties\advance system settings\Environment variable 4.let's start Android studio And set the path of gradle C:\Gradle\gradle In Android studio

(There are 2 solutions mentioned in existing answers that might work, but the preferred one - manually download gradle for gradlew, is lack of essential details, and cause it fail. So, I would add a summary with missing details to save the unnecessary time wasted, in case others encounter into the same issue.)

There are 2 possible solutions:


Solution A: Use location gradle, and delete gradlew related files. (not recommend)

Refer to this answer from this post: https://stackoverflow.com/a/29198101/

Tips:

  • I tried, it works, though this is not suggested for gradle use in general.
  • And, with this solution, each time open project, android-studio will ask to confirm whether to use gradlew instead, it's kinda annoying.

Solution B: Download gradle distribution manually for gradlew. (recommended)

Android Studio will download gradle to sub dir named by a hash.
To download manually, need to download to the exact sub dir named by the hash.

Steps:

  • Get the hash.
    • Start android-studio.
    • Create a basic project.
    • Then it will create the hash, and start to download gradle.
      e.g .gradle/wrapper/dists/gradle-4.10.1-all/455itskqi2qtf0v2sja68alqd/
    • Close android-studio.
    • Find the download process, by android-studio.
      e.g ps -aux grep | android
    • Kill all the related android processes.
    • Remove the blank project.
  • Download gradle by hand.
  • Start Android Studio and try again.
    • Create a new blank project again.
    • Then it shouldn't need to download gradle again.
    • It will uncompress gradle in the the same dir.
      e.g .gradle/wrapper/dists/gradle-4.10.1-all/455itskqi2qtf0v2sja68alqd/gradle-4.10.1/
    • And starts to sync dependencies, indexing, and build.

Tips:

  • After restart Android Studio & creating blank project again, if you see it says waiting for other process to download the distribution.
    That means you didn't kill the previous download process, kill it first, then remove blank project, then create a new project to confirm again.
  • Each version of Android Studio might use different gradle version, thus might need to repeat this process once, when Android Studio is upgraded.

BTW:

  • Here is the dir layout on my Linux
    (For Android Studio 3.3.1, which use gradle 4.10.1)

    eric@eric-pc:~/.gradle/wrapper$ tree -L 4

    .
    └── dists
    └── gradle-4.10.1-all
    └── 455itskqi2qtf0v2sja68alqd
    ├── gradle-4.10.1
    ├── gradle-4.10.1-all.zip
    ├── gradle-4.10.1-all.zip.lck
    └── gradle-4.10.1-all.zip.ok
    
    
    

Suggestions to Android-Studio

  • Since it's so slow when download gradle distrbution within Android Studio.
    It's better to provide an option to choose local gradle installation dir or .zip file to be used by gradlew.

I'm not sure why there are so many contorted descriptions of doing this. Perhaps because Android Studio (AS) is constantly changing/evolving? Nevertheless, the procedure is this simple.

Assuming you have already installed Gradle in a suitable directory, mean that you probably also defined an environment variable for GRADLE_HOME, if not define it now, and restart AS.

For my example: GRADLE_HOME=C:\Gradle\gradle-5.2.1

Then fire up AS and navigate to:
File > Settings > Build, Execution, Deployment > Gradle

Now, all you need to do is to select: (o) Use local gradle distribution. AS will then tell you it found your local Gradle paths:

enter image description here

Hit Apply and Ok and restart AS.

Now all the Gradle data can be found in your HOME directory, usually located here (in this case on Windows):

# tree -L 2 $USERPROFILE/.gradle/ | tail -n +2


├── build-scan-data
│   ├── 2.1
│   └── 2.2.1
├── caches
│   ├── 3.5
│   ├── 5.2.1
│   ├── jars-3
│   ├── journal-1
│   ├── modules-2
│   ├── transforms-1
│   ├── transforms-2
│   ├── user-id.txt
│   └── user-id.txt.lock
├── daemon
│   ├── 3.5
│   └── 5.2.1
├── native
│   ├── 25
│   ├── 28
│   └── jansi
├── notifications
│   └── 5.2.1
├── workers
└── wrapper
└── dists

Tested on latest AS 3.3.2

I used like this,

distributionUrl=file\:///E\:/Android/Gradle/gradle-5.4.1-all.zip

And its worked for me.

Like @ said

https://services.gradle.org/distributions/

Download The Latest Gradle Distribution File and Extract It, Then Copy all Files and Paste it Under:

C:\Users\{USERNAME}\.gradle\wrapper\dists\

but you have to first make Android Studio try downloading the zip file and cancel it.

That way you can get the hash and copy the file and put it under the hash

Follow these steps

  1. Download gradle
  2. Extract the file to a specific location you desire
  3. Open android studio Click on setting and on build, execution and deployment, then gradle
  4. Enable work offline and change service directory to the location of the extracted filed.

This is what worked for me. I hope it helps.

download the desired pacakge Then modify the distribution line to

distributionUrl=file:/c:/Gradle/gradle-5.5.1-all.zip

Assume, you have installed the latest gradle once. But, If your particular project gradle version not match with the gradle version that already installed in the machine, the gradle sycn want to download that version. To prevent this download there is one trick. First You have to know the gradle version that already installed in your machine. Go to : C:\Users{username}.gradle\wrapper\dists, here you see the versions allready installed, remember the latest version, assume it is gradle-6.1.1-all.zip . Now, come back to Android Studio. In your Opened project, navigate Android Studio's project tree, open the file gradle/wrapper/gradle-wrapper.properties. Change this entry:

distributionUrl=http\://services.gradle.org/distributions/gradle-6.1.1-all.zip

This way we prevent downloading the gradle again and again. But avoid this thing, if the version really old. If that, you will find this warning :

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.1.1/userguide/command_line_interface.html#sec:command_line_warnings
  1. Download the gralde to your computer
  2. Extract the file to any folder of your choice

Now there are two ways you can tell Android studio to use the downloaded gradle

  1. Go to "AndroidStudio->Preferences->Build,Execution,Deployment->BuildTools->Gradle" and update "Use Gradle from:" to Specified location and set your path

enter image description here

  1. Go to gradle-wrapper.properties file in your project. This will be located in "YourAppFolder->gradle->wrapper->gradle-wrapper.properties" Edit the distributionUrl for example

distributionUrl=file:/Users/john/downloads/gradle-6.5.1-all.zip

On a Debian Distrubition I simply set

distributionUrl=file:///home/freduah/.GradleZip/gradle-6.7-all.zip

Android Studio Version ---- 4.2.1

OS ----- Kali GNU/Linux Rolling

Follow The Following Steps Remember It's Android Studio Version 4.2.1

In the Project Directory Go To android/gradle/wrapper/gradle-wrapper.properties

You Could set it with this syntax

distributionUrl=file:///home/<username>/pathToFile/nameOfFile.extensionType