4

I am making an app in Android Studio and I need to use GSON library. I have downloaded gson-2.8.2-javadoc. And then I followed this way

File->New->New Module->Import .JAR/.AAR Package->gson-2.8.2-javadoc->Finish

After that

File->Project Structure->app->Dependencies->Module Dependency->gson-2.8.2-javadoc->OK

but Android Studio did not recognize the Gson. I googled and saw the this as solution :

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

but this is not working too.

My gradle version was 4.4 and I upgraded it 4.9 , again this is not working too. Here is my build.gradle file:

buildscript {


    repositories {
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'

    }
}

allprojects {
    repositories {
        mavenCentral()
        google()
    }
}

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

By the way, I tried same ways different versions of Gson Library, it did not work as well. So, what is the reason for this issue? And What is the exact solution?

Pelin
  • 43
  • 2
  • 7

2 Answers2

1

I think you may have to had

buildscript {
    repositories {
        mavenCentral()
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

to your application build.gradle file. Then build and it will be good

adc
  • 76
  • 8
  • I am getting "error: package com.google.gson does not exist" error message. @adc – Pelin Jul 31 '18 at 08:11
  • Did you remove the one you downloaded ? – adc Jul 31 '18 at 08:15
  • You should have a look to this question, which is pretty much the same as yours https://stackoverflow.com/questions/17913704/android-gradle-cannot-find-symbol-class-gson/17913798 – adc Jul 31 '18 at 08:17
  • I didn't remove. I tried to do again what you say and build was successfully but it still doesn't recognize Gson. @adc – Pelin Jul 31 '18 at 08:18
  • I have seen the link you share, solutions written over there are not working @adc – Pelin Jul 31 '18 at 08:21
  • can you post your build.gradle files ? @Pelin – adc Jul 31 '18 at 08:30
  • I post it. @adc – Pelin Jul 31 '18 at 08:42
  • If you deleted the one you import, clean you're project then rebuild it, it should work. You can check if the package was download after the build in your External Libraries folder. Don't forget to import Gson class where you need it and you'll be able to use it ;-) !! – adc Jul 31 '18 at 09:45
1

At first, Make sure you removed gson MODULE from your Project.

Read How to delete a module in Android Studio ?

"File -> Project Structure" -> Select gson Module and then Click Red Mark Negative Sign.

FYI

You should use latest version.

implementation 'com.google.code.gson:gson:2.8.5'

Then Clean-Rebuild-Run.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198