Android Studio Gradle Error: Multiple dex files define-ThrowExceptions

Exception or error:

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

How to solve:

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

dexOptions {
    preDexLibraries = false
}

###

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

Got to Build > Clean Project

See image for reference

###

dexOptions {
    preDexLibraries = false
}

and

Got to Build > Clean Project

and

Got to Build > Rebuild Project

###

The correct answer is:

dexOptions {
    preDexLibraries = false
}

Leave a Reply

Your email address will not be published. Required fields are marked *