問題描述
我有一個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會有多個副本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!