When you build an android project in android studio, you may encounter DexArchiveMergerException unable to merge dex exception. The detail error message is as below.
Error:Execution failed for task ‘:app:transformDexArchiveWithExternalLibsDexMergerForDebug’.
> java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
If you still can not fix this error after struggle for taking some actions such as clean the project, even restart the android studio, you need to look at the error messages carefully.
In my environment, i finally resolve above error by change the dependencies library version in the Module:app build.gradle file follow below steps.
- Double click the Module:app build.gradle file in android studio left panel to open it.
- Look at each warning message in right panel build gradle content carefully to know the reason of the error.
- In my build.gradle i found the warning message is that This support library should not use a different version (26) than the compileSdkVersion (27) for the dependency library ‘com.android.support:design:26.1.0’ . So i change the version to ‘com.android.support:design:27.1.0’.
- After change the version, click Sync Now at the top right corner and rebuild the project again, you will find the error has been fixed.
- Conclusion : When you encounter build error in android studio, you had better look at the build.gradle file content carefully. Especially for the dependencies library with warning messages. Such error occurred usually because of dependency library version mis match.