Friday, February 13, 2015

Discovering Espresso for Android: Espresso 2.0 and java.lang.NoClassDefFoundError

Currently I'm moving all my tests to Espresso 2.0 and Junit4. And today I was struggling with one nasty issue that you can face with as well and found the solution.

So, the problem was noticed on pre-Lollipop devices/emulators. My app under test is configured to target the current latest API level 21. And after adapting some tests to Espresso 2.0 and JUnit4 I was able to successfully run it on emulator with Lollipop but trying to run it on devices/emulators lower then API 21 was failing with below issue:
java.lang.NoClassDefFoundError: com.my.app.activities.MyActivity
at com.my.app.test.instrumentation.TestMyActivity.(MyActivity.java:52)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
.
.
.
And solution is to add below peace of code into your build.gradle:
configurations {
    androidTestCompile.exclude group: 'com.android.support', module: 'support-v4'
}
:)