如何使用来自 google 的 gson 库

How to use the gson library from google

我正在尝试在 android studio 中使用 google gson 库,但我无法导入它。我已经将 .jar 文件添加到 libs 文件夹中,并且我添加了

compile fileTree(dir: 'libs', include: ['*.jar'])

到我的应用 build.gradle 依赖项。当我右键单击 .jar 文件时,没有添加库的选项。

那我需要写打包声明吗?还是进口?

我是 java 的新手,非常感谢您的帮助。提前致谢!

您应该在 build.gradle 中添加以下依赖项:

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

然后同步项目,你将包含gson!

这里是 gson 的 repo:https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5

在您的应用中 gradle 添加此

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.google.code.gson:gson:2.3'
}