ClassNotFoundException during Android test instrument
I've seen many topics dealing with this error but could not found a working solution. So I'll try to describe the problem completely.
I've got a project and a project test. My project manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.toto.app"
android:versionCode="3"
android:versionName="0.3.1">
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
...
</application>
</manifest>
And my test project manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.toto.app.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<instrumentation android:targetPackage="com.toto.app" android:name="android.test.InstrumentationTestRunner" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Here is the activity i want to test : src/com/toto/app/activity/ListActivity, I've got a LaunchActivity (declared as start activity in manifest) that launch this activity.
And in the test, my ListActivityTest is in the same path (src/com/toto/app/activity). I've also tried with src/com/toto/app/test. The source is :
public class ListActivityTest extends ActivityInstrumentationTestCase2<LaunchActivity>{
private Solo solo;
public ListActivityTest() {
super(LaunchActivity.class);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
solo.wait(2000);
}
}
The LaunchActivity seems to be found but when it launch the ListActivity, i've got the error :
% adb shell am instrument -w com.toto.app.test/android.test.InstrumentationTestRunner
com.toto.app.activity.ListActivityTest:INSTRUMENTATION_RESULT: shortMsg=java.lang.ClassNotFoundException INSTRUMENTATION_RESULT: longMsg=java.lang.ClassNotFoundException: com.toto.app.activity.ListActivity in loader dalvik.system.PathClassLoader[/system/framework/android.test.runner.jar:/data/app/com.toto.app.test-2.apk:/data/app/com.toto.app-2.apk]
I don't understand why one class is found and the other is not... It fails in the same way in Eclipse. I tried to use directly ListActivity in the testcase but same error. How does it build this apk to avoid some class ?
This is the full stack when using adb.
The full stack in eclipse logcat :
java.lang.NoClassDefFoundError: com.toto.app.activity.ListActivity
at com.toto.app.activity.LaunchAct开发者_C百科ivity.onCreate(LaunchActivity.java:19)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.toto.app.activity.ListActivity in loader dalvik.system.PathClassLoader[/system/framework/android.test.runner.jar:/data/app/com.toto.app.test-2.apk:/data/app/com.toto.app-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
... 14 more
It is because my activity extends a FragmentActivity, not a simple Activity.
To resolve the problem, I had to export android-support-v4.jar in the main project.
Hope that will help others...
精彩评论