Android Studio 中集成第三方库的最佳方式 [英] The best way to integrate third party library in Android studio
问题描述
我们可以找到一些非常好的安卓开源库.我想知道将它们集成到我们自己在 Android Studio 中的项目的最佳方式是什么.以下是一些基本方法:
We can find some very good open source libraries for android. I want to know what is the best way to integrate them to our own projects in Android studio. Here are some basic methods:
- 将源代码和资源文件复制到我们自己的项目中.我们需要更改很多代码(包名,xml中的名称等)
- 如果提供了 jar 文件,我只需为我的项目创建 libs 文件夹并复制其中的 jar 文件.并在模块设置的依赖项中添加 jar 文件.但不幸的是,我收到了很多错误消息,例如Gradle:Package com.google.gson 不存在".
是否有将第三方源或 jar 文件添加到现有 android studio 项目的一般规则?谢谢
Is there a general rule to add third party source or jar files into an existing android studio project? Thanks
推荐答案
我更喜欢使用中央存储库进行依赖项管理.所以对于 gson 2.3
依赖,你应该添加到 build.gradle
文件:
I prefer to use central repository for dependencies management. So for gson 2.3
dependency you should add to build.gradle
file:
指定您要为您的依赖项使用 maven 中央存储库
Specify that you want to use maven central repository for your dependency
存储库 {jcenter()}
添加compile
依赖到gson 2.6.2
dependencies {compile 'com.google.code.gson:gson:2.6.2'}
Android Studio 以及您的 CI 服务器现在应该可以轻松构建您的项目.您还可以继续开发应用.
Android Studio as well as your CI server should easily build your project now. And you can continue app development.
我更喜欢使用中央存储库进行依赖项管理,因为:
I prefer to use central repository for dependencies management because:
- 更容易的范围管理 - 有些库仅用于测试,有些应该包含在 apk 中,有些是运行环境的一部分(如 android.jar 本身)
- 更简单的传递依赖项管理 - 收集库依赖项非常困难,如果您使用jar-with-dependencies",则在 dexing 过程中可能会出现类已添加"错误
- 更轻量的存储库和更容易的依赖升级
示例:
- Robolectric jar 应仅用于单元测试,不应成为 apk 本身的一部分
- 存储库从带有 jar 的不同文件夹中清理干净,结帐所需的时间要少得多.无需下载并用新罐子替换旧罐子
我应该注意到:
- maven 中心的库并不多,你应该努力在你的项目中以这种方式使用它们
- 在使用中央存储库方法进行 dexing 期间,您可以更轻松地获得类已添加"错误
- 您可以混合使用来自中央存储库和 lib 文件夹的依赖项,但为了简单起见,我更喜欢只使用一种方式
这篇关于Android Studio 中集成第三方库的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!