Saad Farooq
Saad Farooq

Reputation: 13402

Instrumentation tests fail on virtual devices

I am using Jake Wharton's Double Espresso library for setting up Instrumentation tests in my app. The tests run fine on my phone but crash on any virtual device (I tried an emulator and Genymotion device).

The error I get is the IllegalAccessError that has been reported [previously]((Instrumentation run failed due to 'java.lang.IllegalAccessError'. Gradle + Espresso). However, what puzzles me is why it only happens on virtual devices. The setup should be fine if it's able to run on physical devices.

The stacktrace includes errors all over the place but the one reported as fatal is :

java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
        at com.myApp.onCreate(MyApp.java:32)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4344)
        at android.app.ActivityThread.access$1500(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

In any case, I tried excluding all the libraries that could have possible clashes but I still can't get it to work. Does anyone have any ideas on what the issue might be or even how to troubleshoot ?

The problem does seem to be related to daggger which is being used in the app as well. MyApp:32 points to the line where ObjectGraph is created and the following two lines are in the stacktrace.

W/dalvikvm﹕ Class resolved by unexpected DEX: Lcom/myApp/MyApp;(0xa4df7388):0x97542000 ref [Ldagger/ObjectGraph;] Ldagger/ObjectGraph;(0xa4df7388):0x97add000
W/dalvikvm﹕ (Lcom/myApp/MyApp; had used a different Ldagger/ObjectGraph; during pre-verification)

**UPDATE: ** The issue is likely related to the Dalvik VM on the virtual devices. My phone is set to ART and similar issues with Dalvik have been reported previously. It should be fixed by adding the following:

androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
  exclude group: 'com.squareup.dagger'
}

as per the library page but that doesn't seem to help.

Upvotes: 2

Views: 816

Answers (1)

Saad Farooq
Saad Farooq

Reputation: 13402

The issue was actually caused by the espresso-support library. I needed to add com.squareup.dagger as an exclusion there too while I was only adding exclusions to the other library.

androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
    exclude group: 'com.squareup.dagger'
}
androidTestCompile ('com.jakewharton.espresso:espresso-support-v4:1.1-r3') {
    exclude group: 'com.squareup.dagger'  // this goes here too
    exclude group: 'com.android.support', module: 'support-v4'
}

Upvotes: 4

Related Questions