Search code examples
androidintellij-idearobotiumandroid-fragmentactivityandroid-testing

java.lang.NoClassDefFoundError for FragmentActivity class when running Robotium


I've tried everything I could think of, but still I cannot run my tests using Robotium.

I set the robotium library and my main project to be exported in the Project configuration as suggested here

I've set the android:debuggable flag to true as suggested here

I also checked the android:targetPackage is set properly in the manifest as suggested here

Also tried to change the scope of the main project dependency when building the test module to Provided instead of Compile (I am not sure what that does...)

Here is the AndroidManifest for the Test project:

<?xml version="1.0" encoding="utf-8"?>
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.matthieu.tests"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-sdk android:minSdkVersion="4"
              android:targetSdkVersion="17" />

    <application
            android:debuggable="true">
        <uses-library android:name="android.test.runner"/>
    </application>

    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.matthieu"
                     android:label="Tests for com.matthieu"/>
</manifest>

And here is the exception I get:

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.matthieu.MainMenuActivityTest.<init>(MainMenuActivityTest.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.matthieu.MainMenuActivity
... 19 more

Note also that I see a few errors like this in the logcat:

01-04 14:44:51.822: WARN/dalvikvm(1263): Class resolved by unexpected DEX: Lcom/matthieu/MainMenuActivity;(0x44e7f980):0x249f70 ref [Lcom/actionbarsherlock/app/SherlockFragmentActivity;] Lcom/actionbarsherlock/app/SherlockFragmentActivity;(0x44e7f980):0x121770
01-04 14:44:51.833: WARN/dalvikvm(1263): (Lcom/matthieu/MainMenuActivity; had used a different Lcom/actionbarsherlock/app/SherlockFragmentActivity; during pre-verification)
01-04 14:44:51.851: WARN/dalvikvm(1263): Unable to resolve superclass of Lcom/matthieu/MainMenuActivity; (644)
01-04 14:44:51.851: WARN/dalvikvm(1263): Link of class 'Lcom/matthieu/MainMenuActivity;' failed
01-04 14:44:51.851: ERROR/dalvikvm(1263): Could not find class 'com.matthieu.MainMenuActivity', referenced from method com.matthieu.MainMenuActivityTest.testFirstTimeAppOpening
01-04 14:44:51.851: WARN/dalvikvm(1263): VFY: unable to resolve check-cast 849 (Lcom/matthieu/MainMenuActivity;) in Lcom/matthieu/MainMenuActivityTest;

If it makes any difference, I am using IntelliJ Idea (11.1.5), SDK 17, SDK Tools 21.0.1 and updated Robotium to 3.6

UPDATE:

Found one related discussion here. I can see different error messages whether I run only one test or all the tests, and my application does use FragmentActivity. But no matter what I do, I am not able to run any test (not one, not all)...


Solution

  • Finally.... I found the only way I could find to make it work there

    Do not keep the Robotium test cases in a separate module... no idea why there is no way to make it work, but really I tried everything I could think of or I could find around.