【问题标题】:How to include internal jar files in android library如何在android库中包含内部jar文件
【发布时间】:2019-02-21 23:14:36
【问题描述】:

我正在尝试下载并运行现有的 android studio 项目,目前我收到错误:

error: package com.google.gson does not exist

需要gson的java文件在主app部分。 gson-2.8.1.jar 文件位于 libs 文件夹内的 client 库中。

所以文件结构看起来有点像:

project_root
 |-- app/
 |   |-- src/
 |       |-- main/
 |           |-- java/
 |               |-- com.company.stuff/
 |                   |-- controller
 |                       |-- fileThatUsesGson.java
 |-- client/
     |-- libs/
         |-- gson-2.8.1.jar

应用程序build.gradle 显示:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation project(path: ':client')
}

客户端build.gradle显示:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation files('libs/gson-2.8.1.jar')
}

这似乎是一个较旧的项目。我不得不将compile 更改为implementation,这似乎是问题的一部分,但我似乎无法弄清楚如何添加那些.jar 文件。

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 这基本上解决了我的问题 stackoverflow.com/a/54818270/5184092

标签: java android android-gradle-plugin gson


【解决方案1】:

扩展 Mr.AF 的回答:

在我添加的应用程序build.gradle文件中

dependencies {
    implementation files('../client/libs/gson-2.8.1.jar')
}

sync project with gradle filesmake project

原创

File ->Project Structure ->App-> dependencies -> + ->add library 

【讨论】:

  • 我这样做并修复了其他几个错误。当我尝试制作项目时,它给了我Error: Program type already present: com.google.gson.FieldNamingPolicy。这是因为 Gson 现在已经存在于应用程序中,所以当我在库中加载 gson.jar 时会引发错误?
  • @luckyging3r 如果在您的项目中 .jar 库被复制。尝试在两个 gradle 文件中引用一个现有的 .jar。
  • 所以这似乎奏效了。我不得不手动输入它。添加implementation files('../client/libs/gson-2.8.1.jar') 似乎是一种“黑客行为”。这是添加该文件的适当方式吗?
  • 是的。我之前做过。没问题,它们在同一范围内,可以使用通用 rss。
  • @luckyging3r 如果您在这里遇到任何问题,我会帮助您。
猜你喜欢
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
相关资源
最近更新 更多