为什么同一个版本的gradle会有多个副本

Why there are multiple copies for the same version of gradle(为什么同一个版本的gradle会有多个副本)

本文介绍了为什么同一个版本的gradle会有多个副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android studio项目,文件gradle/wrapper/gradle-wrapper.properties配置如下.

I have an android studio project, with the file gradle/wrapper/gradle-wrapper.properties configured as following.

#Wed Apr 10 15:27:10 PDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-2.2.1-all.zip

我的主目录中安装了 2.2.1-all 版本.

And I have the 2.2.1-all version installed in my home directory.

.gradle/wrapper/dists/gradle-2.2.1-all/c64ydeuardnfqctvr1gm30w53/gradle-2.2.1-all.zip

当我调用 ./gradlew 命令来构建项目时.我应该使用 gradle-2.2.1-all.zip 来构建.

When I invoke ./gradlew command to build the project. I should use the gradle-2.2.1-all.zip to build.

但它不会,即使是相同版本,它也会下载另一个 gradle.因此,版本 2.2.1-all 有两个 gradle.因为我的网络连接很慢,所以需要很长时间.

But it doesn't, it will download another gradle even for the same version instead. So, there are two gradles for the version 2.2.1-all. Because my internet connection is very slow, it takes too long.

.gradle/wrapper/dists/gradle-2.2.1-all/c64ydeuardnfqctvr1gm30w53/gradle-2.2.1-all.zip
.gradle/wrapper/dists/gradle-2.2.1-all/6dibv5rcnnqlfbq9klf8imrndn/gradle-2.2.1-all.zip

这很烦人,因为每次我调用命令来构建我的项目时,它都必须为同一版本下载一个新版本.

It's very annoying since it has to download a new one for the same version very time I invoke the command to build my project.

为什么 gradle 构建系统无法选择已安装的系统?

Why the gradle build system couldn't pick the installed one?

推荐答案

出现问题是因为s​​tudio的gradle-wrapper.jar和最新的下载url的hash策略不同gradle-wrapper.jar.

The problem occurred is because the hash policy for the download url is different between studio's gradle-wrapper.jar and latest gradle-wrapper.jar.

我的 Android 应用目录下的 gradle-wrapper.jar(我猜它是从 android-sdk-macosx/tools/templates/gradle/wrapper/gradle/wrapper/gradle-wrapper.jar)使用下面的方法计算下载url的hash.

The gradle-wrapper.jar under my Android app directory (I guess it's copied from android-sdk-macosx/tools/templates/gradle/wrapper/gradle/wrapper/gradle-wrapper.jar) use the following method to calculate hash for the download url.

// PathAssembler.java
private String getMd5Hash(String string) {
    try {
        MessageDigest e = MessageDigest.getInstance("MD5");
        byte[] bytes = string.getBytes();
        e.update(bytes);
        return (new BigInteger(1, e.digest())).toString(32);
    } catch (Exception var4) {
        throw new RuntimeException("Could not hash input string.", var4);
    }
}

但是最新的gradle-wrapper.jar使用下面的方法来做.基数从 32 变为 36.

But the latest gradle-wrapper.jar use the following method to do. The radix change from 32 to 36.

private String getHash(String string) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] bytes = string.getBytes();
        messageDigest.update(bytes);
        return new BigInteger(1, messageDigest.digest()).toString(36);
    } catch (Exception e) {
        throw new RuntimeException("Could not hash input string.", e);
    }
}

我在目录名中找到的魔法字符串是下载url的md5哈希字符串.

The magic string I found in the directory name is the md5 hash string of the download url.

对于2.10版本,有目录名

For version 2.10, there is a directory name

.gradle/wrapper/dists/gradle-2.10-all/a4w5fzrkeut1ox71xslb49gst

并且 a4w5fzrkeut1ox71xslb49gst 是从下载 url 散列的.

And the a4w5fzrkeut1ox71xslb49gst is hashed from the download url.

try {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    messageDigest.update("https://services.gradle.org/distributions/gradle-2.10-all.zip".getBytes());
    System.out.println(new BigInteger(1, messageDigest.digest()).toString(36));
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}

通过对来自 gradle/wrapper/gradle-wrapper.properties 的相同下载 url 使用相同的哈希方法(使用相同的 gradle-wrapper.jar),有同一版本的 gradle 不会被多次下载.

By using the same hash method (use the same gradle-wrapper.jar) for the same download url from gradle/wrapper/gradle-wrapper.properties, there won't be multiple downloads for the same version of gradle.

这个问题只存在于android studio项目和其他gradle项目之间.

This issue only exist between android studio project and other gradle project.

这篇关于为什么同一个版本的gradle会有多个副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么同一个版本的gradle会有多个副本

基础教程推荐