UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define android android

UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define


A little late to the game here but this is most likely a problem with the dependencies you have listed in your build.gradle file for you app.

After lots of testing i successfully chased down my problem and believe it could be of help to others.

Things I do not recommend:

Unless you have an absolute need to enable multiDex in your build.gradle DO NOT DO IT, this is just stepping over the underlying problem in your app and not getting to the root of it. You are also unnecessarily increasing the size of your apk, and there could be unexpected crashes when there is a conflicting method in your dex file.

Things to look out for:

Check all your dependencies in your build.gradle file. Are you referencing a dependency that also includes a dependency you have already included? For example, if your including appcompat-v7 there is no need to include appcompat-v4 since v7 includes all features from v4.

WHAT I ACTUALLY FOUND (MY ISSUE causing my app to exceed method limit in my dex file) ----> GOOGLE PLAY SERVICES

If you do not need all the google play services library dependencies STAY AWAY from this line in your build.gradle compile 'com.google.android.gms:play-services:8.3.0' and instead just use what you need!!

Google has a comprehensive list of the libraries for selectively compiling here

With all that said you probably only need to include this one line in gradle for your Google Analytics:

  dependencies{       compile 'com.google.android.gms:play-services-analytics:8.3.0'  }

EDIT

Also, you can view the dependency tree by going to the root of your project (or using terminal in Android studio) and running:

./gradlew app:dependencies

Good Luck and happy coding!

Update

Now as of Android Studio 2.2 you no longer need to trial and error whether you need to use multi-dex in your application. Use the Apk Analyzer to see if its really needed!


Explication: Building Apps with Over 65K Methods

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.

Note:This allows you to refer to all methods of the app. It is as if you have two modules (limit: 2 x 65K) but compacted into one. This entails a cost (time) in the build process.

Solution:

  1. You should try to format your code with libraries to remove excess classes and also not exceed the limit methods. For example if you use maps play-services ( com.google.android.gms: play-services: 8.1.0), you can change to (compile 'com.google.android.gms:play-services-maps:8.1.0') to eliminate unnecessary library dependencies. Then Sync Gradle in AndroidStudio and check if it run. If no run go to point 2.
  2. Add this on build.gradle (app module).
android {   ...   defaultConfig {      ...      multiDexEnabled true   }}


For me it was related to simplexml converter for retrofit 2. And it fixed by:

compile ("com.squareup.retrofit2:converter-simplexml:2.0.0-beta4"){ exclude module: 'stax' exclude module: 'stax-api' exclude module: 'xpp3'}