Polarbear0106 Polarbear0106 - 3 months ago 163x
Java Question

Gradle build failed: Dex files cannot exceed 64k

Whenever I build by project and prepare for launch on my device, I keep getting this error:

Error:The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

Additionally, I get this error:

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

And it says gradle build failed with two errors

So I have some questions:

  • What are dex files

  • I'm not using them directly, so why do I get the errors above?

  • What are in dex files?

  • Do these files have anything to say for the .APK file size?

These errors started appearing again after I stopped using proguard for debug builds as the StackTrace did not show and right before I activated proguard I had the error.

I had this error once before and I deleted the dex folder and it solved it but now it suddenly isn't enough anymore.

my gradle file:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "com.example.myproject"
minSdkVersion 15
targetSdkVersion 23


buildTypes {
release {
minifyEnabled true
shrinkResources true

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


dependencies {

compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.google.android.gms:play-services:9.2.0'
compile 'com.google.android.gms:play-services-ads:9.2.0'


The more dependencies you add - the higher your method count. Change your gradle to reflect this:

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 23

    // Enabling multidex support.
    multiDexEnabled true

This is a straight forward fix, and only works if you minSdkVerison is 14 or above. Also add the following dependency:

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

Sometimes, the dependencies which you leverage will (individually) reference the same libraries - you may have many methods which are doubled, tripled, quadruple, etc... Check out this SO answer to resolve this and always reduce your APK size. gradle - library duplicates in dependencies