Error 'can't write/can't read resource' when trying to build signed Apk

Hi, I’m trying to create a signed APK with proguard enable and I get these errors in gradle :

Note: there were 169 duplicate class definitions.
      (http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass)
Warning:can't write resource [META-INF/LICENSE.txt] (Duplicate zip entry [joda-time-2.7.jar:META-INF/LICENSE.txt])
Warning:can't write resource [META-INF/NOTICE.txt] (Duplicate zip entry [joda-time-2.7.jar:META-INF/NOTICE.txt])
Warning:can't write resource [.readme] (Duplicate zip entry [classes.jar:.readme])
Warning:can't write resource [META-INF/LICENSE.txt] (Duplicate zip entry [commons-lang3-3.2.1.jar:META-INF/LICENSE.txt])
Warning:can't write resource [META-INF/NOTICE.txt] (Duplicate zip entry [commons-lang3-3.2.1.jar:META-INF/NOTICE.txt])
Warning:can't write resource [META-INF/LICENSE.txt] (Duplicate zip entry [commons-io-1.3.2.jar:META-INF/LICENSE.txt])
Warning:can't write resource [META-INF/NOTICE.txt] (Duplicate zip entry [commons-io-1.3.2.jar:META-INF/NOTICE.txt])
:app:proguardAppRelease FAILED
Error:Execution failed for task ':app:proguardAppRelease'.
> java.io.IOException: Can't write [/Users/ne/Projects/myProject/app/build/intermediates/classes-proguard/app/release/classes.jar] (Can't read [/Users/me/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-io/1.3.2/b6dde38349ba9bb5e6ea6320531eae969985dae5/commons-io-1.3.2.jar(;;;;;;!META-INF/MANIFEST.MF)] (Duplicate zip entry [a/a/a/a/a.class == commons-io-1.3.2.jar:org/apache/commons/io/IOUtils.class]))

To fix it, I try to exclude some files as you can see in my build.gradle, but it’s still not working :

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3', 'com.newrelic.agent.android:agent-gradle-plugin:4.244.0'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'newrelic'

repositories {
    mavenCentral()
    maven { url "https://mint.splunk.com/gradle/" }
    maven { url 'https://dl.bintray.com/intercom/intercom-maven' }
}

android {
    signingConfigs {
        config {
            keyAlias ‘***'
            keyPassword ‘***'
            storeFile file(‘***')
            storePassword ‘***'
        }
    }
    compileSdkVersion 19
    buildToolsVersion '21.1.2'
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 230
        versionName "2.3.0 RC1"
        multiDexEnabled true
    }
    productFlavors {
        app {
            applicationId "com.android.myproject"
            versionCode 230
            versionName "2.3.0 RC1"
            signingConfig signingConfigs.config
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude '.readme'
        exclude 'LICENSE.txt'
        exclude 'README.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/MANIFEST.MF'
    }
    dexOptions {
        javaMaxHeapSize "4g"
        incremental true
    }
}

dependencies {
    compile 'joda-time:joda-time:2.7'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.google.guava:guava:15.0'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'com.newrelic.agent.android:android-agent:4.244.0'
    compile 'com.googlecode.libphonenumber:libphonenumber:7.0.3'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(‘:commonlib')
    compile 'com.google.android.gms:play-services:5.0.+'
    compile 'com.github.amlcurran.showcaseview:library:5.0.0'
    compile('io.intercom.android:intercom-sdk-base:1.+@aar') {
        transitive = true
    }
} 

Also I have to use a proguard-rules.pro files to avoid some warnings which prevent me to build my signed apk :

-dontwarn com.google.**

-dontwarn com.amazonaws.auth**
-dontwarn com.amazonaws.services**
-dontwarn com.amazonaws.util**

-dontwarn com.octo.android.robospice.persistence**
-dontwarn com.octo.android.robospice.retrofit**

-dontwarn com.squareup.okhttp.internal**

-dontwarn com.squareup.okhttp.internal**

-dontwarn io.intercom.android.sdk.conversation**

-dontwarn org.joda.time**
  • I’ve also tried to add this line: -outjars library.jar(!META-INF/MANIFEST.MF)
    to my proguard-rules.pro, I don’t know if it’s really better but with this line above added, I get this error :

    Error:Execution failed for task ‘:app:proguardAppRelease’.

    java.io.IOException: The output jar [/Users/me/Projects/myProject/app/build/intermediates/classes-proguard/app/release/classes.jar] must have a filter, or all subsequent output jars will be empty.

  • Another try was to add in my build.gradle of a sub project that I used:

    compile(‘com.octo.android.robospice:robospice-retrofit:1.4.14’) {
    exclude group: ‘commons-io’, module: ‘commons-io’
    }

But I got this error :

Warning:retrofit.client.OkClient: can't find referenced class com.squareup.okhttp.OkUrlFactory
Warning:there were 4 unresolved references to classes or interfaces.
         You may need to add missing library jars or update their versions.
         If your code works fine without the missing classes, you can suppress
         the warnings with '-dontwarn' options.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
:app:proguardAppRelease FAILED
Error:Execution failed for task ':app:proguardAppRelease'.
> java.io.IOException: Please correct the above warnings first.

So I’ve added in my proguard-rules.pro this line :

-dontwarn com.squareup.okhttp.OkUrlFactory

But at the end I get a new duplicate zip error, relative to retrofit :

Note: there were 106 duplicate class definitions.
(ProGuard Manual: Troubleshooting | Guardsquare)
Warning:can’t write resource [META-INF/LICENSE.txt] (Duplicate zip entry [joda-time-2.7.jar:META-INF/LICENSE.txt])
Warning:can’t write resource [META-INF/NOTICE.txt] (Duplicate zip entry [joda-time-2.7.jar:META-INF/NOTICE.txt])
Warning:can’t write resource [META-INF/LICENSE.txt] (Duplicate zip entry [commons-lang3-3.3.2.jar:META-INF/LICENSE.txt])
Warning:can’t write resource [META-INF/NOTICE.txt] (Duplicate zip entry [commons-lang3-3.3.2.jar:META-INF/NOTICE.txt])
Warning:can’t write resource [.readme] (Duplicate zip entry [classes.jar:.readme])
:app:proguardAppRelease FAILED
Error:Execution failed for task ‘:app:proguardAppRelease’.

java.io.IOException: Can’t write [/Users/me/Projects/myPorject/app/build/intermediates/classes-proguard/app/release/classes.jar] (Can’t read [/Users/me/Projects/qudiniandroidclient/app/build/intermediates/exploded-aar/myPorject/mySubPorject/unspecified/jars/libs/retrofit-1.3.0.jar(;;;;;;!META-INF/MANIFEST.MF)] (Duplicate zip entry [b/a/a.class == retrofit-1.3.0.jar:retrofit/android/AndroidApacheClient.class]))

I’m a bit lost, I don’t really see what is wrong here,any help would be welcome. Thank you.

Hi Alexandre,

I am having similar (I think) problems when using proguard. The details are here: http://stackoverflow.com/questions/33026343/crashlytics-proguard-duplicate-zip-entry . If I find anything to try, I will add another reply here. If you find anything more to try, please reply on stackoverflow.

Thanks and good luck!

Mr. Unhappy