Java 将外部 .jar 文件导入 Android 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2694392/
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-13 10:54:45  来源:igfitidea点击:

Importing external .jar file to Android project

javaandroidjar

提问by robintw

I have a standard Java project in which I've written code to parse some BBC Radio XML data from the internet. The project is called BBCSchedules and the class I'm interested in is called BBCChannel.

我有一个标准的 Java 项目,我在其中编写了代码来解析来自 Internet 的一些 BBC Radio XML 数据。该项目称为 BBCSchedules,我感兴趣的课程称为 BBCChannel。

I'm now trying to use the BBCChannel class in an Android application. How should I go about doing this?

我现在正在尝试在 Android 应用程序中使用 BBCChannel 类。我该怎么做呢?

I've tried various things, following various bits of advice on the internet, and the place I've got to at the moment is compiling my BBCSchedules project to a .jar file, and importing that to the Android project using the Build Path/Library/Add External Jar option. However, Eclipse still doesn't recognise the BBCChannel class, and won't let me run the application because of this.

我尝试了各种方法,遵循互联网上的各种建议,目前我要做的就是将我的 BBCSchedules 项目编译为 .jar 文件,然后使用 Build Path/ 将其导入到 Android 项目中库/添加外部 Jar 选项。但是,Eclipse 仍然无法识别 BBCChannel 类,因此不会让我运行该应用程序。

I guess I've done something silly wrong, but what is it?

我想我做了一些愚蠢的错误,但这是什么?

UPDATE: I've tried the steps listed in How can I use external JARs in an Android project?and various other StackOverflow questions I can find that seem to be related to this, but absolutely nothing seems to work. Any other ideas?

更新:我已经尝试了如何在 Android 项目中使用外部 JAR 中列出的步骤以及我发现的其他各种 StackOverflow 问题似乎与此有关,但似乎没有任何效果。还有其他想法吗?

UPDATE: The discussions I've had with the author of the first answer below suggest that it is something to do with how I am using Eclipse to attach the .jar file. The project he sent me with the .jar file already attached didn't work. Any other ideas anyone?

更新:我与下面第一个答案的作者进行的讨论表明,这与我如何使用 Eclipse 附加 .jar 文件有关。他发送给我的带有 .jar 文件的项目不起作用。任何人的任何其他想法?

回答by CommonsWare

Put the JAR in your project's libs/directory. Then Build Path -> Library -> Add JAR should allow you to pick the one out of your project. IIRC, this works with Eclipse.

将 JAR 放在您的项目libs/目录中。然后 Build Path -> Library -> Add JAR 应该允许您从项目中选择一个。IIRC,这适用于 Eclipse。

If you decide someday to dump Eclipse, just having your JAR in libs/is enough -- the command-line build tools will pick it up automatically.

如果您决定某天转储 Eclipse,只需放入 JARlibs/就足够了——命令行构建工具会自动获取它。

UPDATE: If you have the R17 or newer version of the ADT Eclipse plugin, now you only need to add a libs/directory and put your JAR in there. It will automatically be added to your build path, much like with command-line builds.

更新:如果你有 R17 或更新版本的 ADT Eclipse 插件,现在你只需要添加一个libs/目录并将你的 JAR 放在那里。它将自动添加到您的构建路径中,就像命令行构建一样。

回答by cleroo

Here is what I did, and it worked :

这是我所做的,并且有效:

  • Right-click on your project
  • Build Path > Add External Archives
  • Select your archive (.jar)
  • 右键单击您的项目
  • 构建路径 > 添加外部档案
  • 选择您的存档 (.jar)

The library.jar will be added to the 'Referenced Libraries' in your project, but not in the folder of your project. I can now run my project with that external library, and I don't get errors, but I don't know if it is the usual way to do it. My archive is not in the 'libs' folder, as everyone. You should try what I did.

library.jar 将添加到项目中的“引用库”中,但不会添加到项目的文件夹中。我现在可以使用该外部库运行我的项目,并且没有出现错误,但我不知道这是否是通常的做法。和大家一样,我的存档不在“libs”文件夹中。你应该试试我做的。

回答by Kumar Shorav

Check the version of jar and also your system Java version. If jar version is compatible to the system version it will run.

检查 jar 的版本以及您的系统 Java 版本。如果 jar 版本与系统版本兼容,它将运行。

Moreover, check the Jar by running java -jar filename.jaron the command prompt.

此外,通过java -jar filename.jar在命令提示符下运行来检查 Jar 。

After that do the same thing as mentioned in the above version.

之后做与上面版本中提到的相同的事情。

If not please pass your jar and sample code.

如果没有,请传递您的 jar 和示例代码。