Conflict with Robolectric and SlideUpPanelLayout

The other day I was trying to integrate Robolectric with my project that uses the SlideUpPanelLayout. If I removed either Robolectric from the test build or SlideUpPanelLayout from my project it fixed the issue (temporarily, because I needed both) When using Robolectric and SlideUpPanelLayout together I get the following error.

Test failed to run to completion. Reason: ‘Instrumentation run failed due to 'java.lang.IllegalAccessError''. Check device logcat for details
Test running failed: Instrumentation run failed due to 'java.lang.IllegalAccessError'

This error is down to a Gradle configuration error. The reason is that there are dependencies declared multiple times in the build and one of them needs to be excluded. The challenge in this situation is knowing which dependency it is.

To work this out look at the exception which in this case ended with

Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
at com.sothree.slidinguppanel.ViewDragHelper.<init>(
at com.sothree.slidinguppanel.ViewDragHelper.create(
at com.sothree.slidinguppanel.ViewDragHelper.create(
at com.sothree.slidinguppanel.SlidingUpPanelLayout.<init>(
at com.sothree.slidinguppanel.SlidingUpPanelLayout.<init>(
... 26 more

ViewDragHelper was the culprit so if in your IDE (Android Studio) open the search for class name (Command-O in OS X) and enter ViewDragHelper. You will see that there are multiple Jars that are returned. You will need to exclude the one that you don’t want.

In this situation we had the android-19 sdk, support-v4-19 and support-v4-20 and library-2.0.1 (the slideuppanellayout library) to solve this I needed to exclude support-v4.

So in the build gradle file for the project where I included robolectric I needed to add the following to the list of excludes.

exclude module: 'support-v4'

This solved the problem!