CountingIdlingResource 的 IllegalAccessError

IllegalAccessError with CountingIdlingResource

该应用程序包含一个短暂显示的初始屏幕,并且 activity 正在使用 IdlingResource 使用插桩测试进行测试,以便测试知道初始屏幕何时关闭。问题是 SplashActivity 在设备 运行 API 19:

测试期间抛出看起来像依赖相关的异常
import android.support.test.espresso.idling.CountingIdlingResource;
...
private CountingIdlingResource espressoTestIdlingResource =
new CountingIdlingResource("Splash_Delay"); // <-- Exception here line 22
...

app/build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.code.findbugs'
        exclude module: 'espresso-idling-resource'
        exclude group: "javax.inject"
    })
    compile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2'

    compile 'com.google.dagger:dagger:2.10'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
    compile 'com.google.dagger:dagger-android:2.10'
    compile 'com.google.dagger:dagger-android-support:2.10'
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.10'

    compile "com.android.support:appcompat-v7:$supportLibraryVersion"
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support.constraint:constraint-layout:1.0.2"

    compile "com.jakewharton.timber:timber:4.5.1"
    compile "com.squareup.phrase:phrase:1.1.0"
    compile "com.squareup.retrofit2:retrofit:2.2.0"
    compile "com.squareup.retrofit2:converter-gson:2.2.0"
    compile "com.squareup.okhttp3:logging-interceptor:3.7.0"
    compile 'net.danlew:android.joda:2.9.9'

    testCompile 'junit:junit:4.12'
    compile 'com.google.firebase:firebase-crash:10.2.4'
    androidTestCompile 'junit:junit:4.12'
}

异常:

java.lang.IllegalAccessError
java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
    at com.myapp.android.ui.splash.SplashActivity.<init>(SplashActivity.java:22)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1208)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    at android.app.ActivityThread.accessX800(ActivityThread.java:135)
    at android.app.ActivityThreadXH.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInitXMethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)

异常发生在 Firebase 测试实验室中的 API 级别 19 Nexus 4 物理设备上。它不会出现在我们正在测试的任何其他平台上,包括本地 API 19 模拟 Nexus S.

我理解异常意味着存在 ambiguous/duplicate(传递)依赖项,但我在 gradlew 依赖项中看不到任何依赖项,只有 Espresso Idling Resources v2.2.2。依赖项是 "compile" 而不是 "androidTestCompile",因为在 Activity.

中引用了 CountingIdlingResource

如何确定原因并解决它?

更新:Nexus 5 和 Nexus 7 上的 API 19 也会发生异常。以下是与空闲资源相关的“./gradlew dependencies”输出的部分图书馆:

_debugApk - ## Internal use, do not manually configure ##
+--- com.android.support.test.espresso:espresso-idling-resource:2.2.2
+--- com.google.dagger:dagger:2.10
...
_debugCompile - ## Internal use, do not manually configure ##
+--- com.android.support.test.espresso:espresso-idling-resource:2.2.2
+--- com.google.dagger:dagger:2.10
...
_releaseApk - ## Internal use, do not manually configure ##
+--- com.android.support.test.espresso:espresso-idling-resource:2.2.2
+--- com.google.dagger:dagger:2.10
...
_releaseCompile - ## Internal use, do not manually configure ##
+--- com.android.support.test.espresso:espresso-idling-resource:2.2.2
+--- com.google.dagger:dagger:2.10
...
compile - Classpath for compiling the main sources.
+--- com.android.support.test.espresso:espresso-idling-resource:2.2.2
+--- com.google.dagger:dagger:2.10

据我了解Google Samples依赖关系:

com.android.support.test.espresso:espresso-idling-resource:2.2.2

仅在您实施自定义 IdlingResource 时才需要。即使在 IdlingResourceSample README 中也有一句话:

Consider using the CountingIdlingResource class from the espresso-contrib package

据我了解您的代码,您正在尝试使用 espresso-contrib 包中的 CountingIdlingResource,因此请尝试按照其他 Google sample.

中所写的方式组织您的测试依赖项