Add non-Android library project to Android app in Eclipse

I have three projects in the Eclipse workspace:

  • A maven-based library project that generates a .jar file. This project contains our main code.
  • Java desktop application based on maven. This application is library dependent.
  • The Android application that we just started to create. This is not yet stunned.

I want the Android application to use the library project as a dependency.

I read several answers to similar questions about adding a library file dependency to an Android application, and I tried to execute them:

Method # 1:

Project-> Properties-> Java Build Path-> Projects β†’ Add and Add Library Project.

Now I can use the classes from the library project. The compiler does not generate any warnings. However, when I run the application on the emulator, I get an error not defined by the class.

Method No. 2:

Copy the generated .jar file to the "libs" directory of the Android application.

It works. Class exception thrown. However, I cannot connect the source and therefore cannot go through the library code. In addition, every time the source changes, I will have to manually copy the .jar file.

Method 3:

Go to the Library Project Properties section. In the left pane you will see "Android". Select it and check the "IsLibrary" box.

In my case, however, there is no β€œAndroid” property in the left pane. I assume that this property is displayed only if it is a real Android library project.

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

Here is a summary of what I want to achieve:

  • The same library project that will be used in the desktop application as an Android application.
  • In an Android app, source code debugging should work.
  • If there are any changes to the source code in the library application, the Android application should automatically receive new changes (instead, I try to drag the new .jar file into the libs folder every time).
+7
java android eclipse
source share
1 answer

Method # 1 seems still available, but you need to study why you get this ClassNotFoundException - unzip the APK (since this is an archive), delete it and see why the library classes are not included.

From experience, if you work with Eclipse, I believe that you get this exception because you did not check the library as exported when you add it to an Android project - after you added it for assembly, follow these steps: Project Props β†’ Build path β†’ Order and export, here make sure that the library project is selected. There is a known issue / disappointment regarding this ClassNotFoundException, and the workaround is selecting the export tab. I believe that this article contains more or this topic.

When it comes to debugging jar code, you need to include a properties file in the libs folder that has the same name as your jar file (for example, if the jar file is mylib.jar library, you need to have a mylib.jar.properties ) This file should have an src entry pointing to the path in which the source code lives (either as a jar file or file system). Eclipse is a little dumb when you add this file to restart it. It may contain a doc entry that also indicates where javadoc lives. Here is my example:

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

All Articles