74

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!

Community
  • 1
  • 1
Saket Jain
  • 1,352
  • 2
  • 12
  • 25
  • 1
    Can your post the error's full stack trace? To do this, run `./gradlew clean build --full-stacktrace` – Alex Lipov Nov 20 '15 at 12:15
  • 2
    @Alex i tried ur command but it failed at the same point and it gives me same error org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesWithDexForDebug'. – Antwan Dec 05 '15 at 12:05
  • 1
    Please go through my answer as well as the accepted answer on this [Stackoverflow Link](http://stackoverflow.com/a/37202393/2094075) – user2094075 May 13 '16 at 06:16

29 Answers29

50

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.

Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
25

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.

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • Can you please explain what this does? – Saket Jain Nov 15 '15 at 08:54
  • I don't know.I just see that in multiDexEnabled .Or you can clean you project and delete build folder. – tiny sunlight Nov 15 '15 at 08:59
  • Ok, can you please provide your source? – Saket Jain Nov 15 '15 at 09:00
  • 1
    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. – lgdroid57 Dec 02 '15 at 21:23
  • finally this answer was the only which worked for me ! thnx – Amr Mohammed Dec 13 '15 at 01:08
  • @tinysunlight Thanks, Its help me – pRaNaY Dec 18 '15 at 05:42
  • I still don't understand what it exactly did, but it worked for me. I added a second activity and changed it to be the main activity; after that I could get the project to build, but whenever I wanted to run it (on an emulator), it would rebuild and fail with that error. Adding this to the app's build.gradle helped. I am using Android Studio 2.0 for incremental builds. – Igor Jan 31 '16 at 17:22
  • For me this is the only solution which worked...Thanks a lot. – shripal Mar 08 '16 at 07:01
  • 2g is probably enough. – Nick Jun 25 '16 at 22:05
20

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

  defaultConfig {
    multiDexEnabled true
}
Mohamed Mesalm
  • 704
  • 8
  • 17
15

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>
Courysky
  • 331
  • 4
  • 4
14

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

    defaultConfig {
        multiDexEnabled true
       }
Senjuti Mahapatra
  • 2,570
  • 4
  • 27
  • 38
Mohamed Ibrahim
  • 149
  • 1
  • 3
8

The solution that worked for me personally was:

in the build.gradle

defaultConfig {
        multiDexEnabled true
    }

 dexOptions {
        javaMaxHeapSize "4g"
    }
Zach L
  • 712
  • 4
  • 18
Camilo Soto
  • 81
  • 1
  • 2
6

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

Daniel S.
  • 2,629
  • 2
  • 21
  • 19
2

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'
Izak
  • 909
  • 9
  • 24
  • In my case I had to update the versions of other `com.google` libs to the latest version to fix the issue. – Manuel Dec 15 '18 at 01:47
1

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.

  • I am facing the problem with Gradle 1.2.2, so not really sure if its gradle dependent? – Saket Jain Dec 17 '15 at 07:43
  • @SaketJain Have you tried any of the other versions? Are you running 64-bit Java in a 64-bit machine? There are many variables that might affect. – Miguel Palacio Dec 17 '15 at 15:27
  • btw, I tried the workarounds proposed in the other answers (changing the heap size and reducing dependences as much as possible) but none of them worked for me. – Miguel Palacio Dec 17 '15 at 15:27
  • I tried the other answers, but no they did not work for me. That's why I haven't accepted any answers. – Saket Jain Jan 18 '16 at 12:24
1

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.

Saket Jain
  • 1,352
  • 2
  • 12
  • 25
maturecheese
  • 92
  • 1
  • 9
1

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 !

Sanyam Jain
  • 2,925
  • 2
  • 23
  • 30
1

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'
    }
}
Mateusz Jablonski
  • 1,039
  • 9
  • 11
1

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.

Neelay Srivastava
  • 1,041
  • 3
  • 15
  • 46
Pian0_M4n
  • 2,505
  • 31
  • 35
1

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.

1

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

   }

}
cammando
  • 596
  • 7
  • 20
1

Go into Build -> Clean and run your app again

Dodi
  • 2,201
  • 4
  • 30
  • 39
1

Go into Build ->

Clean Project ->

Run project : done

working on android 5.1 for me

DILSHAD AHMAD
  • 215
  • 2
  • 4
1

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

and build again with react-native run-android

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
  • can you check [this](https://stackoverflow.com/questions/61510246/execution-failed-for-task-apptransformdexarchivewithdexmergerforrelease) question, please? – Oliver D Apr 29 '20 at 19:40
1

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

Fabien Thetis
  • 1,666
  • 15
  • 11
0

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.

Community
  • 1
  • 1
Mapsy
  • 4,192
  • 1
  • 37
  • 43
0

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.

user3760100
  • 679
  • 1
  • 9
  • 20
0

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

Firsake
  • 71
  • 5
0

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

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

Rene Enriquez
  • 1,418
  • 1
  • 13
  • 33
0
  1. Execute: cordova plugin rm cordova-plugin-compat --force
  2. Execute: cordova plugin add cordova-plugin-compat@1.2
  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
0

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

Hanako
  • 1,637
  • 1
  • 13
  • 16
0

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
Roman
  • 1
0

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.

Vivek S
  • 1
  • 1
-1

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'

-1

Just delete .gradle folder from android platform, then re-build the project, the Android Studio will generate it again.

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58