在Android工作室中集成第三方库的最佳方式 [英] The best way to integrate third party library in Android studio

查看:164
本文介绍了在Android工作室中集成第三方库的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以找到一些非常好的开源库android。我想知道什么是最好的方式来将它们集成到我们自己的项目在Android工作室。以下是一些基本方法:




  • 将源代码和资源文件复制到我们自己的项目中。我们需要更改很多代码(包名称和xml中的名称等)

  • 如果提供了jar文件,我只需为我的项目创建libs文件夹,然后复制jar文件内。并在模块设置的依赖关系中添加jar文件。但不幸的是,我收到了很多错误消息,如Gradle:com.google.gson不存在。



一个一般的规则添加第三方源或jar文件到现有的Android工作室项目?感谢

解决方案

我更喜欢使用中央存储库来管理依赖。因此对于 gson 2.3 依赖关系,您应该添加到gradle文件中:




  • 指定要为您的依赖关系使用maven中央存储库



    repositories {jcenter()}


  • 添加 gson 2.6.2 p>

    dependencies {compile'c​​om.google.code.gson:gson:2.6.2'}

    / li>


Android Studio以及CI服务器应该可以轻松地构建项目。



我更喜欢使用中央存储库来管理依赖关系,因为:




  • 更容易的范围管理 - 一些库只用于测试,一些应该包括到apk,一些是运行环境的一部分(如android.jar本身)

  • 如果你使用jar-with-dependencies,你可能会在dexing期间得到class already added的错误。

  • / li>


示例:




  • Robolectric仅用于单元测试,不应该是apk本身的一部分

  • 存储库对于jars的不同文件夹是干净的,checkout要少得多。无需使用新的jars下载和替换旧jars



我应该注意到:




  • 很少有图书馆在maven中心,你应该努力在项目中使用它们。

  • 已添加错误。

  • 您可以混合使用中央存储库和lib文件夹中的依赖关系,但我更喜欢使用一种简单方法。


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:

  • Copy the source code and resource files into our own project. We need to change a lot of codes (the package name, and the name in xml,etc)
  • If jar files is provided, I just create libs folder for my project and copy the jar files inside. And add the jar file in Module setting's dependencies. But unfortunately I got a lot of error messages like "Gradle: Package com.google.gson doesn't exist".

Is there a general rule to add third party source or jar files into an existing android studio project? Thanks

解决方案

I prefer to use central repository for dependencies management. So for gson 2.3 dependency you should add to gradle file:

  • Specify that you want to use maven central repository for your dependency

    repositories {jcenter()}

  • Add compile dependency to gson 2.6.2

    dependencies {compile 'com.google.code.gson:gson:2.6.2'}

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:

  • easier scope management - some libraries are only for testing, some should be included to apk and some are part of running environment (like android.jar itself)
  • easier transitive dependencies management - it is quite hard to collect libraries dependencies and if you use "jar-with-dependencies" you could get error "class already added" during dexing
  • lighter repository and easier dependency upgrade

Examples:

  • Robolectric jar should be used for unit testing only and shouldn't be part of apk itself
  • Repository is clean from different folders with jars, checkout takes much less. No needs to download and replace old jars with new jars

I should notice:

  • Not many libraries are in maven central and you should make some effort to use them such way in your project
  • You could much easier get to "class already added" error during dexing with central repository approach
  • You can mix usage of dependencies from central repository and from lib folder, but I prefer to use only one way for simplicity

这篇关于在Android工作室中集成第三方库的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆