Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).
like image 557
Peter Avatar asked Sep 02 '13 05:09

Peter


People also ask

How do I add a library project to Android Studio?

Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.

Can I use any Java library in Android?

The android developers recommendation is that you assign unique prefixes for your resource names in each android library. Java libraries cannot reference any android SDK classes such as android.

Can I develop Android app using Eclipse?

For developing the android application using eclipse IDE, you need to install the Eclipse. you can download it from this location download the Eclipse. Eclipse classic version is recommended but we are using the Eclipse IDE for JavaEE Developers.

Can I use Eclipse instead of Android studio?

Android Studio is faster than Eclipse. There is no need to add a plugin to Android Studio but if we use Eclipse then we do need to. Eclipse needs many resources to start but Android Studio does not. Android Studio is based on IntelliJ's Idea Java IDE and Eclipse uses the ADT Plugin to develop Android applications.

How do I create an Android app in Eclipse?

Open your Eclipse installation, and let’s get started! 1) Open the File menu. Select New followed by Project. 2) In the New Project wizard, select Android followed by Android Project. Click Next. 3) Enter a name for your project (in this example, we’ll use AndroidApp) and select the location where your project will be stored. Click Next.

How do I add a library to an android project?

In the Package Explorer, right-click the dependent project and select Properties. In the Properties window, select the "Android" properties group at left and locate the Library properties at right. Click Add to open the Project Selection dialog.

How do I import a library into Android Studio?

Your library should be available for your project. Click on “ Import Existing Project “. Step 2: Select the desired library and the desired module. Then click finish. Android Studio will import the library into your project and will sync Gradle files. Step 3: In the next step you need to add the imported module to your project’s dependencies.

How do I use an Android library in another app module?

To use your Android library's code in another app module, proceed as follows: Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.


1 Answers

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
like image 167
gunar Avatar answered Oct 04 '22 04:10

gunar