Android Essentials: Adding JAR Files to Android Projects

Java libraries can be packaged and distributed using the Java Archive Format (also called JAR). There are a number of Android-compatible libraries that can be leveraged within your Android applications provided they are linked properly to your project. In this quick tip, you'll learn how to include third party JAR files in your Android projects.

Whether you have componentized your own Java libraries for reuse or you're leveraging a third party API like the Google Analytics SDK for Android or the Android Support package, you'll need to include them correctly within your Android projects in order to build your applications properly.


Android Compatible JAR Files

For a JAR file to be compatible with Android, it can only reference classes available as part of Android and other classes implemented specifically in the JAR itself. See the Android Developer website SDK reference for a complete list of packages available for use with Android applications at: http://d.android.com/reference/packages.html. It's safe to say the JAR files available for download through the Android SDK Manager are Android compliant.


Step 0: Getting Started

This tutorial assumes you are using the Eclipse development environment so popular with Android developers. The steps described use the latest version available, which, at this point, is Indigo (3.7). The steps vary only slightly for previous versions of Eclipse.

This tutorial also assumes you already have an existing Android project in Eclipse that you are adding the JAR file to.


Step 1: Acquire the JAR File

First, you need to get your hands on a compatible JAR file. Third party JAR files can be downloaded from source websites while others are available for download from within the Android SDK Manager. For example, the Google Analytics SDK for Android is available both online and through the Android SDK Manager.


Step 2: Create a JAR File Directory

Next, create a /libs directory within your Android project directory and copy the JAR file to that directory.


Step 3: Add the JAR File

Adding the jar file to your project within Eclipse is easy. Simply:

  1. Click on the Project properties for your Android project.
  2. Under the Java Build Path settings, select the Libraries tab.
  3. Click the Add JARs… button and choose the jar within the /libs directory.

You can also reach the project properties by right-clicking on the Android project in the Package explorer tab.

Note: Some JAR files require certain Android permissions to run properly. For example, if the library contacts the network, it would require network permission to run. See the specific SDK documentation for details about what the library requires in terms of configuration details.


Step 4: Start Using the Library

Now that you have successfully linked the class libraries to your project, you should be able to use them just as you would the core Android SDK classes. Remember you can use Ctrl+Shift+O to organize your imports in your Java classes!

What kind of libraries might be useful? We've used Apache libraries for MIME handling. You can find libraries for many useful tasks.


Conclusion

JAR files are an easy way to organize and redistribute libraries that can be leveraged by Android applications, which are written in Java. What third party SDKs and libraries are you using with your Android apps? Let us know in the comments!


About the Authors

Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams Teach Yourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to [email protected], via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon

Tags:

Comments

Related Articles