关于如何正确地在android项目中添加第三方jar包
在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:
- Download the library to your host development system.
- Create a new folder, libs, in your Eclipse/Android project.
- 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).
- 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).
- 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:
- Includes a Dalvik-converted JAR in your Android project.
- 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项目正常识别并使用。以下是他提供的步骤:
- 下载第三方jar包
- 在android项目下创建一个libs目录(就我理解及测试的经验,名称并不固定,你完全可以取其他名称)
- 在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项目中添加第三方jar包的更多相关文章
- Android Studio 在项目中引用第三方jar包
在Android Studio项目中引用第三方jar包的方法: 步骤: 1.在build.gradle文件中添加如下代码: 备注:要添加在Android作用域下 sourceSets { main { ...
- 在maven项目中解决第三方jar包依赖的问题
在maven项目中,对于那些在maven仓库中不存在的第三方jar,依赖解决通常有如下解决方法: 方法1:直接将jar包拷贝到项目指定目录下,然后在pom文件中指定依赖类型为system,如: < ...
- Eclipse项目中引用第三方jar包时将项目打包成jar文件的两种方式
转载自:http://www.cnblogs.com/lanxuezaipiao/p/3291641.html 方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFES ...
- Intellij IDEA在maven项目中添加外部Jar包运行
一. 问题概述 我们知道Intellij IDEA是非常好用的Java语言开发的集成环境.提供了非常多实用的功能,包括了智能代码助手.代码自动提示.代码重构.各种插件等,当然也集成了maven 正常情 ...
- java项目怎么打jar包(项目中包含第三方jar包)
1,右键选中项目,点击Export导出,找到java,选择第三个Runnable JAR file, 2,在Launch configuration中,填入程序的入口(有main方法的类), 然后 ...
- struts项目中添加的jar包
一般我们使用struts时,添加的jar如下: commons-fileupload-1.2.1.jar commons-io-1.3.2.jar freemarker-2.3.16.jar java ...
- 【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)
原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添 ...
- Nexus-在项目中使用Maven私服,Deploy到私服、上传第三方jar包、在项目中使用私服jar包
场景 Ubuntu Server 上使用Docker Compose 部署Nexus(图文教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/ ...
- Android中使用第三方jar包
步骤: 方法1:Eclipse下, 右键工程, Build path, java build path, 选择libraries 在右边的按钮中点击“Add Library” 选择“User libr ...
随机推荐
- java中的序列化流和反序列化流
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px SimSun; color: #4e9072 } 序列化流:把对象按照流一样的方式存入文本文件 ...
- 简单三段式状态机实验2-LCD12864
此实验是在“基于I2C EPPRPM(AT24C02B) + LCD12864实验”基础上,把LCD模块里的一段式状态机改成三段式,I2C EPPROM模块暂时未改出来,一步一步来吧,改完后代码下载到 ...
- mysql触发器之姓名转姓名拼音
人员表people 字段:id,peopleNumber,name,namePinyin,sex,birthday,nation. namePinyin不用填写,根据name自动生成. wordlib ...
- Entity Framework Power Tools Beta 3
http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/ PM> Install-P ...
- CodeForces 510E Fox And Dinner
网络流. 原点到偶数连边,容量为2, 奇数到汇点连边,容量为2, 偶数到与之能凑成素数的奇数连边,容量为1 如果奇数个数不等于偶数个数,输出不可能 如果原点到偶数的边不满流,输出不可能 剩下的情况有解 ...
- [算法] kmp实现
字符串查找是经典场景,也是面试中最常见的一道题. 说来惭愧,毕业3年了,才明白了kmp算法的实现,以前一直以为这类算法是基础,工作中中不会碰到[也的确没有碰到过...] 但是,对这些基本算法结构的理解 ...
- pandas 基础
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 类似于 Numpy 的核心是 ndarray,pandas 也是围绕着 Series 和 DataFrame 两个核心数据 ...
- CMU机器学习课程-简介
CMU在机器学习研究领域大名鼎鼎,Tom Mitchell 即是该学校老师.学校开设有机器学习课程.如今机器学习应用的领域越来越广泛,之前传统的<机器学习>课程,现在分成一般非机器学习领域 ...
- jsoup -- xml文档解析
jsoup -- xml文档解析 修改 https://jsoup.org/cookbook/modifying-data/set-attributes https://jsoup.org/cookb ...
- android log写入机制
这几天和华为的leader面试了下.感觉不错.关键是小女.不容易.是技术面啊.我说的不容易不是面试不容易,是说在华为写代码的小女不容易.哥走南闯北这么多年,女人代码写的好真不多. 其实在任何时候,只要 ...