10

I am facing a problem in android studio after adding JSON.simple and enabling MultiDex and get the following error:

Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'. java.util.zip.ZipException: duplicate entry: org/hamcrest/BaseDescription.class

Here is my build.gradle :

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.MildlyGoodApps.EffortlessDescriptions"
    minSdkVersion 10
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    multiDexEnabled true

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

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.googlecode.json-simple:json-simple:1.1.1'
compile 'com.android.support:multidex:1.0.0'
}

Thank you.

Fixed:

Changed compile 'com.googlecode.json-simple:json-simple:1.1.1' to compile('com.googlecode.json-simple:json-simple:1.1.1'){ exclude group: 'org.hamcrest', module: 'hamcrest-core' }.

Thank you Kane O'Riley!

Peter Warrington
  • 654
  • 10
  • 32
  • 1
    That means you have 2 or more dex files defining `org.hamcrest.BaseDescription`. Perhaps post your `build.gradle`, and check your dependencies for duplicate entries of this class. Poorly coded libraries sometimes include external dependency classes directly rather than referring to them correctly. – Kane O'Riley Sep 13 '15 at 03:15
  • Added build.gradle . – Peter Warrington Sep 13 '15 at 09:00
  • What jars do you have in your `libs` directory? – Kane O'Riley Sep 13 '15 at 09:02
  • I can't find the `libs` directory, but when I remove the line `compile fileTree(include: ['*.jar'], dir: 'libs')` nothing changes and I get the same error. – Peter Warrington Sep 13 '15 at 09:23
  • 2
    Can you post the output of running `./gradlew -q api:dependencies --configuration compile` from the project directory? Also, try changing the compile directive for `json-simple` to look like this: `compile('com.googlecode.json-simple:json-simple:1.1.1') { exclude group: 'org.hamcrest', module: 'hamcrest-core' }` – Kane O'Riley Sep 13 '15 at 09:38
  • Thank you. Adding `{ exclude group: 'org.hamcrest', module: 'hamcrest-core' }` worked! – Peter Warrington Sep 13 '15 at 10:01
  • 1
    Cool. I posted as an answer, so if it solved your issue please upvote and accept it :). – Kane O'Riley Sep 13 '15 at 10:03
  • I have accepted it but I haven't got enough reputation to upvote it! – Peter Warrington Sep 13 '15 at 10:09
  • Oh that's fine, accepting is the important part so others know this solved the issue for you. – Kane O'Riley Sep 13 '15 at 10:12

1 Answers1

22

Change your json-simple import to exclude the hamcrest dependency like this:

compile('com.googlecode.json-simple:json-simple:1.1.1') {
    exclude group: 'org.hamcrest', module: 'hamcrest-core'
}

This will prevent multiple copies of the dependency being included.

Kane O'Riley
  • 2,482
  • 2
  • 18
  • 27