2

I'm attempting to use org.json.XML to convert an XML file into JSON in my Android app. As the included org.json classes provided by Android do not include org.json.XML, I added it as a dependency in my build.gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.volley:volley:1.1.1'
    implementation group: 'org.json', name: 'json', version: '20180813'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso- core:3.0.2'
}  

However, when I attempt to run my app it fails with the following error:

java.lang.NoSuchMethodError: No direct method <init>(Ljava/io/Reader;)V in class Lorg/json/JSONTokener; or its super classes (declaration of 'org.json.JSONTokener' appears in /system/framework/core-libart.jar)
    at org.json.XMLTokener.<init>(XMLTokener.java:57)
    at org.json.XML.toJSONObject(XML.java:516)
    at org.json.XML.toJSONObject(XML.java:548)
    at org.json.XML.toJSONObject(XML.java:472)

In addition build.gradle has the warning:

json defines classes that conflict with classes now provided by Android. Solutions includes finding newer versions or alternative libraries that do not have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.

This is the same issue as presented in this question, but the answer provided does not work as org.json.XML is not included by default with Android.

Given the above, is it possible to rewrite my build.gradle to either ignore the included org.json library or to only import org.json.XML? Or will I have to look at using another library such as https://github.com/smart-fun/XmlToJson instead?

jaherne
  • 23
  • 5

0 Answers0