Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a Illegal Access Error when running my Android tests?

I get the following stack trace when running my Android tests on the Emulator:

java.lang.NoClassDefFoundError: client.HttpHelper
at client.Helper.<init>(Helper.java:14)  
at test.Tests.setUp(Tests.java:15)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:164)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:151)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:425)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1520)
Caused by: java.lang.IllegalAccessError: cross-loader access from pre-verified class
at dalvik.system.DexFile.defineClass(Native Method)
at dalvik.system.DexFile.loadClass(DexFile.java:193)  
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:203)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
... 11 more

I run my tests from an extra project. And it seems there are some problems with loading the classes from the other project. I have run the tests before but now they are failing. The project under tests runs without problems.

Line 14 of the Helper Class is:

this.httpHelper = new HttpHelper(userProfile);

I start a HttpHelper class that is responsible for executing httpqueries. I think somehow this helper class is not available anymore, but I have no clue why.

like image 881
Janusz Avatar asked Mar 12 '10 08:03

Janusz


1 Answers

I was having the same problem but in my case I was adding the library reference to both the application and test projects.

By removing the reference to library from the test project and leaving only the reference to the application project the problem was solved. The java.lang.IllegalAccessError exception and the message 'Class resolved by unexpected DEX' were the clues to help solve this problem.

like image 199
David Momenso Avatar answered Oct 23 '22 06:10

David Momenso