java - Android:WireMock:com.android.dex.DexIndexOverflowException:方法ID不在[0,0xffff]中:65536

标签 java android android-studio android-multidex wiremock

Gradle 3.3,WireMock 2.6.0。 我通过 Gradle 构建 Android 项目。我用 Espresso、Mockito 编写单元测试,一切正常。

这是我的 build.gradle:

dependencies {
    // for folder "androidTest"
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile "org.mockito:mockito-android:2.7.21"
    androidTestCompile "org.powermock:powermock-api-mockito:1.6.6"
    androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6'

    // for folder "test"
    testCompile 'junit:junit:4.12'
    testCompile 'org.powermock:powermock-api-mockito:1.6.6'
    testCompile 'org.powermock:powermock-module-junit4:1.6.6'
    }

我的项目是成功构建和单元测试成功运行。好的。 现在我想添加 WireMock 单元测试。所以我更改了我的 build.gradle (如本博客所示: http://handstandsam.com/2016/01/30/running-wiremock-on-android/ ):

    dependencies {
// for folder "androidTest"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile "org.mockito:mockito-android:2.7.21"
androidTestCompile "org.powermock:powermock-api-mockito:1.6.6"
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6'

//WireMock
androidTestCompile("com.github.tomakehurst:wiremock:2.6.0") {
    //Using Android Version Instead
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'

    //Version conflict with our app's slf4j version
    exclude group: 'org.slf4j', module: 'slf4j-api'

    //Was getting a classpath conflict for org.objectweb.asm.AnnotationVisitor which is a part of 'net.minidev:asm'
    exclude group: 'org.ow2.asm', module: 'asm'

    //Was getting this warning, so decided to ignore this version included by WireMock.
    //Warning:Dependency org.json:json:20090211 is ignored as it may be conflicting with the internal version provided by Android.
    //In case of problem, please repackage with jarjar to change the class packages
    exclude group: 'org.json', module: 'json'
}

// for folder "test"
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
}

当我运行我的应用程序时,一切正常。但是当我尝试启动 Android 的仪器化单元测试时,我收到下一个错误:

    :app:compileDevAndroidTestShaders
:app:generateDevAndroidTestAssets
:app:mergeDevAndroidTestAssets
:app:transformClassesWithDexForDevAndroidTest FAILED

FAILURE: Build failed with an exception.

What went wrong:
Execution failed for task ':app:transformClassesWithDexForDevAndroidTest'.
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED

最佳答案

当访问索引大于 64K(Dex 方法计数限制)的方法时,会发生 DexIndexOverFlowException。这意味着您的android项目中的实际方法计数(引用)数量大于64K,并且您的应用程序的最终.dex文件将被拆分为多个dex文件。在这种情况下,您应该通过以下方式让您的应用程序支持 multidex:

1- 添加 multidex 依赖项:

    compile 'com.android.support:multidex:1.0.1'

2- 将以下配置添加到 app/build.gradle :

    android {

      defaultConfig {

          multidexEnabled true
       }
    }

3- 创建自定义应用程序类来扩展 MultiDexApplication

public MyApplication extends MultiDexApplication

4- 在 AndroidManifest.xml 中将此自定义类作为应用程序的入口点:

    <application
    android:name=".MyApplication"

关于java - Android:WireMock:com.android.dex.DexIndexOverflowException:方法ID不在[0,0xffff]中:65536,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43652595/

相关文章:

android - 谁能提供一个将生成 ForegroundServiceStartNotAllowedException 的示例?

Android:将空 View 设置为 ListView

android - Features2D + Homography OpenCV 链接错误

java - 如何将每个句子的第一个字母转换为大写并将所有其他字母转换为小写?

java - 如何在运行时从文件夹加载 jar 文件并在 JBoss EAP 6.0.1 中实例化类

java - GridBagLayout 中的组件对齐

java - 外部任务执行完成 'signingReport'

java - 从 C# 客户端上的流中读取 Java servlet 字节

java - 矩形不更新也不显示颜色

由于内部错误 : GPU Found,Android 模拟器关闭