47

I'm trying to do a 'release' build on an Android app and I keep on getting this error of:

Unable to compute hash of /../AndroidStudioProjects/../classes.jar

And then when I look into that directory for 'classes.jar' the file isn't there. Do I have to create this file myself with a gradle task?

There's something going on with proguard here but it's not giving much useful information other than 'Unable to compute hash...."

Here's my gradle.build file:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.example"
        minSdkVersion 11
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
            buildConfigField "String", "SERVER_URL", '"http://10.0.2.2:3000"'
        }
        release {
            buildConfigField "String", "SERVER_URL", '"https://example.com"'
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debugRelease {
            buildConfigField "String", "SERVER_URL", '"https://example.com"'
            debuggable true
            jniDebuggable false
            renderscriptDebuggable false
            minifyEnabled false
            zipAlignEnabled true
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:recyclerview-v7:22.1.1'
    compile 'com.android.support:cardview-v7:22.1.1'
    compile 'com.google.android.gms:play-services-gcm:7.5.0'
}

If someone could help me debug this issue that'd be great.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
Jamaal
  • 1,469
  • 1
  • 16
  • 32

8 Answers8

41

All the current answers to this question are just giving the Proguard rules that worked for them, every fix will be different. First off, confirm it's a Proguard problem by checking that the classes-proguard directory is somewhere in the error message, something like this: Unable to compute hash of /Users/Documents/projectX/app/build/intermediates/classes-proguard/release/classes.jar

This means it's caused by an earlier Proguard error so you need to scroll up in the Messages window or Gradle Console window and check what warnings or errors you're getting. Just as an example, in my current project, Square's Picasso library is causing the error: Warning: com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.OkHttpClient. I just added -dontwarn com.squareup.okhttp.** to ignore the warnings, and the app still worked as normal.

A lot of the Proguard errors will be warnings about some class so just adding -dontwarn for whatever class is causing it in your project often works.

I know the StackOverflow way is just Google the error message, copy and paste the top answer and hope for the best but here you've to understand it a bit and figure out the proguard rules for you!

georgiecasey
  • 21,793
  • 11
  • 65
  • 74
  • And what if there are no Proguard errors in the Message or Gradle Console windows, or when assembled on the command line? No errors at all before "Unable to compute hash..." – Kevin Krumwiede Jun 13 '16 at 23:54
  • 1
    Incidentally, you're right. But I was only able to fix it because I recently changed a library version that I happened to know added a new transitive dependency. (Because I wrote the library.) I never did find any error message from Proguard. – Kevin Krumwiede Jun 14 '16 at 05:49
21

I figured out the problem:

Open up the proguard-rules.pro for your project and add this to the bottom:

-dontwarn java.nio.file.Files
-dontwarn java.nio.file.Path
-dontwarn java.nio.file.OpenOption
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

Basically how I solved it was this I tried to run my app in 'release' mode and got a bunch of errors similar to this guy here: https://github.com/square/okio/issues/144

I pretty much followed what he said and fixed it.

Hope this can help others with generating their APK's!

Jamaal
  • 1,469
  • 1
  • 16
  • 32
  • 5
    I am still getting same error after adding these lines in proguard-android.txt – Manish Agrawal Oct 15 '15 at 08:19
  • Same here. I was getting the error even after adding these lines to pro-gurad. I had to do a clean rebuild of the project to resolve some of the errors seen during compilation. Ironically those errors didnt show up while generating a signed build. – Nilesh Pawar Oct 21 '15 at 15:09
  • For newcomers: please see an answer of @georgiecasey for his solution. Don't try to guess what classes are needed to dontwarn. – CoolMind Jan 28 '16 at 12:06
21

For me the following 2 lines inside proguard.cfg helped:

 -keep class com.google.android.gms.** { *; }
 -dontwarn com.google.android.gms.**
goRGon
  • 4,402
  • 2
  • 43
  • 45
  • 1
    there is no proguard-cfg in modern android studio – Bibaswann Bandyopadhyay Oct 16 '15 at 11:04
  • 1
    The filename is not important. It could be whatever you want. If there is no configuration for proguard yet, you can just add inside your build.gradle the following line: proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg' – goRGon Oct 19 '15 at 20:56
  • 1
    This worked for me. How did you figure it out? I didn't see any errors above the unable to compute hash message. No idea how I would fix this on my own. – Barodapride Mar 09 '16 at 02:51
  • I was just looking into different stackoverflow answers and tried to get the main idea from them =) – goRGon Mar 10 '16 at 00:08
3

I know there's already an answer here, but my situation was a little different, and I wanted to share it.

For me, my project's proguard file, namely, proguard-rules.pro, was somehow renamed to proguard-android.txt.

The proguard-android.txt is reserved for Android's default proguard rules, so essentially, I was overriding Android's proguard file, which was causing havoc, and I would get the Unable to compute hash message.

Once I changed the filename in my project from proguard-android.txt to proguard-rules.pro I was able to get things working.

WindsurferOak
  • 4,861
  • 1
  • 31
  • 36
1

I know this is an old question but maybe this answer works for someone. I added -dontwarn butterknife.** to proguard then gradle builds properly.

mavixce
  • 338
  • 3
  • 15
1

For me, it is showing the same error i solved it make sure you have the correct Keystore password in file

\mobile\android\gradle.properties 
MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=my-key-alias
MYAPP_UPLOAD_STORE_PASSWORD=yourpassword
MYAPP_UPLOAD_KEY_PASSWORD=yourpassword
0

Just create a file proguard-rules.pro and add "-dontwarn package names" that gave you warning on the message.

0

Disabling the minifyEnabled option from my gradle file corrected the error in my build.

build.gradle

buildTypes {
       release {
              // DISABLE OPTION
              //minifyEnabled true
              proguardFiles 'proguard.cfg'
       }
}
  • Do you understand, that you switched-off obfuscation done by ProGuard, and you can also comment out the line with `proguardFiles ...`, as there's no ProGuard running in your release build? – Ped7g Dec 30 '16 at 17:25
  • This solutions is unacceptable, it turns off an obfuscation. – Ufkoku Nov 21 '17 at 12:28