Unexpected Top-Level Exception: Com.Android.Dex.Dexexception: Multiple Dex Files Define

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

The error occurs when you have the same library/directory included more than once in your build.gradle's dependencies. Ok, let’s say you have an App structure that looks like this:

Sample Image

So you have the main “app” and then you have a bunch of sub-apps/modules/libraries. The libraries are: 1) gene_test_library, 2) genes_nine_old_androids_library, & 3) swipe_list_view_library.

My name is Gene, so that’s why there are all these “gene” libraries.

Inside the build.gradle for “app”, I have:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'

compile project(':libraries:gene_test_library')
//compile project(':libraries:genes_nine_old_androids_library')
compile project(':libraries:swipe_list_view_library')
}

Inside the build.gradle for gene_test_library, I have nothing:

dependencies {
}

Inside build.gradle for gene_nine_old_androids_library, I have:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}

Inside build.gradle for swipe_list_view_library, I have:

dependencies {
compile 'com.nineoldandroids:library:2.4.0+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}

This line of code compile fileTree(dir: 'libs', include: ['*.jar']) just means “hey, look inside the ‘libs’ folder inside this module for any jar files. I do not have anything in the libs folder of any of the modules so you can ignore that line of code.

So let’s say I uncomment out //compile project(':libraries:genes_nine_old_androids_library')
In the build.gradle for the “app” module. Then I would get the “UNEXPECTED TOP-LEVEL EXCEPTION:” error. Why is that?

Sample Image

Well, writing //compile project(':libraries:genes_nine_old_androids_library') inside the build.gradle for “app”, is the same as taking the build dependencies of “genes_nine_old_androids_library” module and putting it there. So uncommenting the //compile project(':libraries:genes_nine_old_androids_library') statement, the build.gradle for “app” module becomes:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'

compile project(':libraries:gene_test_library')
***compile fileTree(dir: 'libs', include: ['*.jar'])***
***compile 'com.android.support:appcompat-v7:21.0.0'***
compile project(':libraries:swipe_list_view_library')
}

Notice how now compile 'com.android.support:appcompat-v7:21.0.0' shows up 2x. That’s where the error is coming from.

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!

UNEXPECTED TOP-LEVEL EXCEPTION: Multiple dex files define

This can be caused by both your app project and the library project bringing the android-support-v4 jar. If the library project is already bringing this jar as dependency then you'll need to exclude it from your app project classpath.

UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException

Solved,

I removed libraries, reload project, add libraries, removed my references to support-v4, removed libraries again, build, add libraries, build, added back references and build.

How to fix android Multiple dex files define exception

I made a pull request to the library after getting it to work in Android Studio.

If you get this error when trying to build

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

Or you don't see that, but any error saying local.properties is missing when doing an import, then add a local.properties file that points to your Android SDK location, then trying to import/rebuild.

For example on a Mac, that file would contain this line, but obviously change the file path to point at your respective SDK's location

sdk.dir=/usr/local/opt/android-sdk

Once done with that, it should load into Android Studio without a problem.

The code was also tested on a 4.2 emulator.


The easy way via a Git Terminal (could do the same with Git Desktop).

git clone https://github.com/cricket007/DeviceSDK /path/to/download/
cd /path/to/download/
git branch fixes

Then import /path/to/download/DeviceSDK into Android Studio as Import Project Gradle


The long way (via Android Studio)

Use https://github.com/cricket007/DeviceSDK.git as the Git repo

Import

Open Project, you can ignore most errors that popup

Go the the Menu Bar, select VCS > Enable Version Control Integration

Enable VCS

Choose Git

Go back to VCS > Git > Branches

Checkout fixes as a new local branch

Pick Branches

Checkout

Name the branch.

Choose Force Checkout if prompted

I don't know what clicking the Run button does with a Gradle project, but you should open Gradle View on the right side and open TestDemo, select installDebug

Gradle View

From here on you need the solution above for the local.properties file and enable ADB debugging on a physical device or test on an emulator.



Related Topics



Leave a reply



Submit