运行单元测试时遇到Multidex限制问题

8

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


  }
}

}

你有找到任何解决方案吗? - Sufian
目前还没有找到解决方案。 - thedarkpassenger
我在androidTestCompile中有一些不必要的导入,将它们移除后问题得以解决。 - Sufian
1个回答

3

我已经让它工作了。我在app模块的build.gradle中添加了multiDexEnabled true。但是我在另一个模块中运行单元测试。结果发现我还需要在那个模块中添加multiDexEnabled true

android {
  buildTypes {
    debug {
      multiDexEnabled true
    }
  }}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接