How can I fix Unresolved reference: Gson?

11k Views Asked by At

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

3

There are 3 best solutions below

1
On BEST ANSWER

Using this Lib in your gradle:

dependencies{
  implementation 'com.google.code.gson:gson:2.8.2'
}
0
On

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"
        }
    }
}
1
On

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