测试错误 - NoClassDefFoundError:解析失败:Lorg/hamcrest/Matchers

Fan*_*dez 11 dependencies android gradle android-testing android-espresso

我正在使用 Espresso 进行仪器测试,但在堆栈跟踪上出现此错误:

在此输入图像描述

该错误是由于缺少类引起的,如下所示:

Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hamcrest.Matchers" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/android.test.base.jar", zip file "/data/app/~~vnZzxGNKnS4V6YkEf4falA==/com.example.android.architecture.blueprints.reactive.test-K_x0_yJ0hJeDHaJkDmHXRw==/base.apk", zip file "/data/app/~~oeYx2MgTcILbk-vq_WPx1A==/com.example.android.architecture.blueprints.reactive-0wMHYEe95hx_1cnbdAoZAw==/base.apk"],nativeLibraryDirectories
Run Code Online (Sandbox Code Playgroud)

它首先发生在我在片段测试中添加此代码后:

在此输入图像描述

这些是我在 Gradle 上的相关库:

在此输入图像描述

我有这些进口:

import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.core.IsNot.not
Run Code Online (Sandbox Code Playgroud)

Hen*_*eMS 27

TLDR 答案,您不需要降级整个 ​​espresso 设置,因为 Hamcrest 版本与传递依赖项存在冲突,可以轻松解决:

如果您正在使用:

androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'

//change it to:
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'
Run Code Online (Sandbox Code Playgroud)

如果您正在使用:

androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.4.0'

//change it to:
androidTestImplementation "androidx.test.espresso:espresso-accessibility:3.3.0"
Run Code Online (Sandbox Code Playgroud)

您应该仍然能够将 espresso 核心保留在更高的 3.4.0 版本


如果你想理解,这里有更长的解释:

要查看幕后发生的情况,我们可以使用依赖项 gradle 任务在 android studio 终端中打印出依赖关系树:

./gradlew :app:dependencies
Run Code Online (Sandbox Code Playgroud)

我们可以使用两个版本级别的依赖关系运行它两次,并可以检查差异。

  • 在某些情况下,有人请求 hamcrest 库版本 1.3,它与大多数常见的 Android 依赖项相匹配。
  • 在左侧(v3.3.0 的依赖树),每个 hamcrest 传递依赖项均按要求提供,毫不奇怪它可以工作!
  • 在右侧(v3.4.0 的依赖树),我们可以看到一些请求在更高版本上“替代”了 hamcrest 库,在这种情况下,这不起作用。

[Gradle依赖树比较1

  • 哇,感谢您对此进行更深入的研究,这对帮助社区大有帮助。 (2认同)
  • @Tonnie不用担心,如果我们了解了根本问题,我相信这些知识稍后会派上用场;)在这种情况下,“如何做”可能会帮助其他人摆脱其他一些传递依赖问题 (2认同)
  • 这很好。但它没有给出解决方案,即将其包含在 build.gradle 文件中 ```androidTestImplementation 'org.hamcrest:hamcrest:2.2'``` 为什么这仍然有效?如果问题是 espresso 想要 hamcrest 1.3 但得到 2.2? (2认同)

小智 14

我没有尝试接受的答案,但有些回复保存了我的a$$,只需添加:

androidTestImplementation "org.hamcrest:hamcrest:2.2"
Run Code Online (Sandbox Code Playgroud)


Fan*_*dez 6

经过一段时间的挣扎,我意识到错误是由espresso-contibespresso-accessibility依赖性引起的。我的浓缩咖啡版本是最新版本 3.4.0-alpha05

我删除了它们并且测试通过了。

在包含espresso-contrib&之前espresso-accessibility

/*core contains matches and view assertions, included by default on android project*/
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"

//testing code for advanced views such as recyclerview and Date picker
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion"
Run Code Online (Sandbox Code Playgroud)

注释掉espresso-contrib&espresso-accessibility

//core contains matches and view assertions, included by default on android project
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
//testing code for advanced views such as recyclerview and Date picker
//androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
//androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion"
Run Code Online (Sandbox Code Playgroud)

  • 我似乎已经使用 `androidTestImplementation "org.hamcrest:hamcrest:2.2"` 解决了这个问题 (4认同)
  • 为了测试 RecyclerView 等,我降级到 `espressoVersion = '3.3.0'` (3认同)