6

I'm using Robolectric and trying to test parsing a XML response. I'm getting the following exception:

java.lang.NoClassDefFoundError: java/lang/AutoCloseable
    at android.util.Xml.parse(Xml.java:80)
    at com.test.app.network.parser.soap.BaseSoapParser.doParse(BaseSoapParser.java:57)
    at com.test.app.network.parser.soap.RecipeParser.parse(RecipeParser.java:57)
    at com.test.app.network.command.RecipeCommand.searchRecipes(RecipeCommand.java:64)
    at com.test.app.data.PrepopulateDB.prepopulateDB(PrepopulateDB.java:53)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:230)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:172)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: java.lang.AutoCloseable
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader.loadClass(AsmInstrumentingClassLoader.java:100)
    at android.util.Xml.$$robo$$Xml_e8aa_parse(Xml.java:80)
    at android.util.Xml.parse(Xml.java)
    at com.test.app.network.parser.soap.BaseSoapParser.doParse(BaseSoapParser.java:57)
    at com.test.test.app.network.parser.soap.RecipeParser.parse(RecipeParser.java:57)
    at com.test.app.network.command.RecipeCommand.searchRecipes(RecipeCommand.java:64)
    at com.test.app.data.PrepopulateDB.prepopulateDB(PrepopulateDB.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    ... 22 more

The thing is that "AutoCloseable" was only added in Java 7 and since Android uses Java 6 I can understand why the class isn't found - any help will be appreciated.

Libraries:

robolectric-2.3-20140204.032758-112-jar-with-dependencies.jar

android-all-4.4_r1-robolectric-0.jar / android-all-4.3_r2-robolectric-0.jar

Update: I tested this using JDK 6 and Target API 18 with the same result.

Update 2: It seems that the source of the problem is this class: org.apache.harmony.xml.ExpatReader it is using AutoClosable which isn't available on Java 6 or Android API 18.

Community
  • 1
  • 1
Raanan
  • 4,777
  • 27
  • 47

3 Answers3

0

Actually this interface is in the Android SDK : https://developer.android.com/reference/java/lang/AutoCloseable.html

Added in API level 19

Before level 19, it was there but "hidden".

Guillaume Serre
  • 307
  • 2
  • 8
0

This error happens when you compile your project with a higher API version than some of your required classes, your target is API 18, but as Guillaume sais, when you use AutoCloseable you're trying to load a class which is not added until API 19. So your application crashes at runtime. You need to modify your minSDK version of your project or consider an alternative.

Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
  • This isn't an application, the app works perfectly when compiled with API level 18 and 19. This is a test case using Robolectric which fails at this point. As I wrote in an update this also happens when I run the test with JDK 1.6 and API 18 - so Robolectric is introducing some files that aren't compatible - this is probably the issue I agree, I'm still looking for a solution. – Raanan Mar 24 '14 at 16:57
  • 1
    Can you check this answer about Robolectric emulateSdk feature? http://stackoverflow.com/a/20848052/1668144 maybe it's helpful for you – Guillermo Merino Mar 24 '14 at 16:59
  • Thanks for your help. This doesn't work I think it's a Robolectric bug in the classes imported for xml parsing in the API level 18 version. (they aren't compatible with the API level as far as I can tell.) IMO This will only be solved by either Robolectric supporting API level 19 which it doesn't or fixing those files, I opened an issue on Robolectric. – Raanan Mar 24 '14 at 17:13
  • https://github.com/robolectric/robolectric/issues/810 it seems that Roboelectric doesn't support API 19, also emulateSdk only works with API levels 16 , 17 and 18. Sorry for suggesting that in the previous commentary. IMO your best choice is to use a different tester. – Guillermo Merino Mar 24 '14 at 17:35
  • Trying to convince the backend to switch this function to JSON ;) also a way to solve my specific problem (not this issue though). – Raanan Mar 24 '14 at 18:11
-1

Download the SDK bundle [1], extract it and then update/overwrite your Eclipse and Android SDK (just make a backup copy of eclipse and android-sdk folder beforehand). Of course afterwards make all the necessary updates within the SDK and Eclipse IDE

[1] https://developer.android.com/sdk/index.html

Eric
  • 1,685
  • 1
  • 17
  • 32
  • Just a thought. Had similar problem with my own project and after updating to the newest SDK/Eclipse it went away. Worth a try – Eric Mar 21 '14 at 13:58
  • I'm a great believer in voodoo but it didn't help. :( – Raanan Mar 24 '14 at 13:33
  • Hmm that's a shame, are you sure you're using the latest updates adt jdk etc? Check the following link for more information: http://stackoverflow.com/questions/20480090/do-android-apps-support-jdk-6-or-7-java-code – Eric Mar 24 '14 at 20:03