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

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

问题描述

这是关于在 Eclipse 中向 Android 项目添加第三方库 (JAR) 的两部分问题.

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

问题的第一部分是,当我尝试将第三方 JAR(库)添加到我的 Android 项目时,我首先遇到了

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

解析 XML 时出错:未绑定前缀

Error parsing XML: unbound prefix

因为我正在尝试使用该 JAR 中的一个类(并且需要以某种方式定义前缀).怎么回事?

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

其次,(在解决这个问题之后——答案在下面给出),我的应用程序不能在 Android 上运行,我通过调试器 (LogCat) 发现我尝试使用的类不存在.

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.

原因:java.lang.ClassNotFoundException:com.github.droidfu.widgets.WebImageView...

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

为什么,当我在 Eclipse 中没有得到编译或链接器错误时,模拟器上会出现这个问题吗?

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.

推荐答案

现在是缺课问题.

我是一名 Eclipse Java EE 开发人员,多年来一直养成通过 Build Path 中的用户库"机制添加第三方库的习惯.当然,添加第三方库的方法至少有3种,我个人认为最优雅的一种.

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.

然而,这对 Android 不起作用,它的 DalvikJVM"无法处理普通的 Java 编译类,但必须将其转换为特殊格式.当您以我不习惯的方式添加库时,不会发生这种情况.

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.

相反,按照(广泛可用的)说明导入第三方库,然后使用 Build Path 添加它(这使得 Eclipse 知道它用于编译目的).步骤如下:

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. 将库下载到您的主机开发系统.
  2. 创建一个新文件夹,libs,在您的 Eclipse/Android 项目.
  3. 右键单击 libs 并选择导入 -> 通用 -> 文件系统,然后下一步,浏览文件系统找到图书馆的父母目录(即:你在哪里下载到).
  4. 单击确定,然后单击中的目录名称(不是复选框)左窗格,然后检查右侧窗格中的相关 JAR.这个将库放入您的项目中(身体上).
  5. 右键单击您的项目,选择构建路径 -> 配置构建路径,然后单击库选项卡,然后添加 JARs...,导航到您的libs 目录中的新 JAR 和添加它.(顺便提一下,这是您的新 JAR 转换为在 Android 上使用的时刻.)
  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.)

注意

如果 lib 已包含在您的构建路径中,则可能不需要第 5 步.只需在添加之前先确保其存在即可.

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. 包括一个 Dalvik 转换的 JAR在您的 Android 项目中.
  2. 使 Java 定义可用到 Eclipse 以找到开发(即编译)时的第三方类您项目的源代码.

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

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