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

Viewed 2255

I am using Espresso for Instrumented Test but got this error on the Stack Trace:

enter image description here

The error being caused by a missing class as shown below:

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

It first occurred immediately after I added this code in the Fragment Test:

enter image description here

These are my relevant Libs on the Gradle:

enter image description here

I have these imports:

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
3 Answers

TLDR answer, you don't need to downgrade the whole espresso setup there is a Hamcrest version conflict with transitive dependencies which can be easily solved:

If you are using:

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

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

if you are using:

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

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

You should still be able to keep espresso core at the higher 3.4.0 version


If you want to understand, here is the longer explanation:

To see what is going on under the hood we can use the dependencies gradle task to print out the dependency tree, in android studio terminal:

./gradlew :app:dependencies

We can run it twice, using the dependencies at the two version levels and can check the differences.

  • At several points there are requests for hamcrest libraries version 1.3 which match most of the common android dependencies.
  • On the left side (dependency tree for v3.3.0) every hamcrest transitive dependency is provided as requested, no surprise it works!
  • On the right side (dependency tree for v3.4.0) we can see some of the requests got "substitute" hamcrest libraries at a higher version which, in this case, does not work.

[Gradle dependency tree comparison1

After struggling for a while I realized the error is being caused by espresso-contib and espresso-accessibility dependencies. My espresso version was the latest version 3.4.0-alpha05.

I removed them and tests passed.

Before including 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"

After comment out on both 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"

I didn't try the accepted answer, but some of the replies saved my a$$, just add:

androidTestImplementation "org.hamcrest:hamcrest:2.2"
Related