将非Android库项目添加到Eclipse中的Android应用程序 [英] Add a non-Android library project to an Android app in Eclipse

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

问题描述

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


  1. 一个基于maven的库项目,生成一个.jar文件。该项目包含我们的核心代码。

  2. 基于maven的Java桌面应用程序。这个应用程序取决于图书馆项目。

  3. 我们刚刚开始构建的Android应用程序。我不想让Android应用程序使用库项目作为依赖。


$ b

我已经阅读了一些关于将图书馆文件依赖关系添加到Android应用程序的类似问题的回复,我试图关注它们:



方法#1:



项目 - >属性 - > Java构建路径 - >项目 - >添加和添加库项目。 >

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



方法#2:



将生成的.jar文件复制到Android应用程序的libs目录中。



这个工作。没有类没有定义的异常。但是,我不能附加源代码,因此无法通过库代码。此外,每次源更改时,我将不得不手动复制.jar文件。



方法#3:



转到图书馆项目的属性。您将在左窗格中看到Android。选择它并标记IsLibrary字段。



然而,在我的情况下,左窗格中没有Android属性。我想这个属性只有当它是一个真正的Android图书馆项目时才显示。



我被卡住了。如果有人能告诉我如何最好地处理这种情况,我将不胜感激。



以下是我希望达成的内容:


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

  2. 在Android应用程序,源代码调试应该可以工作。

  3. 如果库应用程序中有任何源代码更改,Android应用程序应自动接收新的更改(而不是我尝试拖动新的.jar文件进入libs文件夹每次)。


解决方案

方法#1似乎很可能要选择,但是您需要研究为什么要获得ClassNotFoundException - 解压缩APK(因为它是一个存档),而不是它,并且看看为什么不包括库类。



从经验来看,如果您正在使用Eclipse,我相信您会收到该异常,因为您在添加时没有将库检查为导出到Android项目 - 在您将其添加到构建路径之后,请执行以下操作:Project Props - >构建路径 - >订单和导出,这里确保您的库项目已选中。有一个已知的关于这个ClassNotFoundException的问题/挫败感,解决方法是选择导出选项卡。我相信这篇文章包含更多详细信息这个SO主题



当调查jar代码时,您需要在libs文件夹中包含与您的jar文件具有相同命名的属性文件(例如,如果库jar文件为 mylib.jar ,则需要具有有一个 mylib.jar.properties 文件)。该文件应该有一个 src 条目,指向源代码所在的路径(作为jar文件或文件系统)。当您添加此文件时,Eclipse有点愚蠢,因此您需要重新启动它。它可以包含一个 doc 条目,并指向javadoc所在的位置。这是我自己的一个例子:

  src = .. / docs / spring-android-rest-template-1.0.1。 RELEASE-sources.jar 
doc = .. / docs / spring-android-rest-template-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中的Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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