UNEXPECTED TOP-LEVEL EXCEPTION Multiple dex files define - Android Studio

1:25:00 AM , 0 Comments

As I see new android Studio user face this problem when they are debugging or run the project.

However, this is not a big problem.many developers waste lot's of time to solve this issue.
Actually we need to understand why show this error? when we run the project.

If we implement the multiple libraries in the same project. then project conflicted with both libraries.

Don't worry we have a solution for this. In this article, I will write down 4 solution for this problem step by step.

If you face the problem please read the solution step one by one and apply the process to your error project. I hope you can solve the problem Error:Execution failed for task ':app:dexDebug'.  

the problem are duplicate dx.bat file that's why new file are not generated by IDE
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
 C:\Android\android-studio\sdk\build-tools\19.0.3\dx.bat --dex --output C:\Newproject\TraditionalMehendi\app\build\dex\debug C:\Newproject\TraditionalMehendi\app\build\classes\debug C:\Newproject\TraditionalMehendi\app\build\dependency-cache\debug C:\Newproject\TraditionalMehendi\app\build\pre-dexed\debug\classes-48d144ddea6f49f726182658d2cd8e370ccc3aaa.jar C:\Newproject\TraditionalMehendi\app\build\pre-dexed\debug\classes-9f4d5885611b2cd25e7d8eab67c143627ce2b90e.jar C:\Newproject\TraditionalMehendi\app\build\pre-dexed\debug\cordova-2.9.0-ed7f0ff9375df6df791502102448d1e17b9157d8.jar C:\Newproject\TraditionalMehendi\app\build\pre-dexed\debug\google-play-services-951d8a946461792fa0ed722eaf024834954f8800.jar C:\Newproject\TraditionalMehendi\app\build\pre-dexed\debug\libGoogleAnalyticsServices-b42dc6d8aa0346a80f1328d54575ffcd87dd725e.jar C:\Newproject\TraditionalMehendi\app\build\pre-dexed\debug\support-v4-19.1.0-949793351ba251d9ea726b83e38d287075a5b846.jar
Error Code:
 2
Output:
 UNEXPECTED TOP-LEVEL EXCEPTION:
 com.android.dex.DexException: Multiple dex files define Lcom/google/ads/AdRequest$ErrorCode;
  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)
Solution for Fix this problem

Step 1: Your google play services library is being exported from other dependencies of your project and at the compile time the dex compiler gets confused.
If you're using Gradle then including this in your project's build.gradle should exclude the support library from being exported into your main project.

apply plugin: 'android'
apply plugin: 'crashlytics'

/** Must exclude exported support jars from dependencies, or get dex duplicate class      error.
*  we're
**/
configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
    all*.exclude group: 'com.google.android.gms', module: 'play-services'
} 

If you're using the android studio build system. Then you should go to File -> project structure and disable -> modules.

Go through each module the and click on the dependency tab, unchecked the export column for for the support library and google play services library.

Step 2:Make sure you have downloaded Support Repository to use support library dependency in build.gradle.


If these all are there already installed sync your project with gradle once using the button available.


Step 3:Because you may include two same libs in your project. check your build.gradle file.

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile files('libs/android-support-v4.jar') 
 } 

if your file includes compile 'com.android.support:appcompat-v7:+' and compile files('libs/android-support-v4.jar'), it will have this problems. delete this sentence: compile files('libs/android-support-v4.jar')
To Change Make it

dependencies {
    compile 'com.android.support:appcompat-v7:+' 
 } 

Steep 4: If you are try the way but not solve the problem than this this another way . hope you will solve the problem .

Simple go to  Project Folder Remove  .gradle  .idea  folder 


And Go to yourproject\apps\build Remove All Directory in this Build Folder Now go to Android Studio And sync your project with gradle once using the button.

And Sunc the project you will now debug the apps hope you never face this problem in this project again


If you again face this problem then post a comment here I will hardly try to solve your problem.
But to better understand your project error. can you please show your build.gradle file information when you ask solution here.

without seeing your build.gradle file information provided a solution for you is quite hard for my side.

One last think here in this article comment section many developers solve this problem with this article help. So don't forget to see how they solve the problem.

Abdullah Al Mahamud,

Abdullah Al Mahamud

Abdullah Al Mahamud is a programmer and founder of Thedevline an Inspiration blog since 2014. live in Bangladesh. , 28 years old.He is passionate about Technology and Design.He work on the freelancing marketplace as a freelancer and has also written for this blog.. Know more


Let's get connected with: Facebook Twitter Google+ Instagram

Content may not be re-published without permission.
Some pages on this site include affiliate links.

0 comments: