2

I am developing an Android project in eclipse. I am creating a library project that is used by a main project. Main project uses an external jar file. I want to add this external jar file to my library project and just add the jar file of library project to the main project. I have added that jar file in library project but when I compile library project its size is not changed and the external jar file is not embedded in the library project.

I mean I want to combine the library and the external jar into one library that my project can link to.

How can I do that?

Thanks,

Linaina
  • 161
  • 1
  • 2
  • 8
  • possible duplicate of [Adding a library/JAR to an Eclipse Android project](http://stackoverflow.com/questions/3642928/adding-a-library-jar-to-an-eclipse-android-project) – nullpotent Jan 06 '13 at 09:16
  • @iccthedral No I know this. I want to Embed it in my own library project – Linaina Jan 06 '13 at 09:27
  • Are you looking for something like that: http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.6.3/ksoap2-android-assembly-2.6.3.pom?r=945 ? – kamaci Jan 08 '13 at 16:27

3 Answers3

3

So you want to combine the library and the external jar into one library that your project can link to? That is not possible. You cannot just combine/link two binaries (JARs) and expect them to work as one single library. Your libraries are dynamically linked and not statically, and each library is loaded in a different address space. If you want them to be as one, you need to have the source of the two libraries (your existing library and external jar) and compile them into one (that is, static linking).

Update:

Thanks Sergiu for the insight, that is quite true. The above answer was based on the precinct that a library is a binary loaded by the JRE at runtime at some memory location, and that it is formatted binary with functions/variables loaded at locations calculated by the environment - A General CS theory.

Based on your comments, I googled how to combine two JARs, and your method seems right.

Here's another SO answer: Combine two JARs

Check out the 2nd answer, it also clarifies about the Manifest.mf file.

Community
  • 1
  • 1
sanjeev mk
  • 4,276
  • 6
  • 44
  • 69
  • 4
    That is not quite true. Jar files are just `.zip` files with a different extensions, and in most cases it's enough to just unzip two jars in the same directory and re-zip it to "combine" jars. Also, there's no _linking_ in Java. – Sergiu Dumitriu Jan 06 '13 at 09:30
  • @SergiuDumitriu What should we do with file `MANIFEST.MF`? – Linaina Jan 06 '13 at 09:35
  • @SergiuDumitriu Thanks. I tested it and it worked. Is there any way in eclipse to do this without unzipping and zipping? Can eclipse do it automatically? – Linaina Jan 06 '13 at 09:47
0

You can add external jars into your project using two ways:

  1. right click on project, Go to Properties.
  2. In Java Build Path tab select Library tab and click on the Add External JARs.. button to add the external jar files into your project.
  3. Click on OK.

The other way is to copy jar file into your projects libs folder and then right click on the jar file and select Build Path>Add to Build Path.

Try out this ways , i hope it might help you.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • I have done this. But when I compile my library, I notice that it is not embedded in my jar file – Linaina Jan 06 '13 at 09:21
0

This is a common problem, It is defined as the Scope of your library. Usually the scope is "Provided" which means that the library should be included in the classpath (for javac) and not in the Jar file, But in this case you should change your library scope to "Compile" so it also gets into the Jar file.

I don't use Eclipse so I don't know which menu to look at, But it is common property, (In Apache Maven, IntelliJ, ...) So I'm pretty sure you can find it's place on Eclipse with a little search

Long story short, Change your Library Scope to "Compile"

Hossein Shahdoost
  • 1,692
  • 18
  • 32