124

Can someone give me step by step guide to add the Gson library to an Android project?

I tried the JSON built-in library but that seems to be a bit tedious right now. I saw a couple of examples based on Gson, and that seems really easy.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Venky
  • 1,929
  • 3
  • 21
  • 36

6 Answers6

250

Add following dependency to build.gradle:

implementation 'com.google.code.gson:gson:2.8.7'

Or download the JAR file from Maven by clicking a release and finding the .jar file.

Replace 2.8.7 with the latest version from Maven.

Visit the GitHub repo for documentation and more.

slhck
  • 36,575
  • 28
  • 148
  • 201
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • 2
    Do i need to download the jar file,or should i just add compile 'com.XX?? – Venky May 05 '16 at 12:06
  • 1
    It didn't owrked immediatley after adding in dependencies and trying to build my app. I had restarted my android studio and tried to rebuild it. It worked :) – Jagadish Dharanikota Mar 11 '17 at 17:14
  • @SillySam: Your edit should have been a new answer since it completely rewrites this answer. – BDL Apr 18 '17 at 13:22
  • @JagadishDharanikota I noticed this as well. I think you can fix without restarting by using File -> Sync Project with Gradle Files – okhobb Oct 03 '20 at 01:52
31

Read Google-gson

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

Add the following line to your MODULE LEVEL build.gradle configuration:

dependencies {
     implementation 'com.google.code.gson:gson:2.8.8' // Old 2.8.6
}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
7

Use gradle dependencies to get the Gson in your project. Your application build.gradle should look like this-

dependencies {
  implementation 'com.google.code.gson:gson:2.8.2'
}
Kumar Saurabh
  • 2,297
  • 5
  • 29
  • 43
D_Alpha
  • 4,039
  • 22
  • 36
4

If you are going to use it with Retrofit library, I suggest you to use Square's gson library as:

implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
Ercan
  • 2,601
  • 22
  • 23
1

Gradle:

dependencies {
   implementation 'com.google.code.gson:gson:2.8.5'
}

Maven:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.5</version> 
</dependency>

Gson jar downloads are available from Maven Central.

Jatin Sahgal
  • 119
  • 6
1

There is no need of adding JAR to your project by yourself, just add dependency in build.gradle (Module lavel). ALSO always try to use the upgraded version, as of now is

dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

As every incremental version has some bugs fixes or up-gradations as mentioned here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Muahmmad Tayyib
  • 689
  • 11
  • 28