Test Error - NoClassDefFoundError: Failed resolution of: Lorg/hamcrest/Matchers

Test Error - NoClassDefFoundError: Failed resolution of: Lorg/hamcrest/Matchers

我正在使用 Espresso 进行 Instrumented Test,但在 Stack Trace 上出现此错误:

由于缺少 class 导致的错误,如下所示:

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

我在片段测试中添加此代码后立即发生:

这些是我在 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

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

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

之前包括espresso-contribespresso-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"

之后注释掉 espresso-contribespresso-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"

TLDR answer,你不需要降级整个浓缩咖啡设置 Hamcrest 版本冲突与传递依赖关系很容易解决:

如果您正在使用:

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

//change it to:
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'

如果您正在使用:

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

//change it to:
androidTestImplementation "androidx.test.espresso:espresso-accessibility:3.3.0"

您应该仍然能够将 espresso core 保持在更高的 3.4.0 版本


如果想看懂,这里有更详细的解释:

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

./gradlew :app:dependencies

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

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

[