59
votes

The moment I added the android support annotations to my dependencies

compile 'com.android.support:support-annotations:20.0.0'

I got this error:

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:594) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170) at com.android.dx.merge.DexMerger.merge(DexMerger.java:188) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287) at com.android.dx.command.dexer.Main.run(Main.java:230) at com.android.dx.command.dexer.Main.main(Main.java:199) at com.android.dx.command.Main.main(Main.java:103)

build.gradle

android {
    compileSdkVersion 19
    buildToolsVersion '20.0.0'

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 19
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.0.0'
    compile 'com.crashlytics.android:crashlytics:1.+'
    compile 'com.android.support:support-annotations:20.0.0'
}

Anybody else experienced this issue? I have tried the solutions from here.

18

18 Answers

71
votes

The problem is that android-support-annotations.jar used to be a separate library containing the android annotations, but for some reason these annotations are already included in recent versions of the android-support-v4.jar file.

Deleting the annotations jar solved the issue.

34
votes

Build->clean Project ,and it worked

23
votes

I deleted the android-support-v4.jar and it worked.

10
votes

If this is cordova / ionic project this worked for me

add these line to build.gradle under platforms/android after line number 22 i.e after apply plugin: 'android'

configurations {
   all*.exclude group: 'com.android.support', module: 'support-v4'
}
7
votes

Solved this exact issue in a Cordova project that used the facebook plugin. I was able to successfully build by commenting out this line from platforms\android\project.properties, as shown:

# cordova.system.library.1=com.android.support:support-v4:+

And by commenting out this line from platforms\android\build.gradle, as shown:

// compile "com.android.support:support-v4:+"

Then doing the build. The problem started when I installed (katzer/cordova-plugin-local-notifications) which added these lines, but it created a conflict since the library it was adding to the build was already part of the facebook plugin build.

3
votes

As other users said, the first elements to troubleshoot are dependencies. Although, sometimes you can struggle for hours and you don't find any problem so you can focus on the build process instead.

Changing the way in which the .dex files are produced sometimes solves the problem. You can go through these steps:

  • Open your Build.gradle (app) file
  • Search for the task dexOptions
  • Change it to:

    dexOptions {
      incremental false 
    }
    

If you don't find the task in your file then you can add it.

2
votes

For me the reason was the new data-binding lib

com.android.databinding:dataBinder:1.0-rc2

it somehow used a conflicting version of the annotations lib, which I could not force with

configurations.all {
    resolutionStrategy {
        force group: 'com.android.support', name: 'support-v4', version: '23.1.0'
        force group: 'com.android.support', name: 'appcompat-v7', version: '23.1.0'
        force group: 'com.android.support', name: 'support-annotations', version: '23.1.0'
    }
}

but the new rc3 and rc4 versions seem to have fixed it, so just use those versions

2
votes

I had the same problem , but i deleted build files from the build folder

projectname/app/build

and it removed all the related error. "can't clean the project" and also "dex errow with $anim"

1
votes

I managed to fix this issue. The reason was that I included the android support library 19.0.0 as a dependency, but 19.1.0 is required. See here for more information

So it has to be

dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.crashlytics.android:crashlytics:1.+'
    compile 'com.android.support:support-annotations:20.0.0'
}
1
votes

If you import AppCompat as a library project and you also have android-support-annotations.jar in libs elsewhere, make sure to import everywhere AppCompat library only (it already includes this annotations lib). Then delete all android-support-annotations.jar to avoid merging multiple versions of this library.

1
votes

Updating Android SDK Tools fixed it for me, now it just sees the copy in android-support-v4.jar.

I had the same problem when using ant, and the annotations library was being included automatically by an outdated sdk.dir/tools/ant/build.xml.

1
votes

Clean project works as a temporary fix, but the issue will reappear on next compilation error.

To fix more reliably, I had to update the dependency to android support-v4 to com.android.support:support-v4:22.2.0.

1
votes

Put in your build.gradle the dependency of support-annotations according with your compileSdkVersion. For instance: A project with the compileSdkVersion 25 you can put the following dependence:

compile 'com.android.support:support-annotations:25.0.1'

This will solve your problem.

0
votes

In my case I had a file called cache.xml under /build/intermediates/dex-cache/cache.xml in the root project folder. I deleted this file, rebuild the project and it worked for me.

0
votes

I deleted the android-support-v4.jar and it worked.

Explain - android-support-v4.jar is conflicting with my other .jar files of project\libs files ** specially when you are running with java 8 on AS.

0
votes

Put android-support-v4.jar in your libs folder in eclipse. Clean and build the project. It will resolve the issue.

0
votes

Another reason that messages such as these can come up in Android Studio when building and launching can be the cause of application tags in your libraries.

If you have several Android Library projects that you imported as modules. Go into those projects and remove the <application> ... </application> tags and everything between them. These can cause issues in the build process along with the support library issues already mentioned.

0
votes

From /platforms/android/libs/ delete android-support-v4.jar. It works for me.