Videos
Add following dependency to build.gradle:
implementation 'com.google.code.gson:gson:2.11.0'
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.
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.11.0'
}
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.
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).
I remembered how to do this really quickly. You have to go to File -> Project Structure. Then click the Libraries tab and the green plus. Once clicked select from Maven and then search GSON and find the correct GSON version you want to use from the menu and then GSON dependency will work.
I had the same issue.
Go to File -> Invalidate Caches/ Restart and click the same again.
Android Studio restarts and reloads all your library files.
I faced the same issue. I just added a single line as shown below in my build.gradle dependencies (without adding any jar in project structure) and it worked for me.
dependencies {
compile 'com.google.code.gson:gson:2.2.+'
compile 'com.android.support:support-v4:13.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
}
Along with above, I found few more things which are required for this to work.
Make sure you have
android:targetSdkVersion="18"in AndroidManifest.xml file.<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18" />Make sure you have
targetSdkVersion 18in build.gradle file.defaultConfig { minSdkVersion 10 targetSdkVersion 18 }Make sure you are connected to internet; so that jars will be downloaded from online central maven repository.
Adding it as a dependency in the Project Structure settings is not enough. That setting is only for the IDE. To actually build, Gradle also needs to be aware of it. You must add the .jar file to your build.gradle file like so...
dependencies {
compile files('libs/gson-2.2.4.jar')
}
In app/build.gradle:
dependencies {
implementation 'com.google.code.gson:gson:2.11.0'
}
If you are using Android Studio you can use the following workflow instead of manually changing Gradle files:
- File -> Project Structure -> Modules -> app -> Dependencies Tab
- Click '+' in the bottom left corner and select "Library dependency"
- In the search field type: "gson" and click Search
- Select "com.google.code.gson:gson:2.8.5"
