16

I'm writing tests for Android project right now and just don't understand why is this such a pain! After a whole day of setup I finally get it work, but now, after I have written several test classes Intellij IDEA stands:

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.IllegalAccessError''. Check device logcat for details
Test running failed: Instrumentation run failed due to 'java.lang.IllegalAccessError'

The tests I was running just a couple of minutes ago can't be run anymore. Taking into account I rolled back to my latest commit where everything was ideal and I wasn't changing any settings I'm just wondering why.

Here is what logcat is saying:

02-12 20:16:09.398: E/AndroidRuntime(4922): FATAL EXCEPTION: main
02-12 20:16:09.398: E/AndroidRuntime(4922): java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.actionbarsherlock.view.MenuInflater$MenuState.readItem(MenuInflater.java:327)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:147)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:97)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at <package>.ui.CheckPasswordActivity.onCreateOptionsMenu(CheckPasswordActivity.java:130)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.support.v4.app._ActionBarSherlockTrojanHorse.onCreatePanelMenu(_ActionBarSherlockTrojanHorse.java:45)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:556)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:60)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:154)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:407)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:769)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:201)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.view.Choreographer.doFrame(Choreographer.java:531)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.os.Handler.handleCallback(Handler.java:725)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.os.Looper.loop(Looper.java:137)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at java.lang.reflect.Method.invoke(Method.java:511)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-12 20:16:09.398: E/AndroidRuntime(4922):     at dalvik.system.NativeStart.main(Native Method)

Although I didn't even touched this class, the CheckPasswordActivity line:130 it refers to is just:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu_check_password, menu);
        return true;
    }
Eugene
  • 59,186
  • 91
  • 226
  • 333
  • sounds like some dependency issue with the support library. check in the dependencies of your test module settings (F4 in IDEA) that both the support library and ActionBarSherlock are with 'provided' scopes – Gal Ben-Haim Feb 12 '13 at 18:40
  • Can you tell me more please? I have abs, module where my sources are, support library and test module where my tests are. How these dependencies should look like? – Eugene Feb 12 '13 at 18:44
  • In the module settings make sure the scopes are provided for abs and the support library – Gal Ben-Haim Feb 12 '13 at 20:26

6 Answers6

23

I have finally found a solution. The problem was with dependencies indeed, it is still unknown why it used to work and then suddenly refused, but here is how the dependencies should look like for your test module:

enter image description here

So all in all you need to make sure all your libraries and project libraries are listed for your test module and marked as "Provided" except Robotium lib, which is "Compile".

Eugene
  • 59,186
  • 91
  • 226
  • 333
  • This worked for me. In my case, I also had dependencies on Mockito libraries. I essentially marked all dependencies as "Provided", except for the libraries that my test project directly depended on. Those were marked as "Compile". – Eric Schlenz Jun 03 '13 at 19:00
2

In my case, it is due to the duplicate jars being included.

srikanth Nutigattu
  • 1,077
  • 9
  • 15
1

Based on your other question... I think I have somewhat of a similar setup as you... Here is pretty much how my dependencies are set (read sub-items as dependencies)

  • ActionBarSherlock
    • android-support-v4
  • Android Module
    • ActionBarSherlock
    • android-support-v4
  • Unit Test
    • robotium
    • Android module

All the dependencies are all setup as "compile"

I use ActionBarSherlock from the source code and that module has "Is a library project" checked.

Community
  • 1
  • 1
Matthieu
  • 16,103
  • 10
  • 59
  • 86
  • I have the same dependencies, double checked them, didn't help - I still can't run some of my tests for unknown reason. – Eugene Feb 13 '13 at 06:35
0

I just had the very same problem try to do following as it resolved the problem.

Remove android-support-v4 lib from your test project(or any lib that is doubled for that matter). Clean projects and build it again.

MatBos
  • 2,380
  • 24
  • 34
0

Add the following line :

manifestmerger.enabled=true 

to your project.properties file of your application project.

Did the fix for me :) Had a Project with a Library Project

cV2
  • 5,229
  • 3
  • 43
  • 53
0

I had somewhat the same error. When I ran the test class and one method would fail, (like checking a textview's text but instead the text was other than what you checked.), instead of showing the right usual error of saying what should normally say, it would show illegal access error and the whole test class would be stopped, without running the other tests.

My solution was to read the illegal access error and see/google search which dependency was missing, and add this dependency in build.gradle file, in dependencies section.

For ex. it showed me this error: java.lang.IllegalAccessError: Illegal class access: 'androidx.test.core.app.ListFuture' attempting to access 'androidx.concurrent.futures.DirectExecutor' (declaration of 'androidx.test.core.app.ListFuture' ...etc etc. What i saw from this post and other sources that was something was going on with my dependencies, or was missing.

So from the error i see: ...attempting to access 'androidx.concurrent.futures.DirectExecutor' ... So i google searched 'androidx.concurrent.futures.DirectExecutor' dependency. I've found the official android site that says about "concurrent" dependency, so ive added that dependency(implementation "androidx.concurrent:concurrent-futures:1.1.0" ) to my build.gradle(Module: MyAppName.app), in dependencies section and my problem was solved!

I'm posting this here, for someone else that has "Illegal access error" other than the op or even for the op that sees this. So read the error message. Somewhere might say other missing method and google search it. I know its a bit different error, but its still "Illegal access error" and would help others having somewhat similar error.

John Xenakis
  • 211
  • 2
  • 8