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.
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:
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?
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.
Your google play services library is being exported from other dependencies of your project and at the compile time the dex compiler gets confused.
If you're using Gradle then including this in your project's build.gradle should exclude the support library from being exported into your main project.
apply plugin: 'android'
apply plugin: 'crashlytics'
/** Must exclude exported support jars from dependencies, or get dex duplicate class error.
* we're
**/
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
all*.exclude group: 'com.google.android.gms', module: 'play-services'
}
If you're using the andoid studio build system. Then you should go to File -> project structure and disable -> modules. Go through each module the and click on the dependency tab, uncheck the export column for for the support library and google play services library.
Post comments if you need more help.
It could also happen if you have differing versions of the same library imported in your build.gradle vs one in one of your libraries. For example, Google Play Store services requires you have them all refer to the same version I had:
compile 'com.google.android.gms:play-services-base:7.5.0'
whereas:
compile 'com.google.android.gms:play-services-cast:7.8.+'
Upgrading my app's version to 7.8.0 solved the problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With