java 将外部 jar 添加到 android 项目

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

Adding external jar to android project

javaandroidjarclasspath

提问by mol

I have the following issue:

我有以下问题:

I have two projects: jre1.7 and android 2.1. I'd like to build first as a jar file and use it as framework in the second project. I'm using eclipse and I've done things I usually do when I add external jar. (Right Click on Project - Build Path - Add external Jars), and seems like everything's fine, there are Referenced libraries folder appears and it contains jar.

我有两个项目:jre1.7 和 android 2.1。我想先构建为 jar 文件,然后将其用作第二个项目中的框架。我正在使用 eclipse,并且我已经完成了添加外部 jar 时通常会做的事情。(右键单击 Project - Build Path - Add external Jars),看起来一切都很好,出现了 Referenced libraries 文件夹,其中包含 jar。

But when I'm trying to compile Android project, it gives NoClassDefFound Error. Am I doing something wrong?

但是当我尝试编译 Android 项目时,它给出了 NoClassDefFound 错误。难道我做错了什么?

回答by yorkw

If you use Android SDK & ADT r17 or later, now Google recommended way of adding dependencies is:

如果您使用 Android SDK & ADT r17 或更高版本,现在 Google 推荐的添加依赖项的方式是:

  1. Create libs/ folder under project root folder.
  2. Put all dependencies jar into libs/ folder.
  1. 在项目根文件夹下创建 libs/ 文件夹。
  2. 将所有依赖项 jar 放入 libs/ 文件夹。

Android SDK now automatically pick up and resolve dependencies for you, and you don't need other manual setup anymore (add jar to project build path). Check out the r17 changelog:

Android SDK 现在会自动为您挑选和解析依赖项,您不再需要其他手动设置(将 jar 添加到项目构建路径)。查看r17 更改日志

Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)

添加了自动设置 JAR 依赖项的功能。/libs 文件夹中的任何 .jar 文件都会添加到构建配置中(类似于 Ant 构建系统的工作方式)。此外,库项目所需的 .jar 文件也会自动添加到依赖于这些库项目的项目中。(更多信息

In addition, It is not explicitly stated in official dev guide that Android now fully support java 7, it is better to stay with JDK 6 at the moment.

另外,官方开发指南中也没有明确说明Android现在完全支持java 7,目前还是继续使用JDK 6比较好。

Hope this helps.

希望这可以帮助。

回答by Shankar Agarwal

If you want to include this external jar into your Android project, then you just do the following steps in your Eclipse environment.

如果您想将此外部 jar 包含到您的 Android 项目中,那么您只需在您的 Eclipse 环境中执行以下步骤。

  1. Right click on your project in the project's panel.
  2. Select “Properties” option.
  3. In the Properties window, Click on “Java Build Path”.
  4. In the right side of that window, select “Libraries” tab.
  5. Click on “Add External JARs” button and add this library where you have saved previously.
  6. Now click on “Order and Export” tab and check on “JarName.jar”
  7. Click on “Ok”.
  1. 在项目面板中右键单击您的项目。
  2. 选择“属性”选项。
  3. 在“属性”窗口中,单击“Java 构建路径”。
  4. 在该窗口的右侧,选择“库”选项卡。
  5. 单击“添加外部 JAR”按钮并在您之前保存的位置添加此库。
  6. 现在点击“Order and Export”选项卡并检查“JarName.jar”
  7. 单击“确定”。

回答by TacB0sS

I would go and assume you use ADT 17, there is a classpath issues with Android, you can read about it here, you just go to the Export tab and check the jar.

我假设您使用 ADT 17,Android 存在类路径问题,您可以在此处阅读相关内容,只需转到“导出”选项卡并检查 jar。