Android Studio:错误:任务’:app:dexDebug’的执行失败

我第一次使用Android Studio,导入项目后出现以下错误(以前是一个eclipse项目,我也有问题。)

以下是给出的信息:

Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.internal.LoggedErrorException: Failed to run command: /home/crash-id/Development/SDK/adt-bundle-linux-x86_64-20140702/sdk/build-tools/21.1.2/dx --dex --no-optimize --output /home/crash-id/AndroidstudioProjects/LocalSin/app/build/intermediates/dex/debug --input-list=/home/crash-id/AndroidstudioProjects/LocalSin/app/build/intermediates/tmp/dex/debug/inputList.txt 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:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106) :app:dexDebug 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:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106) 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: /home/crash-id/Development/SDK/adt-bundle-linux-x86_64-20140702/sdk/build-tools/21.1.2/dx --dex --no-optimize --output /home/crash-id/AndroidstudioProjects/LocalSin/app/build/intermediates/dex/debug --input-list=/home/crash-id/AndroidstudioProjects/LocalSin/app/build/intermediates/tmp/dex/debug/inputList.txt 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:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106) * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED 

app build.gradle是这样的:

 apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.myapp.test" minSdkVersion 11 targetSdkVersion 14 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } } dependencies { compile 'com.android.support:support-v4:18.0.0' compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.google.android.gms:play-services:+' compile files('libs/google-play-services.jar') compile files('libs/httpcore-4.3.2.jar') compile files('libs/httpmime-4.3.4.jar') } 

而build.gradle项目是这样的:

 buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.0.0' } } allprojects { repositories { jcenter() } } 

你知道问题是什么,怎么解决? 这个问题的一个很好的解释也很有用,因为我是Android Studio的新手。 提前致谢 :)

 compile 'com.google.android.gms:play-services:+' compile files('libs/google-play-services.jar') 

我认为你应该删除其中一行。

cd android / && ./gradlew clean && cd .. && react-native run-android

我有Android Studio错误:任务’:app:dexDebug’的执行失败,我通过将multiDexEnabled设置为true来解决它

 defaultConfig { // Enabling multidex support. multiDexEnabled true } 

在我的项目中使用了太多的库,因此我超过了Android强加的65K方法dex限制以获取更多信息,请参阅: http : //developer.android.com/tools/building/multidex.html

避免包括整个gms库:
compile 'com.google.android.gms:play-services:+'

相反,只需以这种方式使用所需的组件:

 compile 'com.google.android.gms:play-services-ads:8.3.0' compile 'com.google.android.gms:play-services-analytics:8.3.0' compile 'com.google.android.gms:play-services-gcm:8.3.0' 

发生这种情况是因为您在android studio中复制了app build.gradle中的库依赖项。

Android Studio通过使用自动为libs文件夹中的所有文件添加依赖项

 compile fileTree(dir: 'libs', include: ['*.jar']) 

或者在您的特定情况下使用添加依赖项

 compile files('libs/google-play-services.jar') 

^现在,当您尝试使用时再次添加依赖项

 compile 'com.google.android.gms:play-services:+' 

你得到错误。



删除“app build.gradle”中添加的所有重复依赖项,您应该很高兴。在您的情况下删除

 compile 'com.google.android.gms:play-services:+' 

确保在编译的项目中不重复包名称

在@Drivers的回答中 ,指出在给定项目中,不应该存在具有相同命名空间的多个依赖项。 在上述情况中, Google Play服务库已被包含两次,从而导致程序包名称冲突。

在同一个项目中的多个模块(应用程序,库等)也可能发生同样的情况。 因此,请确保对于每个AndroidManifest.xml文件, package名称在所有模块中都是唯一的: –

AndroidManifest.xml中:

  

我在gradle文件上有以下代码和相同的错误。

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.0' compile 'com.google.android.gms:play-services-ads:8.3.0' } dependencies { compile 'com.android.support:design:23.+' } dependencies { compile 'com.android.support:design:23.+' } dependencies { compile 'com.android.support:design:23.+' } 

删除了最后3个依赖项,现在它正在工作!