Why there are multiple copies for the same version of gradle

I have an Android studio project, and the gradle/wrapper/gradle-wrapper.properties configured as follows.

 #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 

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

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

When I call the ./gradlew command to create a project. I have to use gradle-2.2.1-all.zip to build.

But this is not so, it will load another gradle even for the same version. So, for version 2.2.1-all there are two gradients. Since my Internet connection is very slow, it takes too much time.

 .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 

This is very annoying since it has to download a new one for the same version. I run a team to create my project.

Why could the gradle build system not select the installed one?

+3
source share
3 answers

The problem arose because the hash policy for the download URL is different from gradle-wrapper.jar and the latest gradle-wrapper.jar .

gradle-wrapper.jar in my Android application directory (I assume it is copied from android-sdk-macosx/tools/templates/gradle/wrapper/gradle/wrapper/gradle-wrapper.jar ) uses the following method to calculate the hash for the URL download addresses.

 // 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); } } 

But the last gradle-wrapper.jar uses the following method. The radius varies 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); } } 

The magic line I found in the directory name is the md5 hash line of the download URL.

For version 2.10 there is a directory name

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

And a4w5fzrkeut1ox71xslb49gst 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(); } 

Using the same hash method (use the same gradle-wrapper.jar ) for the same download gradle/wrapper/gradle-wrapper.properties from gradle/wrapper/gradle-wrapper.properties , there will not be multiple downloads for the same version of gradle.

This problem exists only between android studio project and another gradle project.

+5
source

I saw the same problem and tried. It seems that Android Studio is built not with grale/wrapper/gradle-wrapper.jar in your project, but with itself ( Android Studio.app/Contents/plugins/android/lib/templates/gradle/wrapper/gradle/wrapper/gradle-wrapper.jar on macOS).

As @alijandro mentioned, if gradle-wrapper.jar in your two projects does not use the same hash policy or does not match the Android Studio application, this will cause a problem.

To get rid of this, just copy gradle-wrapper.jar from the Android Studio application directory into your project, and you gradle-wrapper.jar done.

0
source

Source: https://habr.com/ru/post/1264131/


All Articles