【问题标题】:How to reuse gradle-wrapper?如何重用 gradle-wrapper?
【发布时间】:2015-03-13 15:31:39
【问题描述】:

所有,我从 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

我需要你的帮助,你知道吗?

【问题讨论】:

  • 什么编译错误?
  • @ScottBarta 我粘贴了上面的错误,似乎版本不匹配。在 build.gradle 中找不到 gradle-version,在哪里指定了版本?

标签: java gradle android-studio


【解决方案1】:

来自gradle-wrapper 文档:

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/ 中查找gradle-wrapper.jar

这是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

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

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

【讨论】:

    【解决方案2】:

    我遇到了同样的错误,刚刚我修复了它! 在根build.gradle 输入

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多