Search code examples
androidtestingspoonandroid-multidexexpresso

Tests fail in Espresso with devices under Lollipop (failed: Instrumentation run failed due to 'java.lang.IllegalAccessError')


I'm trying to run expresso tests with Spoon in a multidex project. Tests are running without problem in devices/VM with Lollipop or Marshmallow, but they are not working under Lollipop devices.

There are some other questions about this problem in stackoverflow and all solutions talk about avoid duplicates of libraries in the dependencies configuration of Gradle, but I couldn't find them nor the any other problem.

This is the result of make a ./gradlew -q :project:dependencies

_debugAndroidTestApk - ## Internal use, do not manually configure ##
+--- com.android.support.test:runner:0.4.1
|    +--- com.android.support.test:exposed-instrumentation-api-publish:0.4.1
|    \--- junit:junit:4.12
|         \--- org.hamcrest:hamcrest-core:1.3
+--- com.android.support.test:rules:0.4.1
|    \--- com.android.support.test:runner:0.4.1 (*)
+--- com.android.support.test.espresso:espresso-contrib:2.2.1
|    +--- com.android.support.test.espresso:espresso-core:2.2.1
|    |    +--- com.squareup:javawriter:2.1.1
|    |    +--- com.android.support.test:runner:0.4.1 (*)
|    |    +--- com.android.support.test:rules:0.4.1 (*)
|    |    +--- org.hamcrest:hamcrest-library:1.3
|    |    |    \--- org.hamcrest:hamcrest-core:1.3
|    |    +--- org.hamcrest:hamcrest-integration:1.3
|    |    |    \--- org.hamcrest:hamcrest-library:1.3 (*)
|    |    +--- com.google.code.findbugs:jsr305:2.0.1
|    |    +--- javax.annotation:javax.annotation-api:1.2
|    |    +--- com.android.support.test.espresso:espresso-idling-resource:2.2.1
|    |    \--- javax.inject:javax.inject:1
|    \--- com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:2.0
|         \--- org.hamcrest:hamcrest-core:1.3
+--- com.android.support.test.espresso:espresso-core:2.2.1 (*)
+--- com.android.support.test.espresso:espresso-intents:2.2.1
|    \--- com.android.support.test.espresso:espresso-core:2.2.1 (*)
+--- com.android.support:multidex-instrumentation:1.0.1
+--- com.squareup.spoon:spoon-client:1.2.0
\--- project :multidextest
 +--- com.android.support:multidex:1.0.1
 \--- com.android.support.test:runner:0.4.1 (*)

Those are our dependencies:

dependencies {
    compile project(':MemorizingTrustManager')
    compile project(':widgets')
    compile fileTree(dir: 'libs')
    compile 'com.android.support:multidex:1.0.1'
    compile "org.igniterealtime.smack:smack-android-extensions:4.1.3"
    compile "org.igniterealtime.smack:smack-experimental:4.1.3"
    compile "org.igniterealtime.smack:smack-tcp:4.1.3"
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.jakewharton:butterknife:6.1.0'
    compile 'com.squareup:otto:1.3.7'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'
    compile 'com.google.dagger:dagger:2.0.1'
    apt 'com.google.dagger:dagger-compiler:2.0.1'
    provided 'org.glassfish:javax.annotation:10.0-b28'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.github.pedrovgs:renderers:2.0.3'
    compile 'com.getbase:floatingactionbutton:1.10.0'
    compile 'fr.avianey.com.viewpagerindicator:library:2.4.1@aar'
    compile 'com.google.android.gms:play-services-analytics:8.1.0'
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile "com.mixpanel.android:mixpanel-android:4.6.4"
    compile 'org.apmem.tools:layouts:1.8@aar'
    compile('com.crashlytics.sdk.android:crashlytics:2.3.2@aar') {
        transitive = true;
    }
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-all:1.10.19'
    testCompile 'org.hamcrest:hamcrest-all:1.3'
    androidTestCompile ('com.android.support.test:runner:0.4.1') {
        exclude module: 'support-annotations'
    }
    androidTestCompile ('com.android.support.test:rules:0.4.1') {
        exclude module: 'support-annotations'
    }
    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') {
        exclude module: 'support-annotations'
        exclude module: 'support-v4'
        exclude module: 'support-v13'
        exclude module: 'recyclerview-v7'
        exclude group: 'javax.inject'
    }
    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.1') {
        exclude module: 'support-annotations'
        exclude group: 'javax.inject'
    }
    androidTestCompile ('com.android.support.test.espresso:espresso-intents:2.2.1') {
        exclude module: 'support-annotations'
    }
    androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') {
        exclude group: 'com.android.support', module: 'multidex'
    }
    androidTestCompile "com.squareup.spoon:spoon-client:1.2.0"
    androidTestCompile project(':multidextest')
}

And finally the stacktrace with the problem:

11:18:24 D/Device: Uploading file onto device 'emulator-5554'
2015-11-17 11:18:49 [SDR.run] About to actually run tests for [emulator-5554]
11:18:49 I/RemoteAndroidTest: Running am instrument -w -r  -e class com.redbooth.newredbooth.presentation.tests.collaboration.dashboard.DashboardActivityTest com.project.debug.test/com.project.multidextest.MultidexAndroidJUnitRunner on Nexus_One_API_15 [emulator-5554]
11:18:49 I/InstrumentationResultParser: test run failed: 'Instrumentation run failed due to 'java.lang.IllegalAccessError''
2015-11-17 11:18:49 [STRL.testRunStarted] testCount=0 runName=com.project.debug.test
2015-11-17 11:18:49 [STRL.testRunFailed] errorMessage=Instrumentation run failed due to 'java.lang.IllegalAccessError'
2015-11-17 11:18:49 [STRL.testRunEnded] elapsedTime=0
11:18:49 I/XmlResultReporter: XML test result file generated at /Users/disaster/Repositorios/Project/android/project/build/custom-report-dir/debug/junit-reports/emulator-5554.xml. Total tests 0, 
2015-11-17 11:18:50 [SDR.run] About to grab screenshots and prepare output for [emulator-5554]
2015-11-17 11:18:50 [SDR.pullScreenshotsFromDevice] Internal path is /data/data/com.project.debug/app_spoon-screenshots
2015-11-17 11:18:50 [SDR.pullScreenshotsFromDevice] External path is /mnt/sdcard/app_spoon-screenshots
2015-11-17 11:18:50 [SDR.pullScreenshotsFromDevice] Pulling screenshots from external dir on [emulator-5554]
2015-11-17 11:18:50 [SDR.pullScreenshotsFromDevice] Pulling screenshots from internal dir on [emulator-5554]
2015-11-17 11:18:50 [SDR.pullScreenshotsFromDevice] Done pulling screenshots from [emulator-5554]
2015-11-17 11:18:50 [SDR.run] Moving screenshots to the image folder on [emulator-5554]
2015-11-17 11:18:50 [SDR.run] Generating animated gifs for [emulator-5554]
2015-11-17 11:18:50 [SDR.run] Done running for [emulator-5554]
2015-11-17 11:18:50 [SR.runTests] [emulator-5554] Execution done.
:project:spoonDebugAndroidTest FAILED

Solution

  • Finally, I found the problem ><.

    The AndroidJUnitRunner is compatible with Multidex so, create an alternative Runner was not necessary, as you can read in the last messages of: https://plus.google.com/+OleksandrKucherenko/posts/i7qZdVEy3Ue

    On the other hand, I had a module with the new runner and some tests and it was not really necessary. I deleted the module and configured again the testInstrumentationRunner as testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' and the problem is fixed.!