0

I am having issues using the JDOM2 library within Android Studio. My application compiles and runs, but as soon as a method is used that includes JDOM, I receive this error:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
     Caused by: org.jdom2.JDOMException: http://xml.org/sax/features/external-general-entities feature http://xml.org/sax/features/external-general-entities not supported for SAX driver org.apache.harmony.xml.ExpatReader
        at org.jdom2.input.SAXBuilder.internalSetFeature(SAXBuilder.java:1011)
        at org.jdom2.input.SAXBuilder.configureParser(SAXBuilder.java:984)
        at org.jdom2.input.SAXBuilder.buildEngine(SAXBuilder.java:859)
        at org.jdom2.input.SAXBuilder.getEngine(SAXBuilder.java:907)
        at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1104)

I have read through the JDOM2 GitHub page explaining how to use JDOM in Android Studio, however the error I am receiving is not explained. I do not have any external entities in my XML file, so I am unsure why it is giving me this error.

The JDOM2 method I wrote works fine in IntelliJ but is throwing this error in Android Studio. I am lost as to what to do now.

Steps I have taken:

  • Fully updated Android Studio
  • Checked and rechecked my XML files for external entities.
  • Added <uses-library android:name="org.apache.http.legacy" android:required="false" /> in an attempt to have Android Studio use a SAX Builder that supports external general entities.

Even with a very basic, tiny XML, it still crashes. (This is not what I'm using in my application, it is just a very simple example XML.)

<root>
    <color>
        <name>Blue</name>
        <hex>0000FF</hex>
    </color>
    <color>
        <name>Red</name>
        <hex>FF0000</hex>
    </color>
    <color>
        <name>Green</name>
        <hex>00FF00</hex>
    </color>
</root>

And the method I use to pull data:

public static String getHex(String colorName) throws JDOMException, IOException {
        final String filename = "src/main/assets/test.xml";
        SAXBuilder parser = new SAXBuilder();
        Document test = (Document) parser.build(new File(filename));

        Element rootNode = test.getRootElement();
        List<Element> colorList = rootNode.getChildren("color");
        String hex = "";
        for (Element element : colorList) {
            if (element.getChildText("name").equalsIgnoreCase(colorName)) {
                hex = element.getChildText("hex");
            }
        }
        return hex;
    }

0 Answers0