How to reuse gradle-wrapper?

2019-04-02 12:05发布

问题:

All, I migrate from eclipse to android-studio for one month. the new build tool is annoying.

Every time I git clone a android-sample, need hours to download the its gradle-wrapper.

I found there're many version of gradle-wrapper

ninja@ninja ~/.gradle/wrapper/dists $ du -h  --max-depth=1
198M    ./gradle-2.2.1-all
176M    ./gradle-1.11-all
86M ./gradle-1.12-bin
85M ./gradle-2.0-bin
358M    ./gradle-1.12-all
186M    ./gradle-2.1-all
1.1G    .

ninja@ninja ~/.gradle/wrapper/dists/gradle-1.12-all $ du -h --max-depth=1
179M    ./2apkk7d25miauqf1pdjp1bm0uo
179M    ./4ff8jj5a73a7zgj5nnzv1ubq0
358M    .

Is there a easy way to build projects from github ?

and why android-studio do not use my gradle?

ninja@ninja ~/.gradle/wrapper/dists/gradle-1.11-all $ gradle -v

------------------------------------------------------------
Gradle 2.1
------------------------------------------------------------

Build time:   2014-09-08 10:40:39 UTC
Build number: none
Revision:     e6cf70745ac11fa943e19294d19a2c527a669a53

Groovy:       2.3.6
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.7.0_67 (Oracle Corporation 24.65-b04)
OS:           Linux 3.13.0-24-generic i386

I tried android-studio's setting use local gradle distribution , and meet compile errors:

ninja@ninja ~/src/goshawk/13_GeexFinanceSource/Geexfinance $ gradle installDebug

FAILURE: Build failed with an exception.

* Where:
Build file '/home/ninja/src/goshawk/13_GeexFinanceSource/Geexfinance/SlidingMenuLibrary/build.gradle' line: 9

* What went wrong:
A problem occurred evaluating project ':SlidingMenuLibrary'.
> Failed to apply plugin [id 'android-library']

Gradle version 2.2 is required. Current version is 2.1. If using the gradle wrapper, try editing the distributionUrl in /home/ninja/src/goshawk/13_GeexFinanceSource/Geexfinance/gradle/wrapper/gradle-wrapper.properties to gradle-2.2-all.zip

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1 mins 5.884 secs

I need your help, any idea?

回答1:

From gradle-wrapper docs:

If you don't want any download to happen when your project is build via gradlew,
simply add the Gradle distribution zip to your version control at the location
specified by your wrapper configuration. A relative URL is supported - you can
specify a distribution file relative to the location of gradle-wrapper.properties 
file.

Basically, once you have the binaries gradle-2.1-bin.zip and the wrapper jar gradle-wrapper.jar you can simply move it around where ever you please with the correct gradle-wrapper.properties and gradlew executable.

The folder structure of your project will be:

Project1 -> gradlew or gradlew.bat (wrapper executable)
            gradle -> wrapper -> gradle-2.1-bin.zip
                                 gradle-wrapper.jar
                                 gradle-wrapper.properties

The gradlew executable looks for gradle-wrapper.jar in ./gradle/wrapper/

Here is an example of gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
# copy the distribution to the gradle/wrapper directory manually for the first run
# that way the distribution doesn't need to be checked into version control
distributionUrl=gradle-2.1-bin.zip

When running ./gradlew in Project1 it will extract to ~/.gradle/wrapper/dists/gradle-2.1-bin/

Just copy Project1/gradle* to Project2/ to reuse the wrapper.



回答2:

I had the same error and just now I fixed it! In root build.gradle type

task wrapper(type: Wrapper) {
    gradleVersion = '2.2'
}

Also it can be useful to fix Gradle settings to recomended in the "Fix Gradle Settings" dialog. Or you can manually download gradle-2.2-all.zip, place it in {project_dir}-gradle-wrapper and run gradlew.bat in terminal.