11

Several Android projects use a Android library project. Now I added a JAR (commons-lang.jar) to this library project. I want to export this commons-lang.jar to all projects that use the library project. I can't get it to work without duplicating the jar to all referencing projects.

What I did so far:

  1. Created a folder named libs within the library projects folder hierarchy.
  2. Copied the JAR to this libs folder.
  3. Added this JAR in the Eclipse "Java Build Path" with "Add external JARs..." to the library project.
  4. On the "Order and Export" tab selected the checkbox to the left of the JAR to export it.
  5. On the referencing project on the "Android" page "Add" library project.

It seems that the commans-lang JAR is not exported with the library project. I need to create the libs folder on the referencing project as well, add the same JARs to the build path of the referencing project. After that everything works but the APK does contain that external JAR twice.

What's wrong with my approach? What should I do in addition?

Many thanks in advance.

Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85
  • Did you ever find a better solution? This is also driving me mad. – Graham Borland Sep 08 '11 at 22:14
  • No. I think I drop Android libraries if they are used in a library tree. For example: lib1 is used in lib2, lib2 is used in proj1 and lib1 is used in proj1 as well --> double size!!! I can see that for jars as well. For example: commons-io used in lib1, commons-io used in lib2, commons-io used in proj1. Three times the size if lib1 and lib2 are used in proj1. – Harald Wilhelm Sep 09 '11 at 10:01

4 Answers4

1

I tried the same thing using gson.jar file.

  • I put a copy of gson jar to the TestAndroidLib project and set the Java Build Path to include it.
  • I added a reference to this TestAndroidLib project to the TestAndroidApp.
  • I didn't need to duplicate the gson jar in the app project to use Gson class.
  • I could run and debug the app project.

What I found in the project properties of the app project is that the gson jar is added as part of Android Dependencies as shown below. You may want to check your app project's properties->Java Build Path->Libraries->Android dependencies to see if the jar you want to use is visible there.

It seems that if you have jar files in a Andoroid lib project with build path set properly, no extra build path setting is required in an app project that uses the lib project.

IdleSun
  • 251
  • 3
  • 5
  • The problem is that the JAR, added to the Android Library project, is included in the target Android project twice. Last week I had the same situation with a new project. Added a JAR to an Android Library project, added Library project to target project and boom - APK size increased by two times the JAR file size. Plain Eclipse here, latest SDK, no fancy Maven, no additional (build) tools. – Harald Wilhelm Jun 02 '12 at 05:27
1

What i would do:

Start from scratch (remove all project references and jar references that you created because of this problem). Create a new Android library project. Put the jar in the libs folder of this project. Let all projects for which you need the jar, reference the library project. This should work.

stoilkov
  • 1,686
  • 1
  • 11
  • 18
1

I believe that what you do is go to Eclipse > preferences > Java > Build Path > User Libraries. Then when you add a jar file to that I believe that all of the projects in the workspace automatically have that jar in the classpath.

Aidan
  • 185
  • 3
  • 10
  • Doesn't work here. As I said it's an Android library - no JAR file. I do have projects that contain the same library three times now and bump the APKs to unbelievable sizes. – Harald Wilhelm Aug 16 '11 at 10:22
  • This worked for me (not sure why it makes sense but it works!) – kellyfj Feb 21 '14 at 16:19
0

I'm curious on what is actually happening when you 'Order and Export'. It sounds like you are introducing the jar twice because the external reference you are adding with 'Add external jars - this is modifying your .classpath file at the root of the project) and what ever build tool being used is automatically assuming the lib folder you have added as well.

The answer by Aidan is probably what you need.

There are a few other ways you could attach this common-lang jar as well.

If this is code you control and plan to change, it's probably better to open a separate project for this code and add the commons project as a 'Required project' on the build path. You can add the project at the Java Build Path - Projects tab. This will put the dependency on the current project you are working and allow for instant changes to the commons code.

I see the comment below about the Android libs. I wouldn't think eclipse would only allow jars for a user lib. I remember a few years ago setting up something for some dlls for a few Swing apps using Aidan's suggestion.

For the little Android development I've done, i've established a maven/android template to get my projects started. If you aren't using Maven, i'm sure there are some other plugins that will take care of your Android library dependencies automatically so you don't have to worry about it.

Vehemon
  • 41
  • 1
  • 4