6

I am trying to import GSON into my project using gradle in android studio. I have used instructions found on google'sGitHub and several stack overflow pages, such as here. However when I go to import into my file using the line

import com.google.gson.Gson;

I still get the following error

Error:(5, 23) error: package com.google.gson does not exist

example from my app's build.gradle

compile 'com.google.code.gson:gson:2.7'

and my settings.gradle

include 'gson:2.7'

Am I missing something?

Leah Busch
  • 111
  • 1
  • 1
  • 6

2 Answers2

10

Open your project. Go to File -> Project Structure... Click on app in the Modules section and switch to the Dependencies tab. Click on the plus icon in the top right corner and select Library dependency. Select the entry for the GSON library from the list and hit OK twice. (Note: the search bar didn't work for me)

Then you are able to use the library just like you described in your question:
import com.google.gson.Gson;

Note that after adding the library your project will do a gradle sync which may take a couple of minutes.

jemand771
  • 457
  • 1
  • 6
  • 17
  • Mine doesn't appear in the Module list for com.google.* query... – Michael Paccione Jun 19 '21 at 05:26
  • 1
    @MichaelPaccione try selecting main folder inside your project folder. also try to click on new library button next to "add selected" and add com.google.code.gson:gson:2.8.7 as the path of the library to add – mrtechmaker Jul 15 '21 at 14:57
  • @jemand771 why doesnt it work by simply adding it to build.gradle – mrtechmaker Jul 15 '21 at 14:58
  • @KaranAhuja I haven't worked with android studio and/or gradle in a while, so this is just a guess: Sounds like you need to install the library beofre you can import/use it. Though as I've said, I don't really know anymore – jemand771 Jul 16 '21 at 06:34
  • @jemand771 sure cool. I think if i add it to build.gradle , the library is auto installed , barring this one. i do not know reason for why gson is not installed just by adding it as dependency in build.gradle. for now i have been able to fix it based on the answers here - adding it manually in idea. – mrtechmaker Aug 03 '21 at 11:13
0

The dependency should be imported properly after syncing your project. Try selecting Sync Project with Gradle Files in the upper right corner of Android Studio (referring to the default layout of Bumblebee).

codingmechanic
  • 275
  • 4
  • 7