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"
}
}
}
}
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.
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.
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.
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
}
}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With