Thursday 1 September 2011

Adding JAR Files to Android Projects

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!



No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...