Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

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

I have a trouble trying use a Google Play Services on my Android App using Android Studio.

I've tried everything and still doesn't work.

This is the error.

Execution failed for task ':app:dexDebug'.

com.android.ide.common.internal.LoggedErrorException: Failed to run command: /Users/jghg/Desktop/My App/Android/SDK/android-sdk-mac_86/build-tools/19.0.1/dx --dex --output /Users/jghg/Desktop/My App/Eureka/UDA/app/build/libs/app-debug.dex /Users/jghg/Desktop/My App/Eureka/UDA/app/build/classes/debug /Users/jghg/Desktop/My App/Eureka/UDA/app/build/dependency-cache/debug /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/classes-08979151dd1373bd3f799299d93376d22d4afa46.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/classes-167b9d3c5d689abe004c3fa5b0bcb945d3f0fc8e.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/google-play-services-ec20f8af7bb457c5095cae1afa0cee722582f198.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/support-v4-13.0.0-473d85b8d55c88bfed3404072e6c132f96543429.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/support-v4-19.0.1-861cc05365a0e9262c764da37d61e3f93dc16de6.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/support-v4-19.0.1-dcc11377c764caea791f711123b8b678f876c3b6.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-async-3.0.5-0904cb320186fb23a9a9bf25a048c5bc4ec07bc2.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-core-3.0.5-41d2d5805e2d90cf77813a126306c4cbe22583ae.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-examples-3.0.5-adc1ee9b037c8061429560e6a5fe89ce8e502db6.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-media-support-3.0.5-37d138cdc631738d13ddb6f4d34c560a9cd8e048.jar /Users/jghg/Desktop/My App/Eureka/UDA/app/build/pre-dexed/debug/twitter4j-stream-3.0.5-c96c138ea216b25631a1a8b47520ecaf33f288d8.jar Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lcom/google/ads/AdRequest$ErrorCode; 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)

Thanks. Best Regards.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here

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?

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...