本文转载自http://www.cnblogs.com/developerY/archive/2013/04/18/3027997.html,在此对原作者表示感谢!

在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开发之旅了

Android安卓开发-eclipse正确添加第三方jar包相关推荐

  1. 关于如何正确地在android项目中添加第三方jar包

                  在android项目中添加第三方jar包虽然不是一个很复杂的问题,但是确实给很多开发者带来了不小的困扰.我自己就曾经碰到过calss not found exception ...

  2. docker添加jar包_Maven系列教材 (七)- 如何添加第三方jar包

    Maven系列教材 (七)- 如何添加第三方jar包 步骤1:Maven项目用到第三方jar步骤2:修改App步骤3:给maven项目添加jar步骤4:保存再执行步骤5:重建本地仓库 步骤 1 : M ...

  3. Nexus私服仓库快速添加第三方jar包

    首先需要安装Nexus私服仓库,安装过程 参考如下地址: https://blog.csdn.net/liulihui1988/article/details/72730974 安装完后登录,不多说废 ...

  4. Eclipse如何导入第三方jar包

    本文转自:http://blog.csdn.net/mazhaojuan/article/details/21403717 我们在用Eclipse开发程序的时候,经常要用到第三方jar包.引入jar包 ...

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

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

  6. android安卓开发-eclipse平台下错误记录

    sdk文件夹下的tools里的文件莫名消失,我用sdk manager移除几个sdk包后tools里的文件就只剩libs了:我把sdk文件夹下的temp文件夹删除,tools里的文件也跟着消失只剩li ...

  7. android源码编译jar,在android源码编译中导入第三方jar包

    源码编译,我们可以只留下app的src,res目录和androidmanifest.xml文件,在app的根目录添加一个android.mk文件,还有我们需要导入的jar包,我这里是libjsoup- ...

  8. android 混淆过滤,android studio 代码混淆如何忽略第三方jar包

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 最近在打包混淆时,遇到一个问题,混淆编译都不出错,运行出错,一般遇到这种问题,肯定是在运行的地方有代码被混淆了,经过错误 ...

  9. springboot 添加第三方jar包

    1 添加jar包,右键add as liberary 2 手动引入依赖 <dependency><groupId>cas</groupId><artifact ...

最新文章

  1. 《Greenplum企业应用实战》一2.3 畅游Greenplum
  2. android ffmegp for_FFmpeg 编译for Android
  3. 关于Python编程的一些问答
  4. caffe学习(二):利用mnist数据集训练并进行手写数字识别(windows)
  5. [云炬Mysql数据库笔记] 第2章 数据库设计
  6. android项目出现红色大感叹号的问题解决方法
  7. [css] 如何自定义radio按钮的样式
  8. python连接postgresql数据库
  9. uniCloud免费云存储图床源码
  10. python面试题之Python支持什么数据类型?
  11. 《深入浅出DPDK》读书笔记(十二):DPDK虚拟化技术篇(半虚拟化Virtio)
  12. Beyond Compare 4常用配置
  13. Dubbo介绍前篇------单一应用框架、垂直应用框架、分布式应用框架、流动计算框架,及RPC的简介
  14. Centos6.5 安装配置 Apache +mysql +php 操作文档
  15. Ubuntu 印象笔记网页剪切插件无法登录问题的一种解决方案
  16. 合宙Air724UG二次开发(2):资料描述
  17. 在北京工作5年的程序员,通过人才引进入职县城事业编,月薪曝光
  18. gopl 底层编程(unsafe包)
  19. Shell编程扩展正则表达式(egrep、awk)
  20. vivo图像算法工程师双非研究生可以吗_双非材料专业小硕秋招春招找工作历程,希望对大家有帮助,劝退警告!!!...

热门文章

  1. PAT甲级1043 Is It a Binary Search Tree :[C++题解]判断二叉搜索树BST、给定前序序列和中序序列
  2. centos中卸载php,在Centos 7上完成PHP的卸载和重新安装
  3. canal mysql多实例_canal搭建实例
  4. linux mysql 5.6.22_linux下MySQL5.6.2安装过程
  5. 谷歌查看html地址_跑谷歌购物广告前,你需要了解这些
  6. python解密md5值_Python之POST提交解密MD5
  7. 未来新一代计算机的发展方向,未来计算机的发展方向 (2)
  8. wp转shp_【收藏】空间数据格式转换方法
  9. PercentFrameLayout(百分比布局)的基本使用
  10. c++ 类中静态变量 static