添加库/ JAR到Eclipse Android项目 [英] Adding a library/JAR to an Eclipse Android project

查看:138
本文介绍了添加库/ JAR到Eclipse Android项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是增加一个第三方库(JAR)在Eclipse中的Andr​​oid项目由两部分组成的问题。

这个问题的第一部分是,当我尝试第三方JAR(库)添加到我的Andr​​oid项目我第一次拿到的问题

  

错误的解析XML:未绑定preFIX

因为我试图使用从该JAR类(并且需要preFIX某种程度上定义)。这是怎么回事?

二,(修复之后 - 答案在下面给出),我的应用程序不能在Android上工作,我通过调试器(LogCat中),我正在试图消耗类不存在发现<。 / P>

  

引起的:   抛出java.lang.ClassNotFoundException:   com.github.droidfu.widgets.WebImageView ...

为什么,当我在Eclipse中得到任何编译或链接错误,它有这个问题,在模拟器上?

这两个问题是修辞的我会在下面回答他们自己。在这个论坛的其他帖子攀升的问题和其他地方有讨论,但我觉得我可以更加明确地有助于未来的家伙一起走。

解决方案

现在的丢失类的问题。

我是一个Eclipse的Java EE开发人员,并已在多年养成的习惯,通过在构建路径用户库机制添加第三方库。当然,至少有3种方式来添加第三方库,一个我用的是最优雅的,在我的愚见。

这将不会工作,但是,对于Android,它的DalvikJVM无法处理普通的Java编译的类,但必须把它转换成一个特殊格式。当你在,我不会做的方式添加库,这不会发生。

相反,按照(普及),说明导入第三方库,然后使用构建路径(这使得它已知的Eclipse编译目的)将它添加。这里是一步一步的:

  1. 下载库到您的主机 开发系统。
  2. 创建一个新的文件夹,的的,在 您的Eclipse / Android项目。
  3. 右键单击的,并选择 导入 - >常规 - >文件系统, 然后接着,在文件系统浏览 找到库的父 目录(即:在那里你 它下载到)。
  4. 单击确定,然后单击 在目录名(而不是复选框) 在左窗格中,然后检查 相关JAR在右窗格中。本 把库到项目 (物理)。
  5. 在您的项目用鼠标右键单击, 选择构建路径 - >配置构建 路径,然后单击Libraries选项卡, 然后添加JAR文件...,浏览到您的 在的目录中新的JAR和 添加它。 (顺便提一句,就是在这新的JAR文件转换为Android上使用的那一刻。)

请注意

步骤5可能并不需要,如果lib中已经包含在您的构建路径。只要确保它的存在之前,首先将它添加。

什么,你在这里所做的完成两件事情:

  1. 包括一个Dalvik的转换的JAR 在你的Andr​​oid项目。
  2. 使Java定义可用 为了寻找到Eclipse 第三方类时,开发(即编译) 项目的源$ C ​​$ C。

This is a two-part question about adding a third-party library (JAR) to an Android project in Eclipse.

The first part of the question is, when I try to add a third-party JAR (library) to my Android project I first get the problem of

Error parsing XML: unbound prefix

because I'm trying to use a class from that JAR (and need the prefix somehow defined). What's going on?

Second, (after fixing that--the answer is given below), my application doesn't work on Android and I discover via the debugger (LogCat) that the class I'm attempting to consume doesn't exist.

Caused by: java.lang.ClassNotFoundException: com.github.droidfu.widgets.WebImageView...

Why, when I get no compilation or linker error in Eclipse, does it have this problem on the emulator?

These two questions are rhetorical for I'm going to answer them myself below. Other posts in this forum creep up to the problem and elsewhere there is discussion, but I feel that I can be more explicitly helpful for the next guy to come along.

解决方案

Now for the missing class problem.

I'm an Eclipse Java EE 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.)

NOTE

Step 5 may not be needed, if the lib is already included in your build path. Just ensure that its existence first before adding it.

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.

这篇关于添加库/ JAR到Eclipse Android项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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