14

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

dephinera
  • 3,703
  • 11
  • 41
  • 75
  • possible duplicate of [Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat](http://stackoverflow.com/questions/20989317/multiple-dex-files-define-landroid-support-v4-accessibilityservice-accessibility) – Divers Jan 05 '15 at 21:03

8 Answers8

13

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

diogopalhais
  • 369
  • 1
  • 4
  • 14
  • Please add an explaination for your intention or put this text as a comment. – Reporter Sep 26 '16 at 14:19
  • I'm up-voting because it works, but I agree a little explanation would be helpful. Thanks. – Logic Artist Nov 28 '16 at 15:34
  • 1
    This worked for me. If you're using React Native and you're running into this or `Execution failed for task ':app:dexProductionDebug'` then this will most likely solve your problem. – DeanMWake Apr 11 '17 at 08:47
12
compile 'com.google.android.gms:play-services:+'
compile files('libs/google-play-services.jar')

I think you should remove 1 of those lines.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • This fixed this problem, thank you, but now I have this: Duplicate files copied in APK META-INF/LICENSE File 1: /home/crash-id/AndroidstudioProjects/LocalSin/app/libs/httpcore-4.3.2.jar File 2: /home/crash-id/AndroidstudioProjects/LocalSin/app/libs/httpcore-4.3.2.jar – dephinera Jan 05 '15 at 21:20
  • 1
    Never mind. Fixed it by adding exclude 'META-INF/LICENSE' to packagingOptions {..} – dephinera Jan 05 '15 at 21:29
4

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

Netero
  • 3,761
  • 2
  • 29
  • 29
2

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:+'
meain
  • 833
  • 9
  • 28
2

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'
bskim45
  • 71
  • 1
  • 5
1

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">
Community
  • 1
  • 1
CJBS
  • 15,147
  • 6
  • 86
  • 135
0

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!

Shree Harsha S
  • 665
  • 7
  • 14
-2

delete all files in c:\users\<username>\.gradle\caches\transforms-2\files-2.1\

Enjoy the command flutter run

digiwizkid
  • 791
  • 2
  • 14
  • 28