在运行单元测试用例时,Multidex限制命中

时间:2016-05-03 10:08:14

标签: android unit-testing

我有一个应用程序,我有本地单元测试(测试文件夹)和仪表单元测试用例( 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"
  }


  }
}

}

1 个答案:

答案 0 :(得分:0)

让这个工作。我已将multiDexEnabled true放入app模块build.gradle。但我在其他模块中运行单元测试。事实证明我还需要在该模块中添加multiDexEnabled true

android {
  buildTypes {
    debug {
      multiDexEnabled true
    }
  }}