java - Unit test SherlockFragmentActivity class leads to Could not find class X referenced from method Y -
i try unit test activity extends sherlockfragmentactivity without success in application. isolate problem reproduced in simple sandbox project can't figure out how fix it.
my configuration
i use latest version of eclipse sdk & adt 22.6.2 on linux debian.
the project tested through emulator, in android 2.2 emulated device (api 8).
the target api android 4.4.2 (api 19).
jdk 1.6 (i tried jdk 1.7, see could not find class x referenced method y).
problem
i cannot run unit test on class extends sherlockfragmentactivity.
this output of console tab in eclipse:
[2014-04-06 13:01:02 - sandboxtest] dx trouble writing output: prepared [2014-04-06 13:01:03 - sandboxtest] ------------------------------ [2014-04-06 13:01:03 - sandboxtest] android launch! [2014-04-06 13:01:03 - sandboxtest] adb running normally. [2014-04-06 13:01:03 - sandboxtest] performing android.test.instrumentationtestrunner junit launch [2014-04-06 13:01:03 - sandboxtest] automatic target mode: using existing emulator 'emulator-5554' running compatible avd 'android_2.2' [2014-04-06 13:01:03 - sandboxtest] uploading sandboxtest.apk onto device 'emulator-5554' [2014-04-06 13:01:03 - sandboxtest] installing sandboxtest.apk... [2014-04-06 13:01:10 - sandboxtest] success! [2014-04-06 13:01:10 - sandboxtest] project dependency found, installing: sandbox [2014-04-06 13:01:11 - sandbox] application deployed. no need reinstall. [2014-04-06 13:01:11 - sandboxtest] launching instrumentation android.test.instrumentationtestrunner on emulator-5554 [2014-04-06 13:01:15 - sandboxtest] test run failed: test run failed complete. expected 2 tests, received 0
the test run failed: test run failed complete. expected 2 tests, received 0 due issue, catched logcat output, is:
could not find class 'com.example.sandbox.mainactivity', referenced method com.example.sandbox.test.mainactivitytest.<init> not find class 'com.example.sandbox.mainactivity', referenced method com.example.sandbox.test.mainactivitytest.setup
the full output here:
class resolved unexpected dex: lcom/example/sandbox/mainactivity;(0x45fa69a0):0x127410 ref [lcom/actionbarsherlock/app/sherlockfragmentactivity;] lcom/actionbarsherlock/app/sherlockfragmentactivity;(0x45fa69a0):0x12f910 (lcom/example/sandbox/mainactivity; had used different lcom/actionbarsherlock/app/sherlockfragmentactivity; during pre-verification) unable resolve superclass of lcom/example/sandbox/mainactivity; (824) link of class 'lcom/example/sandbox/mainactivity;' failed not find class 'com.example.sandbox.mainactivity', referenced method com.example.sandbox.test.mainactivitytest.<init> vfy: unable resolve const-class 1031 (lcom/example/sandbox/mainactivity;) in lcom/example/sandbox/test/mainactivitytest; class resolved unexpected dex: lcom/example/sandbox/mainactivity;(0x45fa69a0):0x127410 ref [lcom/actionbarsherlock/app/sherlockfragmentactivity;] lcom/actionbarsherlock/app/sherlockfragmentactivity;(0x45fa69a0):0x12f910 (lcom/example/sandbox/mainactivity; had used different lcom/actionbarsherlock/app/sherlockfragmentactivity; during pre-verification) unable resolve superclass of lcom/example/sandbox/mainactivity; (824) link of class 'lcom/example/sandbox/mainactivity;' failed not find class 'com.example.sandbox.mainactivity', referenced method com.example.sandbox.test.mainactivitytest.setup vfy: unable resolve const-class 1031 (lcom/example/sandbox/mainactivity;) in lcom/example/sandbox/test/mainactivitytest; class resolved unexpected dex: lcom/example/sandbox/mainactivity;(0x45fa69a0):0x127410 ref [lcom/actionbarsherlock/app/sherlockfragmentactivity;] lcom/actionbarsherlock/app/sherlockfragmentactivity;(0x45fa69a0):0x12f910 (lcom/example/sandbox/mainactivity; had used different lcom/actionbarsherlock/app/sherlockfragmentactivity; during pre-verification) unable resolve superclass of lcom/example/sandbox/mainactivity; (824) link of class 'lcom/example/sandbox/mainactivity;' failed class resolved unexpected dex: lcom/example/sandbox/mainactivity;(0x45fa69a0):0x127410 ref [lcom/actionbarsherlock/app/sherlockfragmentactivity;] lcom/actionbarsherlock/app/sherlockfragmentactivity;(0x45fa69a0):0x12f910 (lcom/example/sandbox/mainactivity; had used different lcom/actionbarsherlock/app/sherlockfragmentactivity; during pre-verification) unable resolve superclass of lcom/example/sandbox/mainactivity; (824) link of class 'lcom/example/sandbox/mainactivity;' failed class resolved unexpected dex: lcom/example/sandbox/mainactivity;(0x45fa69a0):0x127410 ref [lcom/actionbarsherlock/app/sherlockfragmentactivity;] lcom/actionbarsherlock/app/sherlockfragmentactivity;(0x45fa69a0):0x12f910 (lcom/example/sandbox/mainactivity; had used different lcom/actionbarsherlock/app/sherlockfragmentactivity; during pre-verification) unable resolve superclass of lcom/example/sandbox/mainactivity; (824) link of class 'lcom/example/sandbox/mainactivity;' failed vfy: unable resolve virtual method 8919: lcom/example/sandbox/mainactivity;.findviewbyid (i)landroid/view/view;
what tried
i tried solutions from:
- could not find class xxx referenced method xxx.<yyy> (i tried checked in order , export tab in libs , projects, didn't work)
- could not find class x referenced method y (i tried jdk 1.6 , 1.7, nothing worked)
- importing class external jar, android
- could not find class referenced method android java.lang.noclassdeffounderror
nothing worked.
sandbox project
first of show sandbox project , associated sandbox test project work reproduce issue easily.
the sandbox project short , easy understand. there "go" button in linearlayout. no action when clicking, nothing complicated.
mainactivity.java
package com.example.sandbox; import android.app.activity; import android.os.bundle; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="go" /> </linearlayout>
androidmanifest.xml
note here there android:theme="@style/theme.sherlock" it's not used yet mainactivity extends activity , not sherlock*activity classes.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sandbox" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" /> <application android:allowbackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/theme.sherlock" > <activity android:name="com.example.sandbox.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
sandboxtest project
the sandbox test easy understand, check text of button "go" (we don't care test result).
mainactivitytest.java
package com.example.sandbox.test; import android.content.intent; import android.test.activityunittestcase; import android.widget.button; import com.example.sandbox.mainactivity; import com.example.sandbox.r; public class mainactivitytest extends activityunittestcase<mainactivity> { private mainactivity mactivity; public mainactivitytest() { super(mainactivity.class); } @override public void setup() throws exception { super.setup(); startactivity(new intent(getinstrumentation().gettargetcontext(), mainactivity.class), null, null); mactivity = getactivity(); } public void testbutton() { assertnotnull(mactivity); button b = (button) mactivity.findviewbyid(r.id.button); asserttrue(b.gettext().tostring().equalsignorecase("go")); } public void testdummy() { asserttrue(true); } }
what works
with sandbox project, when running test in sandboxtest project works expected.
let's reproduce issue
to reproduce issue, it's easy, extends mainactivity of sandbox project sherlockfragmentactivity instead of activity, this:
mainactivity.java (2nd version)
package com.example.sandbox; import android.os.bundle; import com.actionbarsherlock.app.sherlockfragmentactivity; public class mainactivity extends sherlockfragmentactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
when doing got issue mentioned @ start of topic.
i tried play way import actionbarsherlock library, nothing worked.
libraries configuration in eclipse projects
i have checked many qa not found class stuff , talk exporting libs , putting jar files in libs/ subdirectory etc. tried , nothing work. think it's related actionbarsherlock library.
nevertheless, let's see current configuration libs/ subdirectory:
sandbox project: have libs/ subdirectory android-support-v4.jar sandboxtest project: no libs/ subdirectory.
let's see configuration within eclipse each project:
(i cannot add images haven't 10 of reputation... :-/)
sandbox project (right click > properties):
android:
- project build target : android 4.4.2
- library : actionbarsherlock
java build path:
- projects tab void
- libraries tab contains android 4.4.2 (android.jar), android dependencies (actionbarsherlock.jar), android private libraries (android-support-v4.jar)
- order , export tab (in order): [-]sandbox/src, [-]sandbox/gen, [ ] android 4.4.2, [x] android private libraries, [x] android dependencies.
sandboxtest project (right click > properties):
android:
- project build target : android 4.4.2
- library : actionbarsherlock (<< this mistake remove in order work, see edit section @ end of post)
java build path:
- projects tab contains sandbox project
- libraries tab contains android 4.4.2 (android.jar), android dependencies (actionbarsherlock.jar), android private libraries (android-support-v4.jar)
- order , export tab (in order): [-]sandboxtest/src, [-]sandboxtest/gen, [ ] sandbox, [ ] android 4.4.2, [x] android private libraries, [x] android dependencies.
i hope has idea. i've lost half-day on issue...
thanks! jeremy.
edit: solution & workaround
i found main mistake: sandboxtest project must not have dependency actionbarsherlock.
i recommend use jdk 1.6.
after removing dependency, not find class error disappears. replaced one:
java.lang.illegalstateexception: must use theme.sherlock, theme.sherlock.light, theme.sherlock.light.darkactionbar, or derivative. @ com.actionbarsherlock.internal.actionbarsherlockcompat.generatelayout(actionbarsherlockcompat.java:976) @ com.actionbarsherlock.internal.actionbarsherlockcompat.installdecor(actionbarsherlockcompat.java:902) @ com.actionbarsherlock.internal.actionbarsherlockcompat.setcontentview(actionbarsherlockcompat.java:836) @ com.actionbarsherlock.app.sherlockfragmentactivity.setcontentview(sherlockfragmentactivity.java:261) @ com.example.sandbox.mainactivity.oncreate(mainactivity.java:13) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) @ android.test.activityunittestcase.startactivity(activityunittestcase.java:159) @ com.example.sandbox.test.mainactivitytest.testtext(mainactivitytest.java:18) @ java.lang.reflect.method.invokenative(native method) @ android.test.instrumentationtestcase.runmethod(instrumentationtestcase.java:204) @ android.test.instrumentationtestcase.runtest(instrumentationtestcase.java:194) @ android.test.androidtestrunner.runtest(androidtestrunner.java:169) @ android.test.androidtestrunner.runtest(androidtestrunner.java:154) @ android.test.instrumentationtestrunner.onstart(instrumentationtestrunner.java:520) @ android.app.instrumentation$instrumentationthread.run(instrumentation.java:1447)
it's typical mistake in actionbarsherlock when don't use 1 of sherlock theme in app. use theme.sherlock in sandbox project (as in real app) not in sandboxtest project.
the error message seems tell me must use theme.sherlock theme in sandboxtest project. if want that, must add dependency actionbarsherlock lead again first issue.
workaround
i found way run test correctly using contextthemewrapper in sandboxtest project. fidanov, see original message here: https://github.com/jakewharton/actionbarsherlock/issues/454#issuecomment-10472928.
mainactivitytest (workaround inside)
the new setup() method is:
@override public void setup() throws exception { super.setup(); contextthemewrapper context = new contextthemewrapper(getinstrumentation() .gettargetcontext(), r.style.herculetheme); setactivitycontext(context); startactivity(new intent(context, mainactivity.class), null, null); mactivity = getactivity(); }
now when run unit test, doesn't bother me anymore using theme.sherlock , works.
thanks, jeremy.
Comments
Post a Comment