eclipse 如何在我的 Android 应用程序中使用和打包 JAR 文件?

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

How to use and package a JAR file with my Android app?

androideclipsejarpackageapk

提问by Eno

I have an API in the form of a JAR that I would like to use in my Android app. Not sure if it should be part of the project in Eclipse or kept separate and added to the project properties. This JAR also needs to be packaged with the application, so how is that done for Android apps?

我有一个 JAR 形式的 API,我想在我的 Android 应用程序中使用它。不确定它是否应该是 Eclipse 中项目的一部分,还是应该分开保存并添加到项目属性中。这个JAR也需要和应用一起打包,那么Android应用是怎么做的呢?

回答by VonC

As explained by this SO question:

正如this SO question所解释的那样:

  • Your Project -> right click -> Import -> Archive File -> yourjar.jar
  • Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
  • 您的项目 -> 右键单击​​ -> 导入 -> 存档文件 -> yourjar.jar
  • 您的项目 -> 右键单击​​ -> 属性 -> Java 构建路径 -> 库 -> 添加 Jar -> yourjar.jar

You must add it as "external JAR" files, and set the "Order and Export" in your Eclipse project.

您必须将其添加为“外部 JAR”文件,并在您的 Eclipse 项目中设置“Order and Export”。

eclipseexternallibrary2.png

eclipseexternallibrary2.png



Update February 2012:

2012 年 2 月更新:

Paceriermentions in the comment having an issue (ClassNotFound) even though he did declare the library.
He had:

Pacerier在评论中提到有一个问题 (ClassNotFound),即使他确实声明了库。
他有:

class not found

未找到课程

However, the solution was simple:

然而,解决方案很简单:

Over 1 hour wasted. Man was I pissed!
Solution: make sure your JAR is compiled with 1.6 and not 1.7.
Argh

浪费了1个多小时。伙计,我生气了!
解决方案:确保您的 JAR 是用 1.6 而不是 1.7 编译的
阿格

回答by Skilly

Thanks to VonCfor pointing me in the right direction. I had the same symptoms but for a different library (I needed to add org.apache.commons.codec.binary.Base64 for an Android project). The solution that worked for me was SLIGHTLY different. Herewith the steps that I followed:

感谢VonC为我指明了正确的方向。我有相同的症状,但对于不同的库(我需要为 Android 项目添加 org.apache.commons.codec.binary.Base64)。对我有用的解决方案略有不同。以下是我遵循的步骤:

  1. Download library from http://commons.apache.org/codec/download_codec.cgi
  2. Extract commons-codec-1.6.jar from the tarball and copy to a local folder
  3. In eclipse:
    • right-click project name
    • choose "Properties" from context menu
    • on "Libraries" tab, select "Add External JARs..."
    • navigate to and select commons-codec-1.6.jar from the location you extracted it to your local drive
  4. The jar file will now appear under "referenced Libraries" in your Package Explorer and you will be able to reference the library in your code.
  1. http://commons.apache.org/codec/download_codec.cgi下载库
  2. 从 tarball 中提取 commons-codec-1.6.jar 并复制到本地文件夹
  3. 在日食中:
    • 右键单击项目名称
    • 从上下文菜单中选择“属性”
    • 在“库”选项卡上,选择“添加外部 JAR...”
    • 从您将其解压缩到本地驱动器的位置导航到并选择 commons-codec-1.6.jar
  4. jar 文件现在将出现在包资源管理器中的“引用库”下,您将能够在代码中引用该库。