Geeks With Blogs
Paul Kelly

In this blog, by external library I really mean any jar (yours or someone else's) that has some functionality you want to include in an Android app. The Android SDK makes this seem very easy to do. You can add other jars to your android project, and they get DEXed and included in your .apk file for upload to Android emulator or other device. If you are using the Eclipse ADT, this isn't any harder than putting the jars on the build path for your project, using the Java Build Path properties dialog.

But the fly in the ointment is that if your jar is a standard Java project it will have been built against the standard Java runtime libraries. Whereas your Android app once running on either the emulator or an actual device runs against the Android SDK libraries. When you create a new Android project, and specify the Android API level you want to run against, you add the android.jar for that API level to the build path for your Android application. If you look at the screenshot below, I've added an external jar to the library for my android application (the first line), and below that is a an entry for Android 2.2, which on Windows resolves to a jar in the android SDK under platforms\platform-9\android.jar.

My external jar is something built against a regular JDK set of jars. Android will build your application, and you can deploy it, and everything is OK until at runtime you call something in your 3rd party jar that has been built against a Java API which doesn't have an equivalent in android. Then your application crashes (or has "stopped unexpectedly" as Android puts it).

When you look in LogCat, you'll see messages like this:

Dalvikvm VFY: unable to resolve virtual method…

Followed by a big red stack trace and a NoClassDefFound or NoSuchMethodError exception, a bit like the one below:

The solution is to rebuild your external library against the same version of android.jar as your app uses. If you don't have and can't get the sources for the library you are using, you are out of luck at this point and will have to find some other way to get the functionality your app needs. But assuming you do have the sources, once you have created an Eclipse project for the library, right-click on the library and select Properties, then Java Build Path to get the Java Build Path dialog up:

Select the JRE (the exact version will depend on which JDKs you have installed and what you are using in Eclipse for your project defaults), and click the Remove button. Then click Add External Jars, and navigate to the android.jar for the API level you are using in your project and add it so your Libraries tab looks more like this:

At this point, if you had Dalvik run-time errors last time, you'll now have compile time errors in your library project, showing you which JVM functionality the library uses which is not available in Dalvik. You'll have to go through and fix up these errors manually – often there are equivalent methods available under Dalvik. I found that with the older Android APIs, a lot of Java VM methods to do with Strings and Charsets were unavailable, but that there were functionally equivalent methods with different signatures. Google's Android SDK Reference is very helpful here. For every package, class, interface and method there is a "Since:" entry in the top right-hand corner of the page, telling you in which API level the item first became available. Sometimes the methods you want will be included in a later API Level than the one you are targeting, which might give you a simpler solution provided that the devices you are targeting will have that later API level. Obviously, the earlier an API level you can target, the larger the number of devices able to run your application.

Once your library project builds properly, go back to your Android project, remove the reference to the original library jar, and add a reference to your Eclipse project with the library functionality. Next time you deploy the application, it should run without Dalvik verifier warnings, and without crashing because it can't find classes or methods.


Posted on Tuesday, May 17, 2011 4:17 AM Eclipse , Android | Back to top

Comments on this post: Using External Libraries with Android

# re: Using External Libraries with Android
Requesting Gravatar...
how to add the External JAR In Android Sdk Itself.
Left by Thilagaraj on Jul 12, 2011 9:57 PM

# re: Using External Libraries with Android
Requesting Gravatar...
@Thilagaraj Hi - are you asking a question or making a statement? If you are asking a question, I suspect the answer might be to put to create an android library project - see
Left by Paul Kelly on Jul 12, 2011 10:06 PM

# re: Using External Libraries with Android
Requesting Gravatar...
Thanks for your reply. But I want add the Jar file in android sdk itself. I am new android
Left by Thilagaraj on Jul 12, 2011 10:42 PM

# re: Using External Libraries with Android
Requesting Gravatar...
I've followed what you said but it still can't find myself made classes.....any suggestions?
Left by Mike on Oct 18, 2012 2:36 PM

Your comment:
 (will show your gravatar)

Copyright © cyberycon | Powered by: