By

Problem
When trying to run a unit test against an Android project the following error is thrown “java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation”

Cause
“If your APK refers to a class, and it is resolved at dexopt time, and at run time it resolves to a different implementation of the same class, the VM gets upset and throws an exception.” e.g. you are referencing a jar in your application under test and referencing it again in the actual unit test project.

Solution
In Eclipse rather reference the jar in the application under test and export it. And then, in the test project, simply reference the application under test (do not include the jar). Example:
Step 1 – Update the main project you are testing:
1. Right click on the main project in the project explorer
2. Click on properties then go to “java build path”->”Order and Export”
3. Export the jar from the main project (see screenshot below)
preveri01

Step 2 – Update the test project:
Simply reference the main project from the test project (and remove any references to the jars exported in step 1)
1. Right click on the test project in the project explorer
2. Click on properties then go to “java build path”->”Project”
3. Click “Add” and tick the main project and click ok
4. Your project reference should now appear in the list under the “Project” tab:
preveri02
5. Still in “java build path” click on the Libraries tab and remove the jar/s you exported from step 1.

Share


  Hi, I'm Steven and the author of this blog
  Follow me on or