Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidex limit hitting while running unit test cases

I have an application where I have local unit tests ( test folder ) and instrumentation unit test cases ( androidTest folder ). Right now if I click on the androidTest folder, and click "Run All Tests", it throws the following exception.

Error:Error converting bytecode to dex:
Cause: com.android.dex.DexIndexOverflowException: field ID not in [0, 0xffff]: 65536

Error:Execution failed for task ':news-app:transformClassesWithDexForDebugAndroidTest'.
> 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.7.0_79\bin\java.exe'' finished with non-zero exit value 2

This exception is clearly because of hitting the multidex limit. But I have enabled multi-dex for debug build. I guess when the instrumentation test cases are run, they are run in debug mode. Then why is this exception occuring?

I am attaching the build.gradle file

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'


android {
  compileSdkVersion 22
  buildToolsVersion 22.0.1

  defaultConfig {
    minSdkVersion 14
    targetSdkVersion 22
    applicationId "com.xyz"
  }


  buildTypes {

    debug {
      minifyEnabled false
      shrinkResources false
      multiDexEnabled true
    }

    release {
      minifyEnabled true
      shrinkResources true
      multiDexEnabled false
    }
  }

  lintOptions {
    warning 'InvalidPackage', 'GradleCompatible'
  }

  dexOptions {
    preDexLibraries true
    incremental true
    jumboMode = true
    javaMaxHeapSize "4g"
  }


  }
}

}
like image 824
thedarkpassenger Avatar asked May 03 '16 10:05

thedarkpassenger


People also ask

What is the maximum number of methods supported by Dex compiler before needing multidex?

The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536—including Android framework methods, library methods, and methods in your own code.

Is multidex needed?

Multidex support library is enabled by default in API level higher than 21 (Android 5 and higher). Therefore, you do not need to add the Multidex support library.

What is multidex enabled?

Android applications by default have SingleDex support which limits your application to have only 65536 methods(references). So multidexEnabled = true simply allows you to build apps with over 64k methods.


1 Answers

Got this to be working. I had put multiDexEnabled true in the app module's build.gradle. But I was running the unit tests in some other module. Turns out that I needed to add multiDexEnabled true in that module also.

android {
  buildTypes {
    debug {
      multiDexEnabled true
    }
  }}
like image 129
thedarkpassenger Avatar answered Nov 03 '22 13:11

thedarkpassenger