Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProGuard Warning: can't write resource (Duplicate zip entry)

First of all I know that this question already exists in stackoverflow, but I can't solve it, although I tried suggestions like not using -injar, using packagingOptions, etc

I know the release is going to work, but I like clean builds. When I generate signed apks. After a project clean, I get those errors:

Warning:can't write resource [.readme] (Duplicate zip entry [classes.jar:.readme])
Warning:can't write resource [META-INF/LICENSE.txt] (Duplicate zip entry [joda-time-2.8.1.jar:META-INF/LICENSE.txt])
Warning:can't write resource [META-INF/NOTICE.txt] (Duplicate zip entry [joda-time-2.8.1.jar:META-INF/NOTICE.txt])

Sometimes instead of joda-time-2.8.1.jar there is commons-collections4-4.0.jar (same .readme, licence, notice) warnings.

I tried excluding those with packagingOptions, but it doesn't work.

Here is my module's build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude '.readme'
    }

    defaultConfig {
        applicationId 'com.example.myapp'
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName '0.1'
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'org.apache.commons:commons-collections4:4.0'
    compile 'org.jsoup:jsoup:1.8.2'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:cardview-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.google.android.gms:play-services-gcm:7.5.0'
    compile 'joda-time:joda-time:2.8.1'
}

And my proguard-rules.pro file

# Defaults
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment

-keepnames class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>;
    !private <fields>;
    !private <methods>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclassmembers class * {
    public void *ButtonClicked(android.view.View);
}

# Use this to prevent errors in builds.
# usage of org.joda.time in this app does not need org.joda.convert
-dontwarn org.joda.convert.**


# Remove logs. Only when using 'proguard-android-optimize.txt'
-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}

Thanks in advance.

EDIT: As requested from a possible duplicate warning (Proguard warnings "can't write resource [META-INF/MANIFEST.MF] (Duplicate zip entry)"). User that asked the question, said he is using multiple modules (instead of one) and did not post any files (due to sensitive data), so I don't know if its the same case. Also tried all possible options from the first answer and none of them worked. Other answers didn't work either (except the -dontwarn option which I am not going to use for obvious reasons).

like image 332
tchar Avatar asked Oct 30 '22 22:10

tchar


1 Answers

I'm pretty sure you have this jar added on two locations.

1# are at your library project 2# are at your implementation project (that uses the library)

In this case, you can remove the jar from implementation project since it will be packed from the library.

Or.. maybe you have added the jar at your classpath, android will automatically get it from the libs folder, so no need to add it to the classpath.

like image 124
Marcos Vasconcelos Avatar answered Nov 15 '22 05:11

Marcos Vasconcelos