简体   繁体   中英

How can I fix Unresolved reference: Gson?

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).

在此处输入图片说明

Using this Lib in your gradle:

dependencies{
  implementation 'com.google.code.gson:gson:2.8.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'
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM