Adding a Library/Jar to an Eclipse Android Project

Adding a library/JAR to an Eclipse Android project

Now for the missing class problem.

I'm an Eclipse Java EE developer and have been in the habit for many years of adding third-party libraries via the "User Library" mechanism in Build Path. Of course, there are at least 3 ways to add a third-party library, the one I use is the most elegant, in my humble opinion.

This will not work, however, for Android, whose Dalvik "JVM" cannot handle an ordinary Java-compiled class, but must have it converted to a special format. This does not happen when you add a library in the way I'm wont to do it.

Instead, follow the (widely available) instructions for importing the third-party library, then adding it using Build Path (which makes it known to Eclipse for compilation purposes). Here is the step-by-step:

  1. Download the library to your host
    development system.
  2. Create a new folder, libs, in
    your Eclipse/Android project.
  3. Right-click libs and choose
    Import -> General -> File System,
    then Next, Browse in the filesystem
    to find the library's parent
    directory (i.e.: where you
    downloaded it to).
  4. Click OK, then click the
    directory name (not the checkbox) in
    the left pane, then check the
    relevant JAR in the right pane. This
    puts the library into your project
    (physically).
  5. Right-click on your project,
    choose Build Path -> Configure Build
    Path, then click the Libraries tab,
    then Add JARs..., navigate to your
    new JAR in the libs directory and
    add it. (This, incidentally, is the moment at which your new JAR is converted for use on Android.)

NOTE

Step 5 may not be needed, if the lib is already included in your build path. Just ensure that its existence first before adding it.

What you've done here accomplishes two things:

  1. Includes a Dalvik-converted JAR
    in your Android project.
  2. Makes Java definitions available
    to Eclipse in order to find the
    third-party classes when developing (that is, compiling)
    your project's source code.

how to include jar files in my eclipse project

  1. add the jar to your libs folder in your project.

  2. open eclipse go to package explorer

  3. right click on your project and select project properties

  4. click on java Build path in the bar on the left hand side of the popup

  5. select the libraries tab

  6. select the "Add External Jar File" and navigate to your JAR file

best of luck =)

Create jar for android project in eclipse

In Eclipse, select project, right click on the project, Select Export, From that select Jar. Then follow the simple wizard and give the destination where you want to save your jar, and finish. You can find your jar there.
Sample Image

Eclipse IDE- Add jar? Add External jar? Add Library?

What is an External JAR?

  • External as the name suggest is a Jar file that is not built by the currently developing code. It is a utility Jar file that is added externally for extra features. It could be an API Jar file provided from the external site, just as in your case the 3 Jar files will be going to call External Jar Files.
  • To add an External Jar file in Eclipse, follow these steps: Right Click on Project -> Select Build Path -> Select Configure Build Path -> Library Tab in the following Dialog box.

    Sample Image

What is a Library?

A Library is a in-built Jar file from the JDK Directory or any other software e.g. J2ME-Polish as you can see in the Image above. Normally these Library files are developed by a manufacturer.

What's the difference between adding JAR, Adding External JAR, and Adding Library?

  • Adding Jar File: When you have more than one project in Eclipse and you add another project's Jar file, that is called adding Jar File. There is a first Button in the Photo above, it only allows you to add Jar file from the other Project.
  • Adding External Jar: When you add any API Jar file provided by the site or another manufacturer. In your case the 3 Jar files are External Jar Files. You can add then by using second Button "Add External JARs".
  • Adding A Library: Clicking on the 4th Button will open a dialog box as below.

    Sample Image

    You can add different libraries installed on your system. You can also add your own Library, by selecting "User Library".

Is it possible to embed (combine) an external jar file to my library project in eclipse?

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.

Adding a JAR to an Eclipse Java library

In Eclipse Ganymede (3.4.0):

  1. Select the library and click "Edit" (left side of the window)
  2. Click "User Libraries"
  3. Select the library again and click "Add JARs"


Related Topics



Leave a reply



Submit