Why can't android find com.google.gson.Gson

后端 未结 12 2012
[愿得一人]
[愿得一人] 2021-02-03 23:12

I\'m trying to use GSON in my project, but my application is crashing, with logcat saying that com.google.gson.Gson cannot be found. I\'ve put import com.google.gson.Gson on my

相关标签:
12条回答
  • 2021-02-04 00:03

    I had similar problem, but I guess my requirement was more complicated as the GSON libarary was used by an Android Library Project that my main Android project depended on. I have tried both approaches above but none of them worked for me. If I add the GSON jar file directly to the main Android project it works, but that is not what I wanted.

    With a bit of investigation I have realised that the GSON jar does not use any dependencies (third party libraries) itself. So my solution was to copy the source file of the GSON project to my Android Library project's src filder and it worked like a charm. so you can use the Source code rather then the binary code. The source code is part of the download in a jar file, just unzip it.

    0 讨论(0)
  • 2021-02-04 00:04

    I faced the same thing.But the solution was quite easy,just Right-Click on your project->Properies->Libraries->Add external jars->OK and thats it.It solved my problem.hope it will solve yours as well

    0 讨论(0)
  • 2021-02-04 00:10

    I have observed today that it doesn't like when you add your library. The solution that worked for me was to add it as an external jar only. I have observed it by doing the steps described by author of this post i.e. Properties -> Java Build Path -> Libraries -> Add External JARs and point to the downloaded gson library.

    0 讨论(0)
  • 2021-02-04 00:11

    I ran into the same issue (when installed SDK 17) ...

    The solution is, that you just put the pure jar files into the "libs" folder (without subfolders). You also don't need to declare them, android wil find them itself. You may need to do a Project > Clean

    0 讨论(0)
  • 2021-02-04 00:11

    i'm sure you have copied the jar into "libs", and also added property> Java Build Path >(tab) Libraries, right?

    my issue was stupid... please check other than Gson jar, if you have two stuffs at least in above tab:

    1. android x.x
    2. Android Dependencies (especially this one, i missed this, eclipse show me an error of "could not find class com.google.gson.xx" )

    simply do a project copy will solve this problem...

    0 讨论(0)
  • 2021-02-04 00:14

    I'm using IntelliJ, not eclipse but here is how I did it:

    1. Download the source files from https://google-gson.googlecode.com/files/google-gson-2.2.4-release.zip

    2. Unpack them

    3. Copy them into the "libs" folder of your project. I did this manually by using the terminal but you can do it by going in your project folder located in your computer. Your Project_Name >app >Libs [paste them here]

    4. Go to your editor (IntelliJ in my case) You should see the the following 3 files in the libs folder of the project structure:

      • gson-2.2.4.jar

      • gson-2.2.4-javadoc.jar

      • gson-2.2.4-sources.jar

    5. Open your build.gradle file and insert the following line in the dependencies section:
      compile files('libs/gson-2.2.4.jar', 'libs/gson-2.2.4-javadoc.jar', 'libs/gson-2.2.4-javadoc.jar')

    6. Right click on each of the source files and select "Add as Library..."

    7. Use the following options:

      • Name: name of the jar file

      • Level: Project Library

      • Add to Module: select project your adding to

    8. Rebuild project Build > Rebuild project

    9. Import and use it!

      import com.google.gson.Gson;
      
    0 讨论(0)
提交回复
热议问题