11

My Project was working fine, when I have added facebook sdk into my project I have an error like this, I have tried so many ways to fix this, but I didn't. What should I do?

  Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: bolts/AggregateException.class

My App gradle is below

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        applicationId "com.example.myproject"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    compile project(':facebook')
}

And here is the facebook build.gradle

apply plugin: 'com.android.library'

repositories {
  mavenCentral()
}

project.group = 'com.facebook.android'

dependencies {
    compile 'com.android.support:support-v4:[21,22)'
    compile 'com.parse.bolts:bolts-android:1.1.4'
}

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
    }

    lintOptions {
        abortOnError false
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

apply plugin: 'maven'
apply plugin: 'signing'

def isSnapshot = version.endsWith('-SNAPSHOT')
def ossrhUsername = hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
def ossrhPassword = hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""

task setVersion {
    // The version will be derived from source
    project.version = null
    def sdkVersionFile = file('src/com/facebook/FacebookSdkVersion.java')
    sdkVersionFile.eachLine{
        def matcher = (it =~ /(?:.*BUILD = \")(.*)(?:\".*)/)
        if (matcher.matches()) {
          project.version = matcher[0][1]
          return
        }
    }
    if (project.version.is('unspecified')) {
      throw new GradleScriptException('Version could not be found.', null)
    }
}

uploadArchives {
    repositories.mavenDeployer {
        beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

        repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
            authentication(userName: ossrhUsername, password: ossrhPassword)
        }

        snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
            authentication(userName: ossrhUsername, password: ossrhPassword)
        }

        pom.project {
            name 'Facebook-Android-SDK'
            artifactId = 'facebook-android-sdk'
            packaging 'aar'
            description 'Facebook Android SDK'
            url 'https://github.com/facebook/facebook-android-sdk'

            scm {
                connection 'scm:git@github.com:facebook/facebook-android-sdk.git'
                developerConnection 'scm:git@github.com:facebook/facebook-android-sdk.git'
                url 'https://github.com/facebook/facebook-android-sdk'
            }

            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'https://github.com/facebook/facebook-android-sdk/blob/master/LICENSE.txt'
                    distribution 'repo'
                }
            }

            developers {
                developer {
                    id 'facebook'
                    name 'Facebook'
                }
            }
        }
    }
}

uploadArchives.dependsOn(setVersion)

signing {
    required { !isSnapshot && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

afterEvaluate {
    androidJavadocs.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
}
Muhammet Demir
  • 1,995
  • 5
  • 21
  • 42
  • Based on the following http://stackoverflow.com/questions/27694355/unable-to-execute-dex-multiple-dex-files-define-lbolts-aggregateexception and also http://stackoverflow.com/questions/26059838/duplicate-zip-entry-after-gradle-plugin-v0-13-1 , you seem to need to exclude the module from the Facebook SDK dependency `exclude group: 'com.parse.bolts', module: 'bolts-android'` please try this – EpicPandaForce Mar 01 '15 at 09:13
  • 1
    I guess in your my app build.gradle your libs folder includes "support-v4" jar of different version. Try to remove that and run it again... – dudego Mar 01 '15 at 09:13
  • @EpicPandaForce Where should I add exclude group: 'com.parse.bolts', module: 'bolts-android' – Muhammet Demir Mar 01 '15 at 10:08
  • I have solved this with "multiDexEnabled=true" but now given error as follow Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'. > java.util.zip.ZipException: duplicate entry: bolts/AggregateException.class – Muhammet Demir Mar 01 '15 at 10:39
  • 1
    `compile project(':facebook') { exclude group: 'com.parse.bolts', module: 'bolts-android' }` if it works – EpicPandaForce Mar 01 '15 at 21:44
  • Are you doing any updates to Facebook SDK? If not you can link it in from maven as well: `compile com.facebook.android:facebook-android-sdk:3.23.0`. Similarly you can bring in the bolts jar from maven, and it should dedupe.. – Gokhan Caglar Mar 02 '15 at 20:58
  • I have tried both of them but still continue. – Muhammet Demir Mar 02 '15 at 21:12
  • any update on this? i tried `compile (project(':FacebookSDK')) { exclude (group: 'com.parse.bolts', module: 'bolts-android') }` but still the same – Shing Mar 05 '15 at 22:01

5 Answers5

19

Now they split bolts-android into bolts-applinks and bolts-tasks .so you need exclude both from the gradle build

compile ('com.facebook.android:facebook-android-sdk:4.10.0'){
exclude group: 'com.parse.bolts',
        module: 'bolts-tasks'
exclude group: 'com.parse.bolts',
        module: 'bolts-applinks';}

This works perfectly for me !!!!

prasanthMurugan
  • 597
  • 6
  • 21
15

For me, I was adding Facebook SDK as a project, and set it as dependencies.

However, the exclude work after i switching to use the maven source.

I think it is for maven only, not for project dependencies? (please provide correct info if someone know about this)

In other word, you can now delete the Facebook SDK project and files.

remember to add

repositories {
    mavenCentral()
}

if you weren't using maven.

So the build.gradle look like this, I commented out the project way.

repositories {
    mavenCentral()
}
dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':google-play-services_lib')
    compile ('com.facebook.android:facebook-android-sdk:3.23.0'){
        exclude module: 'bolts-android'
        exclude module: 'support-v4'
    }
//    compile (project(':FacebookSDK')){
//        exclude module: 'bolts-android'
//        exclude module: 'support-v4'
//    }
    compile (project(':UserVoiceSDK')){ exclude module: 'support-v4' }
}
Shing
  • 1,255
  • 1
  • 10
  • 18
5

I've had a similar problem. This was really frustrating for me because everything worked fine and suddenly it broke for no reason.

The issue is hinted in duplicate entry: bolts/AggregateException.class. It's a clash of the Bolts library, used by Facebook and Parse.

For me, the problem lay in these two lines:

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.parse.bolts:bolts-android:1.1.4'

I had downloaded the Parse libraries and put them in the /libs/ folder. The problem was that there was another bolts-android file in that folder.

The solution is to delete that library and keep the compile 'com.parse.bolts:bolts-android:1.1.4' part.

Alternative problem

In my case, I used compile 'com.parse.bolts:bolts-android:1.+' instead of a specific version. This always takes the latest version. So when bolts upgraded to version 1.2.0, the thing just seemed to randomly break because all of a sudden the version in the /libs/ folder and the latest version no longer aligned.

Best practice is to avoid 1.+ style versioning and just keep checking and updating to the latest version every now and then.

Hope this helps someone.

Community
  • 1
  • 1
Muz
  • 5,866
  • 3
  • 47
  • 65
4

no need to remove any jar files. In Gradle file we have written these two lines

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.parse.bolts:bolts-android:1.1.4'

just remove

compile fileTree(dir: 'libs', include: ['*.jar'])

because we are compiling all the jar files and then again including the bolts to compile due to which the error is shown

Bhavana Johri
  • 347
  • 2
  • 13
0

in my case the i had added a .jar in the library code . the library in turn gets used in the main app . the dex was still in the cache file even if i had cleaned my project and installed it . To be sure in the main app you can check the count of the library . The cache file is "Project -> build -> dex-cache -> cache.xml" . If you have multiple counts of the library then u need to do this in Android Studio -> File -> invalidate cache / restart