尝试创建 Gson 对象时发生 ClassNotFoundException class

ClassNotFoundException while trying to create object of Gson class

我想要的

我想从我的库项目中生成一个 jar(这个项目在 gradle 中使用 gson)

我在做什么

我创建了一个使用 Gson 库的库模块。 Gson 库添加到库模块的 build.gradle 中。我正在使用 gradle build command.Then 生成 .aar 文件 我提取了 aar 存档并将 classes.jar 文件复制到新项目的 libs 文件夹中。当我尝试调用使用 Gson 的库 class 的方法时,它给出了运行时错误,ClassNotFoundException。

看起来 Gson class 没有与 jar 捆绑在一起。我不确定如何解决这个问题。我还尝试在库模块中使用 gson jar 而不是添加 gradle 依赖项。我不想将库发布到 Maven 存储库,我只想要 jar 文件。

要解决此问题,您可以在使用此库的项目的主模块中添加 gradle 对 GSON 的依赖。

更清楚:

Say, you built library "L" using GSON. Now you created project "X" which is using L Library.

So you just need to add gradle dependency on GSON in main module of project X.