Espresso 2升级后测试失败(失败:由于“java.lang.IllegalAccessError'”导致仪器运行失败)

时间:2015-02-16 23:13:15

标签: android gradle android-4.4-kitkat android-espresso

将Espresso升级到版本2后,我的单元测试都不会在棒棒糖前设备上运行。请参见下面的曲目:

15:07:07.627 [WARN] [org.gradle.api.Project] Tests on Samsung Galaxy S3 - 4.3 - API 18 - 720x1280 - 4.3 failed: Instrumentation run failed due to 'java.lang.IllegalAccessError'
15:07:07.832 [WARN] [org.gradle.api.Project] 
com.android.builder.testing.ConnectedDevice > hasTests[Samsung Galaxy S3 - 4.3 - API 18 - 720x1280 - 4.3] FAILED 
15:07:07.832 [WARN] [org.gradle.api.Project] No tests found.
15:07:07.834 [DEBUG] [org.gradle.api.Project] DeviceConnector 'Samsung Galaxy S3 - 4.3 - API 18 - 720x1280 - 4.3': uninstalling com.nordstrom.fla.test
15:07:08.252 [DEBUG] [org.gradle.api.Project] DeviceConnector 'Samsung Galaxy S3 - 4.3 - API 18 - 720x1280 - 4.3': uninstalling com.nordstrom.fla
15:07:08.666 [INFO] [org.gradle.api.Project] deleteDir(/Users/graemeharnish/workspace/fla-android/app/build/outputs/reports/androidTests/connected) returned: true
15:07:08.702 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':app:connectedAndroidTest'
15:07:08.702 [LIFECYCLE] [class org.gradle.TaskExecutionLogger] :app:connectedAndroidTest FAILED

堆栈跟踪同样无益

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:connectedAndroidTest'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:305)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88)

我的gradle依赖

dependencies {
apt "org.robobinding:codegen:$robobindingVersion"
compile "org.robobinding:robobinding:$robobindingVersion:with-aop"
aspectPath("org.robobinding:robobinding:$robobindingVersion:with-aop")

compile 'org.lucasr.twowayview:twowayview:0.1.4'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'
provided 'com.android.support:appcompat-v7:21.0.3'
compile 'net.hockeyapp.android:HockeySDK:3.5.0-b.4'
compile 'me.dm7.barcodescanner:zbar:1.5'
compile 'com.squareup.picasso:picasso:2.4.0'
compile 'com.squareup:otto:1.3.5'

compile 'org.apache.commons:commons-lang3:3.0'

compile 'com.nispok:snackbar:2.8.0'

compile 'com.rengwuxian.materialedittext:library:1.8.0'
compile 'com.crittercism:crittercism-android-agent:+'

provided 'org.roboguice:roboblender:3.+'

compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.melnykov:floatingactionbutton:1.1.0'

compile 'com.google.code.findbugs:jsr305:1.3.9'
compile 'com.android.support:support-annotations:21.0.3'

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'

androidTestCompile('org.mockito:mockito-core:1.9.5',
        'com.google.dexmaker:dexmaker-mockito:1.1',
        'com.google.dexmaker:dexmaker:1.1')

}

有什么奇怪的是它在5.0设备上工作没问题。

3 个答案:

答案 0 :(得分:9)

当你的应用程序和测试应用程序具有相同的依赖关系时,较旧的设备在运行测试时遇到问题。

要解决此问题,您必须确定哪些依赖项会导致问题。

在我的情况下,Dagger和Espresso都取决于javax.inject,这就是"修复":

androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
    exclude group: 'javax.inject'
}

如果您包含更多或更奇怪的依赖项,您可以查看this build.gradle

使用espresso-contrib时,您可能需要这样做:

androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
    exclude group: 'javax.inject'
    exclude group: 'com.android.support'
}

答案 1 :(得分:5)

当相同的依赖项被包含多次或使用不同的版本时,通常会出现问题。

我有一个工作项目使用espresso2和multidex for api level 14 +。

检查this SO question。接受的答案帮了很多忙。

大多数情况下,使用./gradlew -q:myproject:dependencies,您可以检查重复的依赖关系或不同版本的相同依赖关系不止一次。

这是我的espresso依赖项的一个例子,但我不得不改变并排除许多:

....
//Espresso 2
androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') { 
    exclude group: 'com.google.guava'
    exclude module: 'espresso-idling-resource'
}
androidTestCompile('com.android.support.test:testing-support-lib:0.1') { 
    exclude group: 'com.google.guava'
    exclude module: 'espresso-idling-resource'
}
testingCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
    exclude group: 'com.google.guava'
    exclude group: 'com.android.support', module: 'support-v4'
}
// Need to exclude this when running test
androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') {
    exclude group: 'com.android.support', module: 'multidex' 
}
....

./ gradlew -q:myproject:dependencies 将为每个变体+风味组合提供如下输出:

_prodTestingTestApk - ## Internal use, do not manually configure ##
+--- com.android.support.test.espresso:espresso-core:2.0
|    +--- com.squareup:javawriter:2.1.1
|    +--- org.hamcrest:hamcrest-integration:1.1
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- org.hamcrest:hamcrest-library:1.1
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- javax.inject:javax.inject:1
|    +--- com.android.support.test:testing-support-lib:0.1
|    |    \--- junit:junit-dep:4.10
|    |         \--- org.hamcrest:hamcrest-core:1.1
|    +--- com.google.code.findbugs:jsr305:2.0.1
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- org.hamcrest:hamcrest-core:1.1
+--- com.android.support.test:testing-support-lib:0.1 (*)
\--- com.android.support:multidex-instrumentation:1.0.1

答案 2 :(得分:5)

我在测试中使用RecyclerView时遇到了类似的问题,通过在build.gradle添加以下代码解决了这个问题:

configurations {
    androidTestCompile.exclude group: 'com.android.support', module: 'recyclerview-v7'
}

此外,您还可以尝试添加以下排除:

configurations {
    androidTestCompile.exclude group: 'com.android.support', module: 'support-v4'
} 

所以,configurations build.gradle看起来像那样:

configurations {
    androidTestCompile.exclude group: 'com.android.support', module: 'support-v4'
    androidTestCompile.exclude group: 'com.android.support', module: 'recyclerview-v7'
} 

这解决了我遇到的两个问题:java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementationjava.lang.NoClassDefFoundError。之后,我能够对棒棒糖前设备进行测试。