Android Studio: Gradle - build fails -- Execution failed for task ':dexDebug'

Error:

Gradle: Execution failed for task ':vertretungsplan:dexDebug'.
> Failed to run command:
    P:\Android-Studio\sdk\build-tools\18.0.1\dx.bat --dex --output P:\Projekte\VertretungsplanProject\vertretungsplan\build\libs\vertretungsplan-debug.dex P:\Projekte\VertretungsplanProject\vertretungsplan\build\classes\debug P:\Projekte\VertretungsplanProject\vertretungsplan\build\dependency-cache\debug P:\Android-Studio\sdk\extras\android\m2repository\com\android\support\support-v4\18.0.0\support-v4-18.0.0.jar P:\Projekte\VertretungsplanProject\vertretungsplan\libs\commons-io-2.4.jar P:\Projekte\VertretungsplanProject\vertretungsplan\build\exploded-bundles\VertretungsplanProjectLibrariesActionbarsherlockUnspecified.aar\classes.jar
Error Code:
    2
Output:
    trouble processing:
    bad class file magic (cafebabe) or version (0033.0000)
    ...while parsing de/MayerhoferSimon/Vertretungsplan/LoginActivity$2.class
    ...while processing de/MayerhoferSimon/Vertretungsplan/LoginActivity$2.class
    trouble processing:
    bad class file magic (cafebabe) or version (0033.0000)
    ...while parsing de/MayerhoferSimon/Vertretungsplan/MainActivity$1.class
    ...while processing de/MayerhoferSimon/Vertretungsplan/MainActivity$1.class
    trouble processing:
    bad class file magic (cafebabe) or version (0033.0000)
    ...while parsing de/MayerhoferSimon/Vertretungsplan/YQL/YqlVplanParser.class
    ...while processing de/MayerhoferSimon/Vertretungsplan/YQL/YqlVplanParser.class
    3 warnings
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dx.util.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:592)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:550)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:531)
        at com.android.dx.merge.DexMerger.mergeDexBuffers(DexMerger.java:168)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:186)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:300)
        at com.android.dx.command.dexer.Main.run(Main.java:232)
        at com.android.dx.command.dexer.Main.main(Main.java:174)
        at com.android.dx.command.Main.main(Main.java:91)

Project structure:

enter image description here

build.gradle (actionbarsherlock)

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 11
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

build.gradle (vertretungsplan)

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/commons-io-2.4.jar')
    compile project(':libraries:actionbarsherlock')
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 11
    }
}

settings.gradle

include ':vertretungsplan', ':libraries:actionbarsherlock'

How can I fix this error?


Solution 1:

The right answer is, that some of your jar files does not compile. You should go into your build.gradle file in your project, and look in your dependencies.

If you're just importing some jar files, you could try to remove them and add them one at a time. This will help you determine which one of them causes the error.

In my case, I did just that, and when I was importing the last one, the app compiled. So I think the real problem was that I was importing too many at once. But now it all works.

Solution 2:

I suddenly had the same problem, after no noteworthy changes.

I solved it by deleting the app/build directory and let gradle build the whole project new.

Solution 3:

You must check if the same JAR is being imported again. In my case there was a class inside a jar which was getting imported in another jar. So just check if the any lib / class file is being included twice in the whole project!

Solution 4:

I got the same sort of error when I tried to compile a utils library jar in eclipse using Java JRE 1.8, and use it in my /libs/ in Android Studio 1.1.0.

I had my Android Studio set to use JDK1.8.0.

I switched my Eclipse to work with JRE 1.7, and the error was fixed. Eclipse: Window->Preferences->Java tab->Compiler -> Compliance level 1.7. It will most likely prompt you to switch your JRE System Library to jdk1.7.x_x.

You may need to make sure to uncheck 'compress jar' when you export. I haven't tested whether it had an effect or not. I doubt it was related.

Solution 5:

I had the same problem too. In my case the problem started after a reboot. I closed my App, then I closed the Android Studio (In my case V1.1.0), and finally a normal shutdown. After that, I modified one java file to add a RadioGroup object and then the problem appeared.

I solved my problem only changing a simple '0' for a '1' in my Gradle configuration file, because the root cause of the problem was generated at the Gradle execution process. Previously I used to have version '1.0.0' then i changed it to '1.1.0', as stated in the pictures.

Location of the Gradle configuration a changed Location of the Gradle configuration a changed

Location where I took the right version from (File -> Settings -> Gradle -> Experimental Location where I took the right version from (File -> Settings -> Gradle -> Experimental