我有一个应用程序,我有本地单元测试(测试文件夹)和仪表单元测试用例(
androidTest文件夹).现在,如果我点击androidTest文件夹,然后单击“运行所有测试”,它将抛出以下异常.
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
这个例外显然是因为达到了multidex限制.但是我已经为调试版本启用了multi-dex.我想当运行检测测试用例时,它们以调试模式运行.那为什么会发生这种异常呢?
我附加了build.gradle文件
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"
}
}
}
}
解决方法
得到这个工作.我在应用程序模块的build.gradle中放了multiDexEnabled.但我在其他模块中运行单元测试.事实证明我还需要在该模块中添加multiDexEnabled true.
android {
buildTypes {
debug {
multiDexEnabled true
}
}}