How can I fix Unresolved reference: Gson?
Asked Answered
I

4

8

I'm trying to follow a tutorial about Android application. I'm using an dependency Fuel (which has a dependency to com.google.Gson deserializer). But Gson() is not imported by IDE.

I've tried to specify lower version of gson. I've re-synchronized all project gradle. I've tried to write import manually (import com.google.gson.Gson), but I can't use Gson() constructor. I've read manual about using Gson, but nothing seem to be changed. It's always the same way. Call constructor Gson() and after all static method... Gson().fromJson(....)

Here is section in my build.gradle (module:app)

    // Fuel HTTP Client
    implementation 'com.github.kittinunf.fuel:fuel:2.2.0'
    implementation 'com.github.kittinunf.fuel:fuel-android:2.2.0'
    implementation 'com.github.kittinunf.fuel:fuel-gson:2.2.0'

and In code, I'm using in ArticleDataProvider.kt:

class WikipediaDataDeserializer : ResponseDeserializable<WikiResults> {

        override fun deserialize(reader: Reader): WikiResults? {
            return Gson().fromJson(reader, WikiResults::class.java)
        }
}

Normally, I would to have Gson() recognised by IDE and I wound be able to call .fromJson() normally. Gradle was downloaded properly. (I don't have any message error about).

enter image description here

Isiahisiahi answered 17/8, 2019 at 17:3 Comment(0)
S
12

Using this Lib in your gradle:

dependencies{
  implementation 'com.google.code.gson:gson:2.8.2'
}
Strobila answered 19/8, 2019 at 13:46 Comment(1)
Had to still Invalidate Caches and restart At the time of this comment gson version is com.google.code.gson:gson:2.10.1Glyn
I
2

The problem is probably in dependency of fuel-gson:2.2.0

To bypass it, I added a new dependency to my build.gradle manually and problem is solved.

dependencies {
    implementation 'com.google.code.gson:gson:2.8.5'
}
Isiahisiahi answered 17/8, 2019 at 17:14 Comment(1)
While that cleared up the unresolved reference issue, Android Studio is highlighting the dependency and saying there is a newer version - 2.10 - that I was using when I got the unresolved reference. How is 2.10 newer than 2.8.5?Pulvinus
W
1

Its may happen due to different versions of gson in External Libraries. To resolve it I have added following resolveStrategy in app module build.gradle.

configurations.all {
    resolutionStrategy.preferProjectModules()
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.google.code.gson') {
            details.useVersion "2.8.5"
        }
    }
}
Wieldy answered 22/6, 2021 at 7:56 Comment(0)
F
0

add to your build .graddle(:app) implementation("com.squareup.retrofit2:converter-gson:latest.version")

Florist answered 9/6, 2024 at 22:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.