Robolectric NoClassDefFoundError with org.apache packages
We are trying to integrate robolectric in our project but have runned into some unexpected problems.
The project compiles fine when we're not using Robolectric. However, we are getting NoClassDefFoundError in the org.apache.http packages when running simple tests with Robolectric.
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: org/
apache/http/HttpRequest
开发者_JAVA百科 at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.lang.ClassLoader.defineClass(ClassLoader.java:466)
at javassist.Loader.findClass(Loader.java:379)
at
com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.findClass(Robole ctricClassLoader.java:
60)
at javassist.Loader.loadClass(Loader.java:311)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at
com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(Robole ctricClassLoader.java:
37)
at se.hsr.android.Info.updateInfoFromWebService(Info.java:57)
at se.hsr.android.Info$2.run(Info.java:99)
Caused by: java.lang.ClassNotFoundException: caught an exception while
obtaining a class file for org.apache.http.HttpRequest
at javassist.Loader.findClass(Loader.java:359)
at
com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.findClass(Robole ctricClassLoader.java:
60)
at javassist.Loader.loadClass(Loader.java:311)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at
com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(Robole ctricClassLoader.java:
37)
... 11 more
Caused by: java.lang.IllegalStateException: shouldn't be modifying
bytecode after we've started writing cache!
class=org.apache.http.HttpRequest
at
com.xtremelabs.robolectric.bytecode.AndroidTranslator.onLoad(AndroidTransla tor.java:
68)
at javassist.Loader.findClass(Loader.java:340)
... 15 more
We are using maven to build our project and we have included http-client 4.1 (because we are dependent on http-mime 4.1 from the apache library).
Since the Robolectric jar as default includes httpclient 4.0.3 we can't understand why it can't find the http classes. We assume it has something to do with AndroidTranslator trying to modify bytecode (to be compatible with the dalvik jvm?)
If someone could shed some light on this matter we would be very grateful.
I had the same problem I fixed it by added the following exclusion
<dependency>
<groupId>com.pivotallabs</groupId>
<artifactId>robolectric</artifactId>
<version>1.0-RC1</version>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
精彩评论