19

I'm getting this error when trying to build my project in Android Studio (I'm trying to migrate from Eclipse):
Output:

UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)

after googling and checking stackoverflow the most likely cause is duplicate library references.
I have a number of libraries in my project such as Facebook SDK, SlidingMenu, etc. that required android-support-v4.jar, so I copied that into my main project's /libs folder and subsequently removed it from all the other projects libs folder. Then I went to "Open Module Settings" and added the support-v4 as a library dependency for the libraries that needed it (e.g. Facebook SDK).
Then clean project -> run but I'm still getting this multiple dex files error

running gradle -q dependencies shows:

+--- project :facebook-android-sdk-3.5.1:facebook
|    \--- com.android.support:support-v4:+ -> 19.1.0
+--- project :SlidingMenu:library
|    \--- com.android.support:support-v4:+ -> 19.1.0

Is that okay? If I remove this library dependency in the Module Settings then I will get even more errors while trying to compile my project

Howli
  • 12,291
  • 19
  • 47
  • 72
My House
  • 759
  • 2
  • 6
  • 18

4 Answers4

52

In your build.gradle file try adding the following block inside your android block.

dexOptions {
    preDexLibraries = false
}
Aaron Dancygier
  • 1,996
  • 19
  • 20
  • 2
    YES! I wasted the past 3 hours trying to figure this out; this is the answer that worked. – Drew Shafer Jul 23 '14 at 13:52
  • Am directly importing one project from eclipse(ADT) to studio in eclipse its working fine while importing that project with support libreary projects in to studio getting error multiple dex files found please help and provide solution – Harsha Feb 19 '15 at 10:12
  • 7
    Hi. Does anyone know why this works. It's nice to know the solution to my problem, but I'm curious about the reason. – Rick Sanchez Apr 09 '16 at 23:26
  • You should mention that it must be inside the android {} block in build.gradle file. – Dr. Ehsan Ali Aug 05 '16 at 05:39
3

Just a Clean Build worked for me , Just do this in Android Studio:

Got to Build > Clean Project

See image for reference

Shubham Kushwah
  • 545
  • 1
  • 9
  • 16
3
dexOptions {
    preDexLibraries = false
}

and

Got to Build > Clean Project

and

Got to Build > Rebuild Project
Devix
  • 432
  • 5
  • 16
1

The correct answer is:

dexOptions {
    preDexLibraries = false
}
jiong103
  • 111
  • 7