Referencing Libraries to Implement External Functionality
In Chapter: Getting Started
Author: Rachee Singh (firstname.lastname@example.org) 'racheesingh'
You need to reference an external library in your Android source project
Obtain the JAR file for the library that you require, and add it to your project.
As an example, you might require to use AndroidPlot, a library to plot charts and graphs in your application or use OpenStreetMaps. For this your application needs to reference these libraries.
ADT Plugin Version 17 or later:
- Ensure that your project has a folder with the exact name of "libs" (not the traditional "lib").
- Download the library JAR file into this folder.
You are done: Eclipse (and the standard Ant build process) will now add your Jar file to the project's classpath.
ADT Plugin Version 16 or earlier (legacy)
In Eclipse you need to specify the Jar file in your build path:
- Create your project as described in earlier recipes;
- Download the JAR file corresponding to the library you wish to use.
For future reference you should create a folder called "libs" (not lib)
and store the Jar file there.
- Open your Android project in Eclipse, right click on the project name and select 'Properties' option in the menu.
- From the list on the left side, select 'Java Build Path' and click on the 'Libraries' tab.
- Click on 'Add External JARs' button.
- Provide the location where you downloaded the JAR file for the library you wish to use.
Now you will see a 'Referenced Libraries' directory appearing in your project. Within this directory the JARs that you added will appear.
An alternate approach is to create a 'lib' folder in your project, physically copy the Jar files there, and add them individually as above, but using "Add Jars". This way keeps everything in one place (especially if your project is shared by a version control system to others who might even use a different operating system and be unable to locate the external jars in the same place). However it does raise the burden of responsibility for license issues on the included jar files.
In either case, if you also build with Ant, be sure to update your build.xml file.
Whichever way you do it, it's pretty easy to add libraries to your project.
The source code for this project is in the Android Cookbook repository,