android studio cannot resolve import org.json.JSONObject

Markoh picture Markoh · Feb 18, 2015 · Viewed 31.7k times · Source

I used libgdx to build my project and I am having issues using the JSONObject class. When I add import org.json.JSONObject, it says it cannot resolve. How do I add that library to my project?

Here is what I have tried without success:

  1. I downloaded the json-simple-1.1.jar and put it in core/build/libs folder. Could not use import.org.json.simple.JSONObject either.
  2. When browsing the tree in Android Studio as "Packages" dropdown, I can see the classes I want under "Core -> Libraries -> org -> json" but I cannot add them to my project. I get a "cannot refractor, class is in a jar file" error.

Is there something I have not tried yet to solve this issue? I feel I will run into this again as I try to use other external libraries.

Thank you.

Answer

Jared Burrows picture Jared Burrows · Feb 19, 2015

If you are using LibGdx, it does not come with a Gradle setup, making multi-project builds streamlined.

You need to add implementation 'org.json:json:<version>' to the dependencies block of your top-level build.gradle.

To find the latest version, see http://mvnrepository.com/artifact/org.json/json/

For example, using the latest version at the time of writing:

dependencies {
   implementation 'org.json:json:20200518'
}