http://www.developer.com/ws/android/development-tools/creating-android-libraries-in-eclipse.html

Back to article

Eclipse Android Tutorial: Working with Libraries


May 8, 2012

JAR files provide developers with a handy way of distributing Java class files, associated metadata and resources between projects on the Java platform, and Android is no exception. There are plenty of third party Android-compliant libraries that are packaged and distributed via the JAR format, which can add valuable extra functionality to your Android project. However, to leverage these JAR files, you must first add them as a Referenced Library within Eclipse.

In this tutorial, I'll show you how to add a JAR file to an existing Android project in an Eclipse installation, before showing you how to create your own Android-compliant libraries, for easily sharing resources between projects.

This tutorial uses the following software -- the good news is that it's all free to download and use:

Download and Add Your JAR File

The first step is to decide which JAR file you want to add to your installation. In this example we'll be adding the Google Analytics Android SDK.

Download your JAR file of choice and unzip it.

To add your JAR file to Eclipse, take the following steps

  1. Create a new folder within Eclipse by right-clicking on your project, and selecting New followed by Folder. The library folder is traditionally called lib (for library) which is what I'll be using throughout this tutorial.

    Eclipse Android libraries

  2. Find the JAR folder you unzipped earlier. Drag and drop it into the new lib folder, and when prompted select Copy files.

    Eclipse Android libraries

  3. Your JAR file will now appear in the lib folder in Eclipse's Package Explorer.

    Eclipse Android libraries

Link Your JAR File to Eclipse

Although your JAR file is now displayed in the Package Explorer, you still need to link your JAR file to Eclipse before you can access its functionality.

  1. Start by selecting the Project tab at the top of the screen, and click Properties.

    Eclipse Android libraries

  2. Select Java Build Path followed by the Libraries tab.

    Eclipse Android libraries

  3. Click the Add JARs… button and select your JAR file from within the lib folder.

    Eclipse Android libraries

  4. Your JAR file will now appear in both the lib and Referenced Libraries folders. You can explore the JAR's resources by clicking Referenced Libraries.

You have now successfully added and linked a JAR file to your Android project!

Eclipse Android libraries

Note: In some instances, you need to add specific permissions before you can begin leveraging the JAR file. For example, the Google Analytics Android SDK requires the following permissions to be added to the AndroidManifest.xml file:

  • {uses-permission android:name="android.permission.INTERNET" /}
  • {uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /}

Make sure you read the JAR file's accompanying documentation, to ensure you have the correct permissions set.

Creating Android-Compliant Libraries

You have now successfully added a JAR file, but what if you wanted to create your own library, to share data across several Android projects? In this section, I'll show you how to create an Android Library from an existing Android project, and then leverage that library in a different project. After you have added the library, you will be able to use all of its classes, just as you would the standard Android SDK classes.

  1. Select the project you wish to share. Open the Project menu and select Properties.

    Eclipse Android libraries

  2. Select the Android option, and tick the Is Library box.

    Eclipse Android libraries

  3. Now you have marked this project as a library, you can easily reference it from other Android projects. Simply select a new project in Eclipse's Package Explorer and open the Properties dialog from the Project menu.
  4. Select Android from the list and click the Add button.

    Eclipse Android libraries

  5. Select the project you marked as a library, and click the Ok button.

    Eclipse Android libraries

You have now successfully added the library to your project!

Eclipse Android libraries

Sitemap | Contact Us