简体   繁体   中英

Add a non-Android library project to an Android app in Eclipse

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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