Fran
Fran

Reputation: 554

Why Android Studio doesn't fully recognize GSON Library?

Its weird because its working in the implementation, but the editor is highlighting in red the last word of the import (see attached image). And underlining the file as "with errors".

On my app gradle I have:

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.code.gson:gson:2.8.6'

on the other gradle:

buildscript {
ext.kotlin_version = "1.5.10"
repositories {
    google()
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:4.2.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}}
allprojects {
repositories {
    google()
    mavenCentral()
}
}

task clean(type: Delete) {
    delete rootProject.buildDir}

Also I have errors in the code but its running.

Can anyone point me in the right direction? Thanks!

Upvotes: 1

Views: 3778

Answers (2)

Nick Pedalino
Nick Pedalino

Reputation: 31

In Android Studio:

  • File > Project Structure > Dependencies

  • + button > Library Dependency

  • Search for "Gson"

  • Select com.google.code.gson

  • Ok, Apply

  • In your java code, type Gson, then alt+enter, to get the following import:

    import com.google.gson.Gson;

Latest Gson at time of posting is 2.10, this method will give the latest Gson version.

Upvotes: 3

AShX
AShX

Reputation: 412

Try to update gson dependency to 2.8.7 :
implementation 'com.google.code.gson:gson:2.8.7'

Add jcenter to repositories in build.grade (Project:%name%)

      buildscript {
            repositories {
             //
                jcenter()
            }
....
          allprojects {
                repositories {
                  //
                    jcenter()
                }
            }

Then sync gradle. This should work.

Upvotes: 5

Related Questions