March 7, 2021
Hot Topics:

Eclipse Android Tutorial: Working with Libraries

  • By Jessica Thornsby
  • Send Email »
  • More Articles »

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.

Originally published on https://www.developer.com.

Page 1 of 2

This article was originally published on May 9, 2012

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date