<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>
我的测试项目显示:
<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>
这是我要测试的活动:src / com / toto / app / activity / ListActivity,我有一个启动此活动的LaunchActivity(在清单中声明为启动活动).
在测试中,我的ListActivityTest位于相同的路径中(src / com / toto / app / activity).我也试过src / com / toto / app / test.来源是:
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); } }
似乎找到了LaunchActivity但是当它启动ListActivity时,我得到了错误:
% 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]
我不明白为什么找到一个类而另一个不是……它在Eclipse中以同样的方式失败.我试图在测试用例中直接使用ListActivity但是同样的错误.它如何构建这个apk以避免某些类?
这是使用adb时的完整堆栈.
eclipse logcat中的完整堆栈:
java.lang.NoClassDefFoundError: com.toto.app.activity.ListActivity at com.toto.app.activity.LaunchActivity.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
解决方法
要解决这个问题,我必须在主项目中导出android-support-v4.jar.
希望能帮助别人……