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

后端 未结 12 2013
[愿得一人]
[愿得一人] 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: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;
      

提交回复
热议问题