在android项目中添加第三方jar包虽然不是一个很复杂的问题,但是确实给很多开发者带来了不小的困扰。我自己就曾经碰到过calss not found exception、error inflating class等本质都是第三方jar包未被真正识别但报错指向其他问题的状况,碰到这类问题时我们很容易会被这些指向不够准确的报错误导,因此正确地添加第三方jar包就显得很重要。网上关于这个问题的信息也比较杂乱,因此希望借此文章对这个问题进行明确的解释和解答。

首先要说的是为什么会出现这个问题?我们在以往开发java程序的过程中都已经习惯于通过add external archives来添加第三方的jar包,之所以不能在android项目中用同样的方法引入第三方jar包的原因,下面是来自eclipse j2ee开发者之一Russ Bateman的解释:

I'm an Eclipse JEE developer and have been in the habit for many years of adding third-party libraries via the "User Library" mechanism in Build Path. Of course, there are at least 3 ways to add a third-party library, the one I use is the most elegant, in my humble opinion.

This will not work, however, for Android, whose Dalvik "JVM" cannot handle an ordinary Java-compiled class, but must have it converted to a special format. This does not happen when you add a library in the way I'm wont to do it.

Instead, follow the (widely available) instructions for importing the third-party library, then adding it using Build Path (which makes it known to Eclipse for compilation purposes). Here is the step-by-step:

  1. Download the library to your host development system.
  2. Create a new folder, libs, in your Eclipse/Android project.
  3. Right-click libs and choose Import -> General -> File System, then Next, Browse in the filesystem to find the library's parent directory (i.e.: where you downloaded it to).
  4. Click OK, then click the directory name (not the checkbox) in the left pane, then check the relevant JAR in the right pane. This puts the library into your project (physically).
  5. Right-click on your project, choose Build Path -> Configure Build Path, then click the Libraries tab, then Add JARs..., navigate to your new JAR in the libs directory and add it. (This, incidentally, is the moment at which your new JAR is converted for use on Android.)

What you've done here accomplishes two things:

  1. Includes a Dalvik-converted JAR in your Android project.
  2. Makes Java definitions available to Eclipse in order to find the third-party classes when developing (that is, compiling) your project's source code.

    他的意思是android的dalvik虚拟机不能直接处理编译过的java .class文件,因此直接添加第三方jar包需要经过一定的处理才能被android项目正常识别并使用。以下是他提供的步骤:

  1. 下载第三方jar包
  2. 在android项目下创建一个libs目录(就我理解及测试的经验,名称并不固定,你完全可以取其他名称)
  3. 在eclipse中右键点击libs目录,依次选择Import -> General -> File System,选中jar包所在目录, 然后选中这个目录下的jar包

注:到这一步为止你就成功地把jar包添加到项目中(但是还没有被android的虚拟机识别,因此如果这时你使用jar包中的类,编译都无法通过)

   4.右键点击项目名,依次选择Build Path -> Configure Build Path,选择library选项卡。点击右边的add jars,选择libs目录下的jar包

注:这一步就帮助android虚拟机来处理之前添加的jar包

  理论上到这一步应该就可以正常使用了,但是如果到这一步你调试还会出错的话可以尝试右键点击libs目录,选择build path –> configure as source folder ,基本上到这一步就可以正常使用第三方jar包,继续你的android开发之旅了

转载于:https://www.cnblogs.com/developerY/archive/2013/04/18/3027997.html

关于如何正确地在android项目中添加第三方jar包相关推荐

  1. Android Studio 在项目中引用第三方jar包

    在Android Studio项目中引用第三方jar包的方法: 步骤: 1.在build.gradle文件中添加如下代码: 备注:要添加在Android作用域下 sourceSets {main {j ...

  2. 在maven项目中解决第三方jar包依赖的问题

    在maven项目中解决第三方jar包依赖的问题 参考文章: (1)在maven项目中解决第三方jar包依赖的问题 (2)https://www.cnblogs.com/nuccch/p/6122938 ...

  3. android 工程中引入第三方JAR包后安装APK时老是提示找不到库文件?

    网上查都说是通过添加中Build Path或者使用User library来添加,我不知道这些复杂粘贴的人测试过没,反正我测试了好久都不行,估计是我RP不行,这样添加到后编译会通过,但运行的时候就会提 ...

  4. maven 项目中引入第三方jar包的方法

    说明:这里说的第三方jar包是指maven库中没有改jar包,但是你的项目是maven项目,需要引入该jar包 1 将jar包放入项目的libs(我的路径是/Users/ma/workspace/bi ...

  5. Android安卓开发-eclipse正确添加第三方jar包

    本文转载自http://www.cnblogs.com/developerY/archive/2013/04/18/3027997.html,在此对原作者表示感谢! 在android项目中添加第三方j ...

  6. Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包

    场景 Ubuntu Server 上使用Docker Compose 部署Nexus(图文教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/ ...

  7. Maven项目中使用本地JAR包

    在Maven项目中使用本地JAR包有两种方法: 1. 使用system scope <dependencies><dependency><groupId>org.r ...

  8. 解决Android Studio加载第三方jar包,出现包重复加载的问题

    2015-02-13 0 个评论 来源:JavAndroid 收藏 我要投稿 通过Maven中央库添加第三方jar包的时候,出现了重复加载jar包的问题,解决办法很简单去掉一个不让它去加载就OK了 一 ...

  9. java工程加包_Java工程中添加依赖jar包不起作用问题总结

    Java工程中添加依赖jar包不起作用问题总结 此次总结两种方式的依赖问题 1 在Eclipse中添加依赖jar包不起作用问题 这种方式可能是Eclipse缓存,或者Eclipse还没有反应过来.例如 ...

最新文章

  1. 携程供应商接口数据解密AES/CBC/PKCS5Padding
  2. NHibernate2.1新特性之entity-name
  3. Powershell 命令行泄漏下一个 Windows 10 更新内容
  4. mysql 审计(audit)
  5. 20210903 惯性环节1/(s+1)的各种响应
  6. 我和阿里巴巴的孽缘(三)
  7. 关于手动栈 (模板)
  8. 眼擎科技CEO朱继志:如何设计自动驾驶的视觉成像系统 | 吃瓜笔记
  9. [转载] Python中的数学函数,三角函数,随机数函数
  10. linux  指令 备注
  11. 市场上的视觉图像采集卡软硬功能对比
  12. 音频信号频率测试软件,音频信号发生器软件(MyToneTest)
  13. Windows操作系统 快捷键 必备
  14. 网页游戏常见外挂原理及防御
  15. php20以内的勾股数,[求助]编程求100以内的所有勾股数
  16. python爬虫面试题集锦及答案
  17. mcldownload文件夹_download文件夹是什么?Win7系统download文件夹可以删除?
  18. [工具设置]使用NSIS制作安装包
  19. thinkadmin下拉框实现默认选中
  20. linux时间同步负数,Ubuntu 18.04 Bionic Beaver Linux上的NTP服务器配置

热门文章

  1. Java基础学习(一)—方法
  2. mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1'
  3. Js取float型小数点后两位数的方法
  4. WPF将Ui保存为图片和保存位图
  5. 移动客户端与服务器端安全通信方案
  6. JS-Object 对象的相关方法
  7. 使用ASP.NET Core开发GraphQL服务器 -- 预备知识(下)
  8. linux安装mysql5.7.19
  9. 《黑客秘笈——渗透测试实用指南》—第2章2.4节Web应用程序的扫描
  10. 系统安全运维 Server 2008 R2 事件查看器实现日志分析