如何重用gradle-wrapper?

时间:2014-12-09 09:46:36

标签: java gradle android-studio

所有,我从eclipse迁移到android-studio一个月。新的构建工具很烦人。

每当我git clone一个android-sample时,需要几个小时来下载它的gradle-wrapper。

我发现有许多版本的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    .

有没有一种从github构建项目的简单方法?

为什么android-studio不使用我的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

我尝试了android-studio设置use local gradle distribution,并遇到了编译错误:

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 2.2版。目前的版本是2.1。如果使用gradle包装器,请尝试将/home/ninja/src/goshawk/13_GeexFinanceSource/Geexfinance/gradle/wrapper/gradle-wrapper.properties中的distributionUrl编辑为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

我需要你的帮助,任何想法?

2 个答案:

答案 0 :(得分:3)

来自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.

基本上,一旦你拥有二进制文件gradle-2.1-bin.zip和包装器jar gradle-wrapper.jar,你就可以使用正确的gradle-wrapper.propertiesgradlew可执行文件随意移动它。

项目的文件夹结构将是:

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

gradlew可执行文件在gradle-wrapper.jar

中查找./gradle/wrapper/

以下是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

./gradlew中运行Project1时,它会提取到~/.gradle/wrapper/dists/gradle-2.1-bin/

只需将Project1/gradle*复制到Project2/即可重复使用包装。

答案 1 :(得分:0)

我有同样的错误,刚才我修好了! 在根build.gradle中输入

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

在“修复Gradle设置”对话框中修改Gradle设置也很有用。或者,您可以手动下载gradle-2.2-all.zip,将其放入{project_dir}-gradle-wrapper并在终端中运行gradlew.bat