131

This is a two-part question about adding a third-party library (JAR) to an Android project in Eclipse.

The first part of the question is, when I try to add a third-party JAR (library) to my Android project I first get the problem of

Error parsing XML: unbound prefix

because I'm trying to use a class from that JAR (and need the prefix somehow defined). What's going on?

Second, (after fixing that--the answer is given below), my application doesn't work on Android and I discover via the debugger (LogCat) that the class I'm attempting to consume doesn't exist.

Caused by: java.lang.ClassNotFoundException: com.github.droidfu.widgets.WebImageView...

Why, when I get no compilation or linker error in Eclipse, does it have this problem on the emulator?

These two questions are rhetorical for I'm going to answer them myself below. Other posts in this forum creep up to the problem and elsewhere there is discussion, but I feel that I can be more explicitly helpful for the next guy to come along.

Swati Garg
  • 995
  • 1
  • 10
  • 21
Russ Bateman
  • 18,333
  • 14
  • 49
  • 65

8 Answers8

316

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.
nathan_jr
  • 9,092
  • 3
  • 40
  • 55
Russ Bateman
  • 18,333
  • 14
  • 49
  • 65
  • Russ, you should probably start another separate question. This "answer" looks kind of odd sitting here. – Paul Gregoire Sep 04 '10 at 16:29
  • 3
    So, I rewrote parts of the question a little bit. I've resisted the temptation to do the easy thing (what you suggest here) because I really think both problems arise from trying to add a third-party library and I'm wagering that anyone who has the second problem will probably have had the first one too. Thanks. – Russ Bateman Sep 04 '10 at 17:24
  • I wish there had been something more explicit about this in formal Android docs (haven't stumbled upon it yet) and other answers in other forums walk up to, but never out-sight nail the problem. Note that on BlackBerry, this same problem is a huge nightmare with no one giving reliable information on how to solve it--not sure what I'll do there. – Russ Bateman Sep 04 '10 at 17:26
  • 2
    Hi @RussBateman I tried out the steps you have mentioned above and was successfully able to include the library jar in my android project and so the library classes were accessible to my project at compile time.But when I run the project, I get an error in my console: "Conversion to Dalvik format failed with error 1" .. and my project would not be launched! – Zeba Apr 12 '12 at 07:53
  • Under certain circunstances this doesn't work... I don't remember exactly what I did, deleted the libs folder, readded it... now did everything you said, add libs, import the jar, add to build path... compiler errors are gone, but still get runtime exception that the class is not found :( – User Jun 02 '12 at 20:36
  • Had this issue myself, although not with Android development. If you're doing OSGi/Plugin development, you need an additional step after all of the above. Go to your Manifest.MF > Runtime > Classpath. Add the each of that jars you have added to you build path. Don't forget to add your src/ folder, and whatever else is required at for your plugin. – Martin Oct 25 '12 at 14:04
  • Unfortunately, when adding the jars to libs/, then ant builds no longer work: `com.android.dx.util.DexException: Multiple dex files define Lmain/java/org/random/class/Dates;`. The ant `project.properties` references the library path. For a particular library, Eclipse can't import it because for some reason the source package doesn't match the source path, so Eclipse refuses to compile. – Chloe Apr 07 '13 at 18:25
20

Ensure that your 3rd party jars are in your projects "libs" folder and they will be put in the .apk when you package your application. You may see runtime errors on the device if something in the jar is not supported, but other than that I have had great success with this.

Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
5

Setting up a Library Project

A library project is a standard Android project, so you can create a new one in the same way as you would a new application project.

When you are creating the library project, you can select any application name, package, and set other fields as needed, as shown in figure 1.

Next, set the project's properties to indicate that it is a library project:

In the Package Explorer, right-click the library project and select Properties. In the Properties window, select the "Android" properties group at left and locate the Library properties at right. Select the "is Library" checkbox and click Apply. Click OK to close the Properties window. The new project is now marked as a library project. You can begin moving source code and resources into it, as described in the sections below.

koke
  • 69
  • 1
  • 2
4

If you are using the ADT version 22, you need to check the android dependencies and android private libraries in the order&Export tab in the project build path

ajaykoppisetty
  • 364
  • 4
  • 10
2

First, the problem of the missing prefix.

If you consume something in your layout file that comes from a third party, you may need to consume its prefix as well, something like "droidfu:" which occurs in several places in the XML construct below:

<com.github.droidfu.widgets.WebImageView android:id="@+id/webimage"
          android:layout_width="75dip"
          android:layout_height="75dip"
          android:background="#CCC"
          droidfu:autoLoad="true"
          droidfu:imageUrl="http://www.android.com/images/opensourceprojec.gif"
          droidfu:progressDrawable="..."
          />

This comes out of the JAR, but you'll also need to add the new "xmlns:droidfu"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:droidfu="http://github.com/droidfu/schema"
      ...>

or you get the unbound prefix error. For me, this was a failure to copy and paste all of the supplied example from the third-party library's pages.

Russ Bateman
  • 18,333
  • 14
  • 49
  • 65
1

Error parsing XML: unbound prefix

Resource '/playteddy/res' does not exist.

I got the above two errors and finally I solved it.

Right click your project -> properties -> java build path -> googleadmobadsdk (select and put it top), then you run and problem solved. It is solved my runtime error.

Community
  • 1
  • 1
sujan
  • 11
  • 1
0

Put the source in a folder outside yourt workspace. Rightclick in the project-explorer, and select "Import..."

Import the project in your workspace as an Android project. Try to build it, and make sure it is marked as a library project. Also make sure it is build with Google API support, if not you will get compile errors.

Then, in right click on your main project in the project explorer. Select properties, then select Android on the left. In the library section below, click "Add"..

The mapview-balloons library should now be available to add to your project..

Leander
  • 612
  • 7
  • 8
0

Go to build path in eclipse, then click order and export, then check the library/jar, and then click the up button to move it to the top of the list to compile it first.

Sam Adamsh
  • 3,331
  • 8
  • 32
  • 53