46

I am getting the exception below when I am trying to run the application using Android Studio:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_05\bin\java.exe'' finished with non-zero exit value 2

Pang
  • 9,564
  • 146
  • 81
  • 122
Shobharam Piplode
  • 461
  • 1
  • 4
  • 3

10 Answers10

57

I have same issue, after few hour research, i found a solution to fix it.

You should fixed build.gradle :

android {

    compileSdkVersion ..
    buildToolsVersion '...'

    defaultConfig {
       ...
       targetSdkVersion ..
       multiDexEnabled true  // this line will solve this problem
   }
}

If the number of method references in your app exceeds the 65K limit, your app may fail to compile.

For information on how to do this, see Selectively compiling APIs into your executable and Building Apps with Over 65K Methods

Danh DC
  • 1,246
  • 7
  • 17
  • How did you come about this solution @Danh DC? Worked for me but would want to know how you got this. – Marka A Jan 22 '16 at 00:36
  • 1
    @MarkaA If the number of method references in your app exceeds the 65K limit, your app may fail to compile. You may be able to mitigate this problem when compiling your app by specifying only the specific Google Play services APIs your app uses, instead of all of them. For information on how to do this, see https://developers.google.com/android/guides/setup#split and http://developer.android.com/tools/building/multidex.html – Danh DC Jan 22 '16 at 01:36
  • 1
    To be clear, the thing to put in `build.gradle` is `multiDexEnabled true`. Also somewhat silly that Android Studio doesn't do this automatically, particularly when dependencies/libraries appear to count towards the 65K limit. My app certainly has nowhere near 65K method references in its first-party code. – aroth Jul 26 '16 at 02:04
14

in my case using android studio 2.0 preview 4 I suddenly got that problem and adding multiDexEnabled true didn't help, and also clean and rebuilt didn't help.

so the only thing that solved it for me is deleting that file:

YOUR_APP_NAME\app\build\intermediates

and run the application and it works.

humazed
  • 74,687
  • 32
  • 99
  • 138
  • 1
    After I did this, I got a few more build errors about difficulty creating some directories in the intermediates, but manually adding to the project I was able to build. Thanks @humazed! – safetyOtter Aug 19 '16 at 16:21
  • 1
    Thank you. This is the only way to fix it in my case (AS 3.0.1) on Windows 10 Pro. I tried other methods (restart AS, clear project, rebuild project, Invalidate cache/restart...) to no avail. – Hong Dec 18 '17 at 16:23
5

Modify the module-level build.gradle file

 android {
    ...

    // Enabling multidex support.
    multiDexEnabled true
}
...

}

add dependencies compile 'com.android.support:multidex:1.0.0'

In your manifest add the MultiDexApplication class

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

http://developer.android.com/studio/build/multidex.html

ankush
  • 539
  • 1
  • 5
  • 8
4

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 !

Seems like there was some memory issue related to jvm.

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

Just fixed this issue. In my case, rebuilding the project helped for me. So, try to rebuild your project.

Dr. Failov
  • 311
  • 4
  • 7
0

Add the following line:

multiDexEnabled true

Inside the defaultConfig of build.gradle

Like this:

defaultConfig{
    multiDexEnabled true
}
Draken
  • 3,134
  • 13
  • 34
  • 54
Mohamed Ibrahim
  • 149
  • 1
  • 3
0

At my case change buildToolsVersion from "24" to "23.0.2", solve the problem. This will solve the problem especially if you're using old Android Studio less than version 2.

Ayman Mahgoub
  • 4,152
  • 1
  • 30
  • 27
0

I found the answer from here: After change the build.grade file with the following

minSdkVersion 21

targetSdkVersion 25

multiDexEnabled true

works fine.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

in my case - Clean Project and Rebuild

NataTse
  • 381
  • 2
  • 10
0

In my case I was going back and forth between Expo and Android Studio for react native. Expo wanted one application name in order to build and Android Studio wanted another, once I put it back to the name that android studio wanted it built and deployed fine.

Expo

AppRegistry.registerComponent('main', () => App);

Android Studio

AppRegistry.registerComponent('AwesomeProject', () => App);

Brandon Culley
  • 5,219
  • 1
  • 28
  • 28