Gson library in Android Studio

AndroidGson

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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
}

Solution 3 - Android

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'
}

Solution 4 - Android

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'

Solution 5 - Android

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.

Solution 6 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVenkyView Question on Stackoverflow
Solution 1 - AndroidPEHLAJView Answer on Stackoverflow
Solution 2 - AndroidIntelliJ AmiyaView Answer on Stackoverflow
Solution 3 - AndroidD_AlphaView Answer on Stackoverflow
Solution 4 - AndroidErcanView Answer on Stackoverflow
Solution 5 - AndroidJatin SahgalView Answer on Stackoverflow
Solution 6 - AndroidMuahmmad TayyibView Answer on Stackoverflow