java - 在 Dalvik 运行时设备上运行 Espresso 仪器测试的问题

标签 java android gradle android-gradle-plugin android-espresso

我遇到了与 FragmentActivity can not be tested via ActivityInstrumentationTestCase2 类似的问题

顶级解决方案的问题仅适用于使用 Eclipse 构建系统。我正在为我的项目使用 Gradle。

在运行 ART 运行时的 Nexus 5 上,我的 Espresso 仪器测试运行完美。当我在 Dalvik 运行时或模拟器上使用 Nexus 4 时,我遇到了在测试框架之外运行应用程序时不会发生的异常。

java.lang.NoClassDefFoundError: com.packagename.fragment.ProgressDialogFragment

在日志中,我发现这些可疑消息类似于链接的问题:

Class resolved by unexpected DEX: Landroid/support/v4/app/DialogFragment2;(0x41e969d8):0x76064000 ref [Landroid/support/v4/app/DialogFragment;] Landroid/support/v4/app/DialogFragment;(0x41e969d8):0x75883000
(Landroid/support/v4/app/DialogFragment2; had used a different Landroid/support/v4/app/DialogFragment; during pre-verification)
Unable to resolve superclass of Landroid/support/v4/app/DialogFragment2; (271)
Link of class 'Landroid/support/v4/app/DialogFragment2;' failed
Unable to resolve superclass of Lcom/packagename/fragment/ProgressDialogFragment; (270)
Link of class 'Lcom/packagename/fragment/ProgressDialogFragment;' failed
Could not find method com.packagename.fragment.ProgressDialogFragment.newInstance, referenced from method com.packagename.activity.IntroActivity.doJoin
VFY: unable to resolve static method 47365: Lcom/packagename/fragment/ProgressDialogFragment;.newInstance (I)Landroid/support/v4/app/DialogFragment2;
VFY: replacing opcode 0x71 at 0x0063

ProgressDialogFragmentDialogFragment2 的子类,DialogFragment2 是 Android 支持库的 DialogFragment 类的子类

如果有帮助,这里是我的 Gradle 文件中的依赖项:

    compile 'com.android.support:support-v4:+'
    compile 'com.android.support:appcompat-v7:+'

    compile 'com.google.android.gms:play-services:+'

    compile 'com.google.code.findbugs:jsr305:+'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.3.+'
    compile 'de.greenrobot:greendao:1.3.+'

    compile 'fr.avianey:facebook-android-api:+@aar'

    compile 'com.squareup.mimecraft:mimecraft:1.1.+'
    compile 'com.squareup.picasso:picasso:2.2.+'
    compile 'com.squareup.okhttp:okhttp:1.5.+'

    compile 'eu.inmite.android.lib:android-styled-dialogs:1.1.+@aar'

    compile 'com.newrelic.agent.android:android-agent:3.+'

    compile 'uk.co.chrisjenx:calligraphy:0.7.+'

    compile 'com.github.chrisbanes.actionbarpulltorefresh:library:0.9.+'
    compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:0.9.+'

    apt "com.jakewharton:butterknife:${project.ext.butterKnifeVersion}"
    compile "com.jakewharton:butterknife:${project.ext.butterKnifeVersion}"

    compile fileTree(dir: 'libs', include: '*.jar')

    // Excluded modules were determined from here: https://github.com/robolectric/deckard-gradle
    instrumentTestCompile fileTree(dir: 'libs-test', include: '*.jar')
    instrumentTestCompile 'com.google.guava:guava:14.0.1',
            'com.squareup.dagger:dagger:1.1.0',
            'org.hamcrest:hamcrest-integration:1.1',
            'org.hamcrest:hamcrest-core:1.1',
            'org.hamcrest:hamcrest-library:1.1'

    instrumentTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    instrumentTestCompile('org.robolectric:robolectric:2.3-SNAPSHOT') {
        exclude module: 'classworlds'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-plugin-registry'
        exclude module: 'maven-profile'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'nekohtml'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-http-shared'
        exclude module: 'wagon-provider-api'
    }
    instrumentTestCompile 'org.mockito:mockito-core:1.9.+'

所以我的问题是如何确保在应用程序 APK 和测试 APK 之间使用相同的支持库?我已经尝试添加 instrumentTestCompile 'com.android.support:support-v4:+' 以确保相同的支持库版本,但这没有帮助。

最佳答案

经历了无数的痛苦、汗水和泪水。我设法确定了解决方案。只需将 exclude group: 'com.android.support', module: 'support-v4' 添加到 Robolectric 的排除列表中,Robolectric 就可以继续工作,同时不会在 Dalvik 运行时崩溃。

关于java - 在 Dalvik 运行时设备上运行 Espresso 仪器测试的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22996376/

相关文章:

java - 如何在JAVA中编写正则表达式来提取评论

android - 图库无法在 Android 7.0 上打开 content uri 图像文件

android - 什么是适用于 iOS/Android 的开源 ffmpeg 播放器?

java - 如何在 Gradle 中为多个项目配置多个 gradle.properties 文件?

gradle - 使用 Gradle 将 rpm 工件发布到 yum 快照存储库时遇到问题

java - Wildfly 8.0.2 内的反射库

java - 在 MySQL 表中查找列时出错

java - 如何创建容错JobStep

android - 使用 Android 应用程序将视频从 SD 卡分享到 Facebook

android - gradlew 应用程序 :dependencies command is not working