Why there are multiple copies for the same version of gradle

后端 未结 3 498
一个人的身影
一个人的身影 2020-12-09 01:01

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
         


        
相关标签:
3条回答
  • 2020-12-09 01:16

    It's an Unresolved issue Why Gradle download wrapper multiple times for the same version?

    0 讨论(0)
  • 2020-12-09 01:27

    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.

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

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

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

    For version 2.10, there is a directory name

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

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

    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.

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

    0 讨论(0)
  • 2020-12-09 01:35

    Saw same problem and made a try. Looks like that Android Studio does not build with the grale/wrapper/gradle-wrapper.jar in your project, but instead the one within itself(Android Studio.app/Contents/plugins/android/lib/templates/gradle/wrapper/gradle/wrapper/gradle-wrapper.jar in macOS).

    As mentioned by @alijandro, if the gradle-wrapper.jar in your two projects are not using the same hash policy, or not the same as the Android Studio app, it will cause the problem you saw.

    To get rid of this, just copy the gradle-wrapper.jar from Android Studio app directory to your project and you're all set.

    0 讨论(0)
提交回复
热议问题