Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

Android Instrumentation test - AndroidTestCase: java.lang.IllegalAccessError

3 min read

Today I started writing the instrumentation tests for the Android platform side of the project. For your understanding, here's the structure of my situation:
  • Library Project
    This part of the project encapsulates general functionality and functions as platform independent class library allowing to plug-in the platform dependent components. Testing is straightforward and can be done with plain-old jUnit tests.

  • Android Project
    This is the part of the project which is being deployed on the Android OS platform. It uses the previously mentioned "Library Project" which is directly referenced in the Eclipse workspace.

  • Android Test Project
    This project contains all of the Android platform specific instrumentation tests.
So far everything's good. In the Android Test Project I'm writing my test cases extending from the AndroidTestCase class which gives me basic Android-platform specific objects like the Context object. The Android Test Project references the Android Project for testing it, obviously :) . Moreover since in the test cases I'm using objects from the Library Project I've referenced also this one.

For the purpose of comfortableness you create a generic suite like the following
public class AllInstrumentationTests extends TestSuite {

public static Test suite(){
return new TestSuiteBuilder(AllInstrumentationTests.class)
Then running this test using the correct menu entry "Android JUnit Test"...

...gave the error
[2010-06-03 21:45:04 - DroidSenseInstrumentationTests] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2010-06-03 21:45:10 - DroidSenseInstrumentationTests] Test run failed: java.lang.IllegalAccessError
[2010-06-03 21:45:10 - DroidSenseInstrumentationTests] Test run complete
...with the more "concrete" message
java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
Beside the Eclipse jUnit runner which seems to have problem with the instrumentation tests, forcing me to often start them multiple times in order to get a result (hate that!!), it drove me crazy in trying to find the problem for it. I soon discovered it must depend on the included Library Project. This blog post then pointed me to the right direction and finally I solved the problem.

The Library Project shouldn't be included on the Android Test Project's build path, but instead you need to export (check it in the "Order and Export" tab) it from the Android Project. Otherwise your Android Test Project won't compile:
The same holds for externally included libraries.
  • Reference the library in the build path of your main project as usual, but also check it to be exported
  • In the test project just reference your main project which is subject to the tests. You will not need to also include the externally used library
Now everything should run "smoothly" (under quotation marks 'cause the Android jUnit integration in Eclipse is everything else than "smooth" right now).

Btw, running the tests from the shell rather than from within Eclipse runs them faster, however, remember to deploy your app on your emulator first. The command:
adb shell am instrument -w com.yourcompany.youapp.test/android.test.InstrumentationTestRunner

Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus