Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Proguard duplicate zip entry error - Android Support Library

I am trying to use Proguard in an android application which uses android library projects. My core android project depends on two android library projects. The core and the library projects have dependencies on android support library (in my case: android-support-v4.jar). When I try to obfuscate it with ProGuard, then I always get the following error (If the Proguarg is disabled in the Gradle script, then the compilation works fine.):

:MYANDROIDPROJECT:proguardDemoRelease FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':MYANDROIDPROJECT:proguardDemoRelease'.
> java.io.IOException: Can't write [MYANDROIDPROJECT_PATH\build\intermediates\classes-proguard\demo\release\classes.jar] (Can't read [MYANDROIDPROJECT_PATH\build\intermediates\exploded-aar\com.android.support\support-v4\21.0.3\classes.jar(;;;;;;!META-INF/MANIFEST.MF)] (Duplicate zip entry [classes.jar:android/support/v4/graphics/drawable/DrawableCompat$HoneycombDrawableImpl.class]))

The Gradle build script for my core project:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2+'
    }
}

apply plugin: 'android'

android {
    buildToolsVersion "19.1.0"
    compileSdkVersion 19

    lintOptions {
        abortOnError false
    }

    signingConfigs {
        localks {
            storeFile file("keystore/keystore")
            storePassword "xx"
            keyAlias "xx"
            keyPassword "xx"
        }
    }

    buildTypes {
        debug {
            debuggable true
            runProguard false       
            zipAlign true
            signingConfig signingConfigs.localks
        }

        release {
            debuggable false   
            zipAlign true
            proguardFile 'proguard.txt'
            runProguard true        
            signingConfig signingConfigs.localks
        }
    }

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        ndk{
            moduleName "S3DClient"
            abiFilter "armeabi-v7a"
            stl "gnustl_static"
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            resources.srcDirs = ['src/main/java']
            aidl.srcDirs = ['src/main/java']
            renderscript.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
            jni.srcDirs = []
        }
        paid {
            manifest.srcFile 'src/paid/AndroidManifest.xml'
            java.srcDirs = ['src/paid/java']
            res.srcDirs = ['src/paid/res']

            jni.srcDirs = []
        }
        demo {
            manifest.srcFile 'src/demo/AndroidManifest.xml'
            java.srcDirs = ['src/demo/java']
            res.srcDirs = ['src/demo/res']

            jni.srcDirs = []
        }
    }

    productFlavors {
        paid {
            applicationId = "hu.myprojectpackage1"
        }
        demo {
            applicationId = "hu.myprojectpackage2"
        }
    }
}

dependencies {
    compile files('src/main/libs/sqliteassethelper-2.0.1.jar',
                  'src/main/libs/S3DXAndroidTools.jar',
                  'src/main/libs/roboguice-2.0.jar',
                  'src/main/libs/open-javageom.jar',
                  'src/main/libs/openawt.geom.jar',
                  'src/main/libs/libGoogleAnalyticsServices.jar',
                  'src/main/libs/javax.inject-1.jar',
                  'src/main/libs/guice-3.0-no_aop.jar',
                  'src/main/libs/commons-lang3-3.1.jar',
                  'src/main/libs/bugsense-3.6.jar')
    compile fileTree(dir: "$buildDir/native-libs", include: '*.jar')

    compile 'com.android.support:support-v4:21.0.+'

    compile project(":lib/SlidingLayer")

    compile project(':lib/BlutoothCommunicationHandler') 
    paidCompile project(':lib/Licencing')
}

The Gradle build script for one of the library projects:

apply plugin: 'android-library'

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2+'
    }
}
dependencies {
   compile "com.android.support:support-v4:19.0.+"
}

android {
    buildToolsVersion "19.1.0"
    compileSdkVersion 19

    defaultConfig {
        minSdkVersion 4
        targetSdkVersion 18
    }
    dexOptions {
        preDexLibraries = false
    }

    lintOptions {
        abortOnError false
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

The (relevant part of) ProGuard config is:

-libraryjars src/main/libs/android-support-v4.jar

-keep class android.** { *; }
-keep class android.support** { *; }

-dontnote
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-dontwarn org.openawt.**
-dontwarn android.support.**

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose

-assumenosideeffects class android.util.Log {
    public static *** e(...);
    public static *** w(...);
    public static *** wtf(...);
    public static *** d(...);
    public static *** v(...);
}
-keepattributes Signature
-keepattributes *Annotation*
like image 229
BertZ Avatar asked Mar 17 '23 01:03

BertZ


1 Answers

Main Project build.gradle:

Take some time to convert these local jars:

dependencies {
    compile files('src/main/libs/sqliteassethelper-2.0.1.jar',
                  'src/main/libs/S3DXAndroidTools.jar',
                  'src/main/libs/roboguice-2.0.jar',
                  'src/main/libs/open-javageom.jar',
                  'src/main/libs/openawt.geom.jar',
                  'src/main/libs/libGoogleAnalyticsServices.jar',
                  'src/main/libs/javax.inject-1.jar',
                  'src/main/libs/guice-3.0-no_aop.jar',
                  'src/main/libs/commons-lang3-3.1.jar',
                  'src/main/libs/bugsense-3.6.jar')
    compile fileTree(dir: "$buildDir/native-libs", include: '*.jar')

    compile 'com.android.support:support-v4:21.0.+'

    compile project(":lib/SlidingLayer")

    compile project(':lib/BlutoothCommunicationHandler') 
    paidCompile project(':lib/Licencing')
}

To Maven Dependencies that can be managed easily(I tried to find most of them for you!):

dependencies {
    // compile fileTree(dir: 'libs', include: '*.jar') // all others
    compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
    compile file('src/main/libs/S3DXAndroidTools.jar')
    compile 'org.roboguice:roboguice:2.0' // latest is 3.0.1
    compile 'com.google.android.gms:play-services:6.1.71' // latest is 6.8.57 (issues)
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'com.bugsense.trace:bugsense:3.6'
    compile fileTree(dir: "$buildDir/native-libs", include: '*.jar')
    compile 'com.android.support:support-v4:21.0.3'
    compile project(":lib/SlidingLayer")
    compile project(':lib/BlutoothCommunicationHandler') 
    paidCompile project(':lib/Licencing')
}

After that, you should be able to use android.packagingOptions:

    packagingOptions {
        exclude '.readme'
        exclude 'LICENSE.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/README.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }

Library Project build.gradle

Your library project needs to be changed from:

dependencies {
   compile "com.android.support:support-v4:19.0.+"
}

to:

dependencies {
   compile 'com.android.support:support-v4:21.0.3'
}

Your main issue was that you had two different com.android.support:support-v4, one in your library project and one in your main project.

like image 172
Jared Burrows Avatar answered Mar 22 '23 07:03

Jared Burrows