Bersh
Bersh

Reputation: 2809

No virtual method findViewHolderForPosition(I) when trying to click on RecyclerView item with Espresso

I'm trying to make a simple espresso test that will find the first item in RecyclerView without specific label and click on it. To achieve this I added espresso-contrib to project like this:

androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
    // Necessary to avoid version conflicts
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'design'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'appcompat-v7'
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude module: 'recyclerview-v7'
}

and wrote the following expression in my test case: onView(withId(R.id.sresults_list_recycler)).perform(RecyclerViewActions.actionOnHolderItem(new FirstNotSoldOutMatcher(), click()).atPosition(1));

Matcher works perfectly and RecyclerView got scrolled to the target item. But then I get error:

java.lang.NoSuchMethodError: No virtual method findViewHolderForPosition(I)Landroid/support/v7/widget/RecyclerView$ViewHolder; in class Landroid/support/v7/widget/RecyclerView; or its super classes (declaration of 'android.support.v7.widget.RecyclerView' appears in /data/app/com.example-1/base.apk)
at android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:288)
at android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemViewAction.perform(RecyclerViewActions.java:232)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

This looks strange to me. I read in Google documentation that findViewHolderForPosition method is deprecated now but it should still be there. I also have multidex enabled in the app but according to docs it is supported out of the box so shouldn't be a problem too.

Do you have any idea what can be wrong with this test?

Upvotes: 6

Views: 2263

Answers (2)

tir38
tir38

Reputation: 10421

You can combine @Bersh's answer and this answer to be more strict about what to keep:

-keep class android.support.v7.widget.RecyclerView  {
    public android.support.v7.widget.RecyclerView$ViewHolder findViewHolderForPosition(int);
}

There is no reason to keep the entire RecyclerView class.

Update:

Or for when you inevitably update to AndroidX:

-keep class androidx.recyclerview.widget.RecyclerView  {
    public androidx.recyclerview.widget.RecyclerView$ViewHolder findViewHolderForPosition(int);
}

Upvotes: 3

Bersh
Bersh

Reputation: 2809

Ok, in my case problem was in ProGuard shrinking unused methods. Disabling it for tests helped.

For future seekers - if you don't want to disable proguard for debug build adding this line to proguard config should help too:

-keepclasseswithmembers public class android.support.v7.widget.RecyclerView { *; }

Also, note that this rule should be added to the regular proguard file(the one of listed in proguardFiles) and not the test one(declared as testProguardFile)

Upvotes: 8

Related Questions