一个非Android库项目添​​加到Eclipse中的Andr​​oid应用 [英] Add a non-Android library project to an Android app in Eclipse

查看:114
本文介绍了一个非Android库项目添​​加到Eclipse中的Andr​​oid应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个项目在我的Eclipse工作区:


  1. 生成一个.jar文件基于Maven的库项目。这个项目包含了我们的核心code。

  2. 系统基于Maven的Java桌面应用程序。这个程序是依赖于库项目。

  3. 我们刚刚开始建立一个Android应用程序。目前尚不Maven化。

我想在Android应用程序使用该库项目的依赖。

我看过类似问题的几个答复有关添加库文件依赖性一个Android应用程序,我试图跟着他们:

方法1:

项目 - >属性 - > Java构建Path->项目 - >添加,并添加库项目

现在,我可以使用的类从库项目。编译器不产生任何警告。然而,当我运行模拟器上的应用程序,我得到一个类没有定义的错误。

方法2:

生成的.jar文件复制到库的Andr​​oid应用程序的目录。

这工作。有没有类没有定义的异常。但是,我不能附加的来源,因此通过图书馆code不能一步。此外,每次源的变化,我将不得不手动复制.jar文件了。

方法3:

去图书馆项目的性质。您将在左窗格中看到的Android。选择它,并注明IsLibrary字段。

在我的情况,但是,没有在左窗格中没有Android的属性。我想这属性显示,只有当它是一个真正的Andr​​oid库项目。

我卡住了。我想AP preciate,如果有人能告诉我如何与这种情况最好的交易。

下面是我所希望达到的一个总结:


  1. 要在桌面应用程序中使用相同的库项目,以及
    作为Android应用程序。

  2. 在Android的应用程序,源$ C ​​$ C调试应该工作。

  3. 如果有在图书馆的应用任何源$ C ​​$ c修改,Android的应用程序会自动拿起新变化(而不是我试图将新的.jar文件拖动到库,每次文件夹)。


解决方案

方法1,似乎到目前为止去的选择,但你需要去研究为什么你得到了ClassNotFoundException的 - 解压APK(因为它是一个压缩文件),其水下爆炸,看看为什么不包括在库类。

从以往的经验,如果你正在使用Eclipse的工作,我相信你得到的异常,因为你没有检查库,当你将它添加到Android项目出口 - 您添加它建立后,路径,请执行下列操作:项目道具 - >构建路径 - >在线订单和出口,在这里请确保您的库项目被选中。有关于这ClassNotFoundException的和一个已知问题/挫折解决办法是选择导出选项卡。我相信这篇文章包含更多详情或的这个话题SO

当谈到调试罐子code,你需要在库,包括文件夹具有相同的命名为您的jar文件的属性文件(例如,如果库jar文件, mylib.jar ,你需要有一个有 mylib.jar.properties 文件)。该文件应该有一个的src 条目指向的路径所在的源$ C ​​$ C生活(无论是作为JAR文件或文件系统)。 Eclipse是一个有点傻,所以你需要重新启动它,当你加入这个文件。它可以包含一个商务部项,以及指向哪里javadoc的生活。下面是我自己的例子:

  SRC = .. /文档/弹簧Android的休息-模板1.0.1.RELEASE-sources.jar
DOC = .. /文档/弹簧Android的休息-模板1.0.1.RELEASE-javadoc.jar

I have three projects in my Eclipse workspace:

  1. A maven-based library project that generates a .jar file. This project contains our core code.
  2. A maven-based Java desktop application. This app is dependent upon the library project.
  3. An Android application that we just started to build. It is not yet mavenized.

I would like the Android application to use the library project as the dependency.

I have read a few responses to similar questions about adding library file dependency to an Android application and I tried to follow them:

Method #1:

Project->Properties->Java Build Path->Projects-->Add and add the library project.

Now, I can use the classes from the library project. Compiler does not produce any warning. However, when I run the application on the emulator, I get a class-not-defined error.

Method #2:

Copy the generated .jar file into "libs" directory of the Android app.

This works. There is no class-not-defined exception. However, I cannot attach the source and therefore cannot step through the library code. Plus, each time the source changes, I will have to manually copy the .jar file over.

Method #3:

Go to Properties of the library project. You will see "Android" in the left pane. Select it and mark "IsLibrary" field.

In my case, however, there is no "Android" property in the left pane. I guess this property is shown only if it is a genuine Android library project.

I am stuck. I would appreciate it if someone can tell me how to best deal with this situation.

Here is a summary of what I wish to achieve:

  1. The same library project to be used in a desktop application as well as the Android application.
  2. On the Android application, source code debugging should work.
  3. If there is any source code change in the library application, the Android application should automatically pick up the new changes (instead of me trying to drag the new .jar file into the libs folder each time).

解决方案

Method#1 seems so far the option to go for, but you need to study why you're getting that ClassNotFoundException - unpack the APK (since it's an archive), undex it and see why the library classes are not included.

From experience, if you're working with Eclipse, I believe you're getting that exception because you didn't check the library as exported when you're adding it to Android project - after you've added it to build path, do the following: Project Props -> Build Path -> Order and Export, here make sure your library project is selected. There is a known issue/frustration about this ClassNotFoundException and the work-around is to select the export tab. I believe this article contains more details or this SO topic.

When it comes to debugging the jar code, you need to include in the libs folder a properties file that has the same naming as your jar file (ex. if the library jar file is mylib.jar, you need to have there a mylib.jar.properties file). That file should have a src entry pointing to the path where the source code lives (either as jar file or file system). Eclipse is a bit stupid when you're adding this file so you need to restart it. It can contain a doc entry as well pointing to where the javadoc lives. Here's an example of my own:

src=../docs/spring-android-rest-template-1.0.1.RELEASE-sources.jar
doc=../docs/spring-android-rest-template-1.0.1.RELEASE-javadoc.jar

这篇关于一个非Android库项目添​​加到Eclipse中的Andr​​oid应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆