UNEXPECTED TOP_LEVEL EXCEPTION Multiple dex files define Landroid/support/annotation/AnimRes

526 views Asked by At

After I've added the new library to my project, I started to get this error:

Error:Execution failed for task ':app:dexDebug'. com.android.ide.common.internal.LoggedErrorException: Failed to run command: D:\Android\sdk\build-tools\21.1.2\dx.bat --dex --no-optimize --output D:\Android\AndroidStudioProjects\Discounty\app\build\intermediates\dex\debug --input-list=D:\Android\AndroidStudioProjects\Discounty\app\build\intermediates\tmp\dex\debug\inputList.txt Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106)

I've looked through loads of similar questions on stackoverflow but didn't find the working solution.

Below's the screenshot of my project stucture and the build.gradle file of the library project (in libs folder), here's the link for the screenshot: enter image description here

And here's my root build.gradle file:

buildscript {
    repositories {
        jcenter()
        maven {
            url "https://jitpack.io"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://jitpack.io"
        }
    }
}

And here's the build.grandle from the app folder:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.discounty.discounty"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'cn.pedant.sweetalert:library:1.3'
    compile 'com.android.support:recyclerview-v7:21.0.0'
    compile 'com.getbase:floatingactionbutton:1.6.0'
    compile 'com.yalantis:contextmenu:1.0.0'
    compile('de.keyboardsurfer.android.widget:crouton:1.8.4@aar') {
        exclude group: 'com.google.android', module: 'support-v4'
    }
    compile project('libs:swipe_menu_list_view')
}

I tried cleaning the project, but it didn't help.

The new library that I've added (swipe_menu_list_view) contains the support-v4 dependency and I don't use support-v4 anywhere else in my project (or at least I think so), but I've read that this error might happen because different versions of the same library are used in one project.

Could you please help me with solving that problem?

1

There are 1 answers

3
Gabriele Mariotti On BEST ANSWER

It happens when you are using 2 different versions of the same library.

You can render the dependency tree with the command gradle dependencies

In your case:

'com.getbase:floatingactionbutton:1.6.0' uses

    <dependency>
          <groupId>com.android.support</groupId>
          <artifactId>support-annotations</artifactId>
          <version>19.1.0</version>
    </dependency>

while you appcompat 21.0.3 uses

<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>support-annotations</artifactId>
  <version>21.0.3</version>
  <scope>compile</scope>
</dependency>

You can check if there is a updated version that uses the same 21.0.3 or you can exclude the dependency with:

compile('com.getbase:floatingactionbutton:1.6.0') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }