简体   繁体   English

如何在Eclipse中的Android库项目中引用外部jar

[英]How to reference an external jar in an Android Library project in Eclipse

Oh Android. 哦,Android。 How I love your verbiage. 我多么喜欢你的措辞。

I have a workspace with a few projects in it. 我有一个工作区,里面有几个项目。 App1 and App2 are Android applications. App1和App2是Android应用程序。 Common is an Android library project. Common是一个Android库项目。 App1 and App2 depend upon Common (linked via the Android tab). App1和App2依赖于Common(通过Android选项卡链接)。

Common has some external dependencies, namely httpmime & apache-mime4j, which exist as jar files. Common有一些外部依赖项,即httpmime和apache-mime4j,它们以jar文件的形式存在。

For some reason, it appears that I need to add my mime jars to the build path of App1 and App2 for compilation to succeed. 出于某种原因,我似乎需要将我的mime jar添加到App1和App2的构建路径中,以便编译成功。 This seems really dumb. 这看起来真的很蠢。 In normal Java, I would add Common to the build path of App1 and App2 and things would work. 在普通的Java中,我会将Common添加到App1和App2的构建路径中,并且事情会起作用。 Is this expected that I have to add my jars to every Android application? 我是否应该将我的罐子添加到每个Android应用程序中?

-Andy -Andy

Note: If I don't configure the build path as described above, I get "The type org.apache.james.mime4j.message.SingleBody cannot be resolved. It is indirectly referenced from required .class files | DataCallUtil.java | /App1/Common/util | line 364" 注意:如果我没有如上所述配置构建路径,我得到“类型org.apache.james.mime4j.message.SingleBody无法解析。它是从所需的.class文件间接引用的| DataCallUtil.java | / App1 / Common / util | line 364“

I think omermuhammed and Amit didn't get the fact that Visser is talking about a Android Library Project. 我认为omermuhammed和Amit没有得到Visser正在谈论Android库项目的事实。 For those project, I don't think it is possible to create a jar. 对于那些项目,我认为不可能创建一个jar。 ( jar has nothing to do with all the Android resources thing ). (jar与所有Android资源无关)。

From my experience with Android Library Project, this kind of project are just, basically, the sources and the ressources packaged, and ready to be included in another project. 根据我对Android Library Project的经验,这类项目基本上是源和资源打包,并准备包含在另一个项目中。 But the settings are not part of the package, so you have to include the libs for each application. 但是这些设置不是包的一部分,因此您必须为每个应用程序包含lib。 In my experience, this setting is not something that changes often, so it is not so bad. 根据我的经验,这种设置不是经常变化的,所以它并不是那么糟糕。

Android Library Project are still way from being perfect, but still a huge improvement from what was there before ( ie nothing ). Android库项目仍然不是完美的,但仍然是以前的巨大改进(即没有)。

To augment omermuhammed's reply, if the common project is not one that is being changed frequently, creating a jar and using it in the other projects is a good solution. 为了增加omermuhammed的回复,如果常见项目不是经常更改的项目,那么创建一个jar并在其他项目中使用它是一个很好的解决方案。

To create a jar right-click the project on Eclipse -> Export -> Java -> JAR file, then select the folders you want to include in the JAR, in your case I guess this includes the folders gen and libs (libs being the folder with the httpmime & apache-mime4j JARS), but probably neither res nor the root directory of your project (with files such as AndroidManifest.xml that will cause problems with the same file in the dependent projects). 要创建一个jar,右键单击Eclipse上的项目 - >导出 - > Java - > JAR文件,然后选择要包含在JAR中的文件夹,在你的情况下我想这包括文件夹genlibs (libs是使用httpmime和apache-mime4j JARS的文件夹,但可能既不是res也不是项目的根目录(使用AndroidManifest.xml之类的文件会导致依赖项目中的同一文件出现问题)。

尝试将Common项目编译到jar中,并将其作为外部jar添加到App1或App2项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM