0

I am creating a cross platform application in java using libgdx. I am using Json in the core project and was trying to deploy on Android when I received this error:

E/AndroidRuntime(2030): java.lang.NoSuchMethodError: org.json.JSONObject.getNames

I read around and I found out that Android has a built in Json library that uses that same package name as the Json library I added to my core project. The library I use has the getName() method and the Android library has a name() method. Is there a way for me to specify the library I want to use? Alternatively, is there a way for me to ignore the Android library?

Update: I tested accessing the JSON method in my android project using

JSONObject.getNames(TEST);

which should have given me null, but instead gave me the same error.

eBehbahani
  • 1,579
  • 5
  • 19
  • 41
  • You can control it, just check which libraries you have imported in your .java file, if it's the library you use it's ok but if it's native one I guess that's the cause and you should remove it. – M. Erfan Mowlaei Jan 07 '15 at 14:42
  • I guess what I am saying is that they both have the same package name so I think the wires are getting crossed somewhere and the app mistakenly uses androids library instead of the one I added. They both use org.json.* – eBehbahani Jan 07 '15 at 14:46
  • I know that, but as long as you import only what you need, there will be no problem and if you have done so, be sure that problem is arising from something else. – M. Erfan Mowlaei Jan 07 '15 at 15:48
  • I'm not sure what else the problem could be. The application runs fine on the desktop. – eBehbahani Jan 07 '15 at 16:05

1 Answers1

2

if you happen to use a lib, that uses the same namespace/package name as another, you are at the mercy of the classloader, which usually picks the first place, where it can find the class by name.

so the easiest way to circumvent this is either to use the same library and version as used on android or move your library in another package.

if the library in question allows it (licence etc) and is reasonable small, you might be easier off just pulling in the source files in your project and let the IDE do the refactoring magic. Also there are tools like jarjar.

There are some question on SO (Calling same Method having same packageName From Different JARs, Java, Classpath, Classloading => Multiple Versions of the same jar/project). So you might be able to circumvent this problem, but my gut feeling is, that you will end up in big mess.

Community
  • 1
  • 1
cfrick
  • 35,203
  • 6
  • 56
  • 68