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!

Leave a Comment