将库/JAR 添加到 Eclipse Android 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3642928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:07:21  来源:igfitidea点击:

Adding a library/JAR to an Eclipse Android project

androideclipsejarandroid-library

提问by Russ Bateman

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

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

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

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

Error parsing XML: unbound prefix

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

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

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

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.

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

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

引起: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?

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

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.

这两个问题是修辞性的,因为我将在下面自己回答。这个论坛上的其他帖子都在讨论这个问题,其他地方也有讨论,但我觉得我可以更明确地帮助下一个出现的人。

回答by Russ Bateman

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.

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

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.

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

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:

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

  1. Download the library to your host development system.
  2. Create a new folder, libs, in your Eclipse/Android project.
  3. Right-click libsand 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 libsdirectory and add it. (This, incidentally, is the moment at which your new JAR is converted for use on Android.)
  1. 将库下载到您的主机开发系统。
  2. 在 Eclipse/Android 项目中创建一个新文件夹libs
  3. 右键单击libs并选择 Import -> General -> File System,然后在文件系统中浏览以找到库的父目录(即:您将其下载到的位置)。
  4. 单击确定,然后单击左窗格中的目录名称(而不是复选框),然后在右窗格中选中相关的 JAR。这会将库放入您的项目中(物理上)。
  5. 右键单击您的项目,选择 Build Path -> Configure Build Path,然后单击 Libraries 选项卡,然后单击 Add JARs...,导航到libs目录中的新 JAR并添加它。(顺便提一下,这是您的新 JAR 转换为在 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.

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

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

回答by Paul Gregtheitroade

Ensure that your 3rd party jars are in your projects "libs" folder and they will be put in the .apk when you package your application. You may see runtime errors on the device if something in the jar is not supported, but other than that I have had great success with this.

确保您的第 3 方 jar 位于您的项目“libs”文件夹中,并且在您打包应用程序时它们将被放入 .apk 中。如果 jar 中的某些内容不受支持,您可能会在设备上看到运行时错误,但除此之外,我在这方面取得了巨大成功。

回答by koke

Setting up a Library Project

建立图书馆项目

A library project is a standard Android project, so you can create a new one in the same way as you would a new application project.

库项目是标准的 Android 项目,因此您可以像创建新应用程序项目一样创建一个新项目。

When you are creating the library project, you can select any application name, package, and set other fields as needed, as shown in figure 1.

在创建库项目时,您可以根据需要选择任意应用名称、包和设置其他字段,如图1所示。

Next, set the project's properties to indicate that it is a library project:

接下来,设置项目的属性以表明它是一个库项目:

In the Package Explorer, right-click the library project and select Properties. In the Properties window, select the "Android" properties group at left and locate the Library properties at right. Select the "is Library" checkbox and click Apply. Click OK to close the Properties window. The new project is now marked as a library project. You can begin moving source code and resources into it, as described in the sections below.

在包资源管理器中,右键单击库项目并选择属性。在“属性”窗口中,选择左侧的“Android”属性组并找到右侧的库属性。选择“是库”复选框并单击应用。单击“确定”关闭“属性”窗口。新项目现在被标记为库项目。您可以开始将源代码和资源移入其中,如以下部分所述。

回答by ajaykoppisetty

If you are using the ADT version 22, you need to check the android dependencies and android private libraries in the order&Export tab in the project build path

如果您使用的是ADT 22版本,则需要在项目构建路径的order&Export选项卡中查看android依赖项和android私有库

回答by Russ Bateman

First, the problem of the missing prefix.

首先,缺少前缀的问题。

If you consume something in your layout file that comes from a third party, you may need to consume its prefix as well, something like "droidfu:" which occurs in several places in the XML construct below:

如果您在布局文件中使用来自第三方的某些内容,则可能还需要使用其前缀,例如“droidfu:”,它出现在以下 XML 构造中的多个位置:

<com.github.droidfu.widgets.WebImageView android:id="@+id/webimage"
          android:layout_width="75dip"
          android:layout_height="75dip"
          android:background="#CCC"
          droidfu:autoLoad="true"
          droidfu:imageUrl="http://www.android.com/images/opensourceprojec.gif"
          droidfu:progressDrawable="..."
          />

This comes out of the JAR, but you'll also need to add the new "xmlns:droidfu"

这来自 JAR,但您还需要添加新的“xmlns:droidfu”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:droidfu="http://github.com/droidfu/schema"
      ...>

or you get the unbound prefix error. For me, this was a failure to copy and paste all of the supplied example from the third-party library's pages.

或者你得到未绑定的前缀错误。对我来说,这是无法从第三方库的页面复制和粘贴所有提供的示例。

回答by sujan

Error parsing XML: unbound prefix

Resource '/playteddy/res' does not exist.

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

资源“/playteddy/res”不存在。

I got the above two errors and finally I solved it.

我得到了上述两个错误,最后我解决了。

Right click your project -> properties -> java build path -> googleadmobadsdk (select and put it top), then you run and problem solved. It is solved my runtime error.

右键单击您的项目-> 属性-> java 构建路径-> googleadmobadsdk(选择并将其放在顶部),然后运行并解决问题。它解决了我的运行时错误。

回答by Leander

Put the source in a folder outside yourt workspace. Rightclick in the project-explorer, and select "Import..."

将源文件放在您工作区之外的文件夹中。右键单击项目资源管理器,然后选择“导入...”

Import the project in your workspace as an Android project. Try to build it, and make sure it is marked as a library project. Also make sure it is build with Google API support, if not you will get compile errors.

将工作区中的项目作为 Android 项目导入。尝试构建它,并确保将其标记为库项目。还要确保它是使用 Google API 支持构建的,否则会出现编译错误。

Then, in right click on your main project in the project explorer. Select properties, then select Android on the left. In the library section below, click "Add"..

然后,在项目资源管理器中右键单击您的主项目。选择属性,然后选择左侧的 Android。在下面的库部分中,单击“添加”。

The mapview-balloons library should now be available to add to your project..

mapview-balloons 库现在应该可以添加到您的项目中了。

回答by Sam Adamsh

Go to build path in eclipse, then click order and export, then check the library/jar, and then click the up button to move it to the top of the list to compile it first.

进入eclipse中的build path,然后点order和export,然后勾选library/jar,然后点up键移动到列表最上面先编译。