Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError in android.test.ServiceTestCase

Tags:

android

Every time I start my Android instrumentation test it fails with

android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests:
Error in testSuiteConstructionFailed:
java.lang.RuntimeException: Exception during suite construction
    at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.reflect.InvocationTargetException
    at com.samy4me.test.Test_Service2.<init>(Test_Service2.java:26)
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
    at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
    at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
    at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
    at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
    at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4218)
    at android.app.ActivityThread.access$3000(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.XXX.ws.Service
    ... 19 more

The class is there. I checked ProGuard, I temporarily deactivated ProGuard. I reduced the test to the absolute minimum:

public class Test_Service2
   extends android.test.ServiceTestCase<Service>
{

   public Test_Service2 ()
   {
      super (Service.class);
   } // Test_Service2
} // Test_Service2

Has anybody got an idea how this might have come about?

Looking closer I now found the following:

04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580): Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at dalvik.system.DexFile.defineClass(Native Method)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:209)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:203)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     ... 26 more

This is part of the original error exception chain but was truncated. The pre-verified exception is not new and yet another pain in Android development.

like image 228
Martin Avatar asked Dec 28 '22 21:12

Martin


1 Answers

OK, I found the problem - mostly PEBKAC. So here it is:

  1. I have looked only at the console. But there the error message was truncated. Lesson learned: always check logcat as well.
  2. The real error was the well know pre-verified error which happens when libraries meant for the main application get dragged into test application. Lesson learned: always make sure that libraries are not dragged into the instrumentation test

If you use Maven — like I do — then you can read the details up here Automate Android Test Project, which — for added embarrassment — I wrote myself two weeks ago.

like image 198
Martin Avatar answered Dec 30 '22 11:12

Martin