[android] Error:Execution failed for task ':app:transformClassesWithDexForDebug'

The error

Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.transform.api.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1

My app gradle file:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    defaultConfig {
        applicationId 'Hidden application ID'
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        disable 'InvalidPackage'
    }
    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
    productFlavors {
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.googlecode.libphonenumber:libphonenumber:7.2.1'
    compile 'com.getbase:floatingactionbutton:1.10.1'
    compile 'com.android.support:preference-v7:23.1.1'
}

While debugging, if I set minifyEnabled to true, then it compiles. However, then I cannot debug my application.

I checked this other question: Execution failed for task ':app:transformClassesWithDexForDebug' while implementing Google sign in for Android, but there is only one answer and implementing it does not resolve the issue unfortunately.

AFAIK, the error is caused due to addition of too many Gradle dependencies, but I may be wrong (I really hope to be wrong because all these packages are really important!).

Please help me to resolve this error. Much thanks!

The answer is


In my case:-

  • What went wrong: Execution failed for task ':app:multiDexListDebug'.

Answer:-

I just deleted the react-native-firebase folder which will be inside the node_modules folder, that's it then I could run the app successfully.

And one more thing don't forget to remove react-native-firebase but not @react-native-firebase in package.json file. So that next time when you run npm I or yarn. This won't get installed in your app.

This package was causing the issue.


Make sure you aren't using two versions of google service.

For example having:

compile 'com.google.firebase:firebase-messaging:9.8.0'

and

compile 'com.google.firebase:firebase-ads:10.0.0'

If java 8 or above is used then the problem is the libraries we use are incompatible with java 8. So to solve this add these two lines to build.gradle of your app and all sub modules if any. (Android studio clearly show how to do this in error message)

targetCompatibility = '1.7' sourceCompatibility = '1.7'


Try

dexOptions {
    javaMaxHeapSize "4g"
    preDexLibraries = false
}

I don't know the reason. Something about preDexLibraries : https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/tips

According to @lgdroid57 :

The following resource should help explain what this code does: link(http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.DexOptions.html) Property | Description javaMaxHeapSize | Sets the -JXmx* value when calling dx. Format should follow the 1024M pattern. preDexLibraries | Whether to pre-dex libraries. This can improve incremental builds, but clean builds may be slower.


By changing

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

to only needed files, the problem can be solved This Issue is generated because of exceeding files in th build.gradle file.


Dont go crazy I just clean , then rebuild project and error was gone


add multiDexEnabled true in default config file of build.gradle like this

    defaultConfig {
        multiDexEnabled true
       }

Using ionic, i was able to fix this error using the command: "cordova clean"


I got this problem when I updated the Gradle plugin from version 1.2.3 to 1.5.0 as Android Studio suggested. In its web page, 1.5.0 appears to be a beta version.

Honestly, I don't know what advantages the version 1.5.0 has, but I'd rather wait until there's a stable version.

Of course, going back to 1.2.3 solved the issue.


Possible help

Tried as absolute last resort after exhausting all other options

I removed all memory sticks (RAM) but one. I have less memory now but its working again. I can't pretend to know why the build process always found corrupted memory, or if that's 100% the root cause - in other respects the system ran fine. Maybe this is something to try, best of luck.


Go into Build -> Clean and run your app again


  1. Execute: cordova plugin rm cordova-plugin-compat --force
  2. Execute: cordova plugin add [email protected]
  3. Change : config.xml in your project folder use "6.2.3" not "^6.2.3", then delete the platforms/android folder, re-run cordova prepare android, and cordova build android

This is the problem about Multidex . You can try to remove some jar, or you can try like this : http://developer.android.com/tools/building/multidex.html#mdex-gradle in app build.gradle :

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

In your manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

just remove both build folder in /android and /android/app

and build again with react-native run-android


Apparently I resolved the issue by combining all the solutions adding following to android manifest:

<application
...
        android:name="android.support.multidex.MultiDexApplication" >
...
</application>

Following to build.gradle app module

android {
...

    dexOptions {
        javaMaxHeapSize "4g"
        preDexLibraries = false
    }
...
    defaultConfig {

        multiDexEnabled true

   }

}

Go into Build ->

Clean Project ->

Run project : done

working on android 5.1 for me


My response is a little bit old but for my the only solution was adding multiDexEnabled option in defaultConfig like this:

    android{
        ...
        defaultConfig{
            ...
            multiDexEnabled true
        }
    }

If this does not work for you try adding this piece of code:

    configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }
}

My error was related to a problem with different libraries versions and this made the trick.

I hope this can help somebody :)


I had this problem when I delegated my compilation task to the Google Compute Engine via SSH. The nature of this issue is a memory error, as indicated by the crash log; specifically it is thrown when Java runs out of virtual memory to work with during the build.

Important: When gradle crashes due to this memory error, the gradle daemons remain running long after your compilation task has failed. Any re-attempt to build using gradle again will allocate a new gradle daemon. You must ensure that you properly dispose of any crashed instances using gradlew --stop.

The hs_error_pid crash logs indicates the following workarounds:

# There is insufficient memory for the Java Runtime Environment to continue.
# Possible reasons:
#   The system is out of physical RAM or swap space
#   In 32 bit mode, the process size limit was hit
# Possible solutions:
#   Reduce memory load on the system
#   Increase physical memory or swap space
#   Check if swap backing store is full
#   Use 64 bit Java on a 64 bit OS
#   Decrease Java heap size (-Xmx/-Xms)
#   Decrease number of Java threads
#   Decrease Java thread stack sizes (-Xss)
#   Set larger code cache with -XX:ReservedCodeCacheSize=

I found that after increasing the runtime resources of the virtual machine, this issue was resolved.


To all who have faced this issue/ will face it in the future:

Click on Build menu -> Select Build Variant -> restore to 'debug'

Outcomment on debuggable in module:app /* debug { debuggable true }*/

Go to Build menu -> generate signed apk -> .... -> build it


Try adding multiDexEnabled true to your app build.gradle file.

  defaultConfig {
    multiDexEnabled true
}

For me closing all other Android Studio solved the problem.

I had opened 3 android studios when I was getting the error, after I closed 2 I didn't get any error.

No need to add any code related to multiDex !


Move the apply plugin declaration at the bottom of the file:

apply plugin: 'com.google.gms.google-services'


I had a similar error. I fixed it by just switching the directory. Cloning my project again from my git repository, importing it into android studio and building it. Seems like one of the directories/files generated by Android Studio when building your project may have gotten corrupted. Deleting all Android Studio generated files in your project or doing what I did could solve the problem. Hope this helps.


I solved it! It's a collection of configuration and update. Add these variables where they fit in build.gradle

android {
packagingOptions {
    exclude 'META-INF/ASL2.0'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
}
dexOptions {
    javaMaxHeapSize "4g"
}
defaultConfig {
    multiDexEnabled true
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
}

Then update to Java 8 http://tecadmin.net/install-oracle-java-8-jdk-8-ubuntu-via-ppa/ and all will be solved! It's because of the buildTools update and new Android studio. Nothing else will fail.


The solution that worked for me personally was:

in the build.gradle

defaultConfig {
        multiDexEnabled true
    }

 dexOptions {
        javaMaxHeapSize "4g"
    }

I had this error when running an Ionic project on an Android device and I solved it by removing and adding android platform:

ionic cordova platform remove android
ionic cordova platform add android

No need for multitex. For "old" project opened in Android Studio 2.1 it was changing gradle plugin version from 1.5.0 to 2.1.0 that fixed problem for me.

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
    }
}

Just correct Google play services dependencies:

You are including all play services in your project. Only add those you want.

For example , if you are using only maps and g+ signin, than change

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

to

compile 'com.google.android.gms:play-services-maps:8.1.0'
compile 'com.google.android.gms:play-services-plus:8.1.0'

From the doc :

In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.

From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:
compile 'com.google.android.gms:play-services:8.3.0'
with these lines:

compile 'com.google.android.gms:play-services-fitness:8.3.0'
compile 'com.google.android.gms:play-services-wearable:8.3.0'

Whole list can be found here.


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-studio

A failure occurred while executing com.android.build.gradle.internal.tasks "Failed to install the following Android SDK packages as some licences have not been accepted" error Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util This version of Android Studio cannot open this project, please retry with Android Studio 3.4 or newer WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Flutter plugin not installed error;. When running flutter doctor ADB.exe is obsolete and has serious performance problems Android design support library for API 28 (P) not working Flutter command not found How to find the path of Flutter SDK

Examples related to android-gradle-plugin

WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Android Material and appcompat Manifest merger failed Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve Failed to resolve: com.google.firebase:firebase-core:16.0.1 com.google.android.gms:play-services-measurement-base is being requested by various other libraries Invoke-customs are only supported starting with android 0 --min-api 26 error: resource android:attr/fontVariationSettings not found Exception : AAPT2 error: check logs for details Could not resolve com.android.support:appcompat-v7:26.1.0 in Android Studio new project Failed to resolve: com.android.support:appcompat-v7:27.+ (Dependency Error)

Examples related to android-multidex

Error:Cannot fit requested classes in a single dex file.Try supplying a main-dex list. # methods: 72477 > 65536 The number of method references in a .dex file cannot exceed 64k API 17 Error:Execution failed for task ':app:transformClassesWithDexForDebug' java.util.zip.ZipException: duplicate entry during packageAllDebugClassesForMultiDex How to enable multidexing with the new Android Multidex support library