Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Gradle Error: Multiple dex files define

Tags:

android

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

like image 415
My House Avatar asked Apr 03 '14 23:04

My House


People also ask

Why multidex is required?

By adding this library, your app can manage the access of additional DEX files. In other words, if you are having more than 64K methods, then you will be having more than one DEX file and these DEX files will be managed by using this multidex support library. Then, modify the module-level build.

Why multidex is required in Android?

This number represents the total number of references that can be invoked by the code within a single Dalvik Executable (DEX) bytecode file. This page explains how to move past this limitation by enabling an app configuration known as multidex, which allows your app to build and read multiple DEX files.

What is the maximum number of methods supported by dex compiler before needing to enable multidex?

dex file can have 65,536 methods(references) so if the number of references exceeds 65,536, you go with multidex.

What is multidex enabled?

Android applications by default have SingleDex support which limits your application to have only 65536 methods(references). So multidexEnabled = true simply means that now you can write more than 65536 methods(references) in your application.


2 Answers

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

dexOptions {
    preDexLibraries = false
}
like image 161
Aaron Dancygier Avatar answered Oct 04 '22 08:10

Aaron Dancygier


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

Got to Build > Clean Project

See image for reference

like image 41
Shubham Kushwah Avatar answered Oct 04 '22 07:10

Shubham Kushwah