Android Studio: Error:Execution failed for task ':app:dexDebug'

Deadly 提交于 2019-11-27 02:46:59

问题


I'm using Android Studio for the first time and I got the following error after importing the project (previously it was an eclipse project where I had issues too.)

Here is the information given:

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

The app build.gradle is this:

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

And the project built.gradle is this:

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

allprojects {
    repositories {
        jcenter()
    }
}

Do you have any idea what the problem could be and how can it be solved? A good explanation for the problem would be useful too as I am new to Android Studio. Thanks in advance :)


回答1:


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

I think you should remove 1 of those lines.




回答2:


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




回答3:


I had the Android Studio Error:Execution failed for task ':app:dexDebug' that i solve it by setting multiDexEnabled to true

defaultConfig {        
    // Enabling multidex support.
    multiDexEnabled true
}

Too many library were used in my project so i exceed the 65K methods dex limit imposed by Android for more information see : http://developer.android.com/tools/building/multidex.html




回答4:


avoid to include whole gms library:
compile 'com.google.android.gms:play-services:+'

Instead, just use required components in this way:

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'



回答5:


This happens because you are duplicating the library dependency inside of app build.gradle in android studio.

Android Studio automatically adds the dependencies for all the files in the libs folder by using

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

or in your specific case the dependency is added using

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

^ Now when you try to add the dependency again by using

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

you get the error.



SOLUTION

Remove all duplicate dependencies added inside the "app build.gradle" and you should be good to go.In your case remove

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



回答6:


Ensure that the Package names are not duplicated across the projects being compiled

In @Drivers' answer, it is pointed out that in a given project, there should not be multiple dependencies that have the same namespace. In the case above, the Google Play Services library had been included twice, and thus caused a package name conflict.

The same can also happen across multiple modules (apps, libraries etc.) in the same project. So, ensure that for each AndroidManifest.xml file, the package name is unique across all modules:-

AndroidManifest.xml:

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="this.must.be.unique.across.modules">



回答7:


I had the below code on gradle file and same error.

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.+'
}

Removed those last 3 dependencies and now its working!



来源:https://stackoverflow.com/questions/27787747/android-studio-errorexecution-failed-for-task-appdexdebug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!