Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - UNEXPECTED TOP-LEVEL EXCEPTION:

I have built a new project in Android Studio using the new project templates provided as part of the tool. All of the code has been generated by Studio I have not made any amendments yet.

I am attempting to run the code but the app fails with the following errors, not sure what the problem is so any help appreciated.

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$Callback;
    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)

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Users\RichardKavanagh\AppData\Local\Android\android-sdk\build-tools\19.0.1\dx.bat --dex --output D:\Android\Projects\MyHealthRecord\app\build\libs\app-debug.dex D:\Android\Projects\MyHealthRecord\app\build\classes\debug D:\Android\Projects\MyHealthRecord\app\build\dependency-cache\debug D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\android-support-v7-appcompat-5a78dab7e2789bbe64f4bc80d106ca75c04dcf6f.jar D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\classes-f9b947272e9f33ba50355b52d82755584f9c0c58.jar D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\support-v4-19.0.0-31a2c13df80d37d62ca50fec3bde6da0ca706223.jar
Error Code:
    2
Output:

    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$Callback;
        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)



* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 12.948 secs
like image 492
BENBUN Coder Avatar asked Jan 13 '14 22:01

BENBUN Coder


3 Answers

Like everyone else said here, the support library (com.android.support) is being included more than once in your project. Try adding this in your build.gradle at the root level and it should exclude the support library from being exported via other project dependencies.

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

If you have more then one support libs included in the dependencies like this, you may want to remove one of them:enter image description here

like image 185
iTapAndroid Avatar answered Oct 17 '22 04:10

iTapAndroid


enter image description here

dependencies {
    compile 'com.android.support:support-v4:19.1.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

This gets conflicted if you have the support jar in your libs folder. If you have the support jar in your project libs folder and you have the module dependency added to compile 'com.android.support:support-v4:13.0.+' the UNEXPECTED_TOPLEVEL_DEPENDANCY exception will be thrown. conflicting folder structure and build.gradle

like image 35
varunarl Avatar answered Oct 17 '22 05:10

varunarl


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')

That's how I fix this problem.

like image 9
buptcoder Avatar answered Oct 17 '22 06:10

buptcoder