59

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.

Community
  • 1
  • 1
Niklas
  • 23,674
  • 33
  • 131
  • 170

18 Answers18

71

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.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
34

Build->clean Project ,and it worked

user2955733
  • 385
  • 3
  • 4
  • 1
    I'm not sure how (I tried this a few times before with no results), but after I tried again (after seeing this comment) .. MIRACLE!! :D – smiron Apr 21 '15 at 21:20
  • 1
    my life is going to get a whole lot easier once I start remembering to try this first any time I have a gradle issue – hair raisin Nov 05 '15 at 13:43
  • It worked when I run 'clean' task which deletes /app/build folder. – Venugopal Aug 23 '16 at 04:29
23

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

Evans Kakpovi
  • 312
  • 1
  • 5
  • 12
10

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'
}
Raj Nandan Sharma
  • 3,694
  • 3
  • 32
  • 42
  • This is the only solution that worked for me using the "cordova-plugin-googleplus" and "phonegap-facebook-plugin" plugins. You save my day thanks! – S. F. Apr 15 '16 at 07:55
  • I was trying to install cordova-plugin-document-viewer and this solution worked for me. Thanks! – dzylich May 15 '17 at 23:51
7

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.

Dave MacDonald
  • 101
  • 1
  • 3
3

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

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

Patrick
  • 33,984
  • 10
  • 106
  • 126
2

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"

geekydhaval
  • 178
  • 1
  • 12
1

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'
}
Niklas
  • 23,674
  • 33
  • 131
  • 170
1

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.

petrsyn
  • 5,054
  • 3
  • 45
  • 48
  • I was having this same problem on android studio however I didn't have a support-annotations in my build.gradle. Cleaning the project didn't fix it. I found that despite not having the android-support-annotations.jar in my app build.gradle it was still in my app.iml. Once I deleted the android-support-annotations.jar from my app.iml (and cleaned the project again for good measure). This fixed my problem completely – Luke Apr 23 '15 at 05:02
1

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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
1

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.

JJD
  • 50,076
  • 60
  • 203
  • 339
Neamar
  • 381
  • 4
  • 7
1

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.

Clairton Luz
  • 2,116
  • 19
  • 15
0

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.

Oliver
  • 66
  • 1
  • 3
0

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.

Parth
  • 1
0

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

sumeet kumar
  • 351
  • 3
  • 15
0

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.

Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
0

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

Rahi.Shah
  • 1,305
  • 11
  • 20