Support free tutorials:











vogella training Training Books



Android Library Projects - Tutorial

Lars Vogel

Version 1.4

24.03.2014

Revision History
Revision 0.1 - 1.4 16.04.2012 - 24.03.2014 Lars
Vogel
bugfixes and updates

Android Library Projects

This tutorial describes how to create and use library projects in Android. The tutorial is based on Eclipse 4.2, Java 1.6 and Android 4.4.


Table of Contents

1. Android library projects and Java libraries
2. Using JAR files in Android
2.1. How to use JAR files
2.2. Restrictions in using Java libraries
3. Android library project
3.1. Using library projects
3.2. Declaring components and auto-merge
3.3. Priorities for conflicting resources
4. Prerequisite
5. Tutorial: Create Android library projects
5.1. Create library project
5.2. RSS - Really Simple Syndication
5.3. Create Model class
5.4. Generate getter and setter
5.5. Create instances
5.6. Set as library project
5.7. Define dependency to the library project
5.8. Use library project to update detailed fragments
6. Support free vogella tutorials
6.1. Thank you
6.2. Questions and Discussion
7. Links and Literature
7.1. Source Code
7.2. Android Resources
7.3. vogella Resources

1. Android library projects and Java libraries

Android project can use code contained in JAR files (Java libraries). It is also possible to use Android library projects. These projects allow you to store source code and Android resources which can be shared between several other Android projects.

2. Using JAR files in Android

2.1. How to use JAR files

To use a Java library (JAR file) inside your Android project, you can simple copy the JAR file into the folder called libs in your application.

The Android tooling adds the JAR file automatically to the classpath of your project. During deployment the Android tooling compiles the .class files in the Android .dex file which contains the Android byte-code.

Tip

If you call the folder libs, the Android Developer Tools automatically add the JAR file to the classpath of your project.

2.2. Restrictions in using Java libraries

If you want to use libraries, these must only use API available in Android. For example, the Android libraries do not contain the java.awt and javax.swing user interface libraries, as Android has its own user interface toolkit.

Library projects cannot be compiled to Android applications and started without another project using them.

Using library projects helps you to structure your application code. Also more and more important Open Source libraries are available for Android. Understanding library projects is therefore important for every Android programmer.

3. Android library project

3.1. Using library projects

If an Android application project uses an Android library project, the Android development tools include the code and resources from the library project into the build result of the Android project. This means, that the components, code and resources of the library project are compiled and packaged into the .apk file of the compiled application.

Therefore a library project can be considered to be a compile-time artifact. An Android library project can contain Java classes, Android components and resources. Only assets are not supported.

To create a library project, set the Mark this project as library flag in the Android project generation wizard.

To use such a library, select the generated project, right-click on it and select Properties. On the Android tab, add the library project to it.

The library project must declare all its components, e.g., activities, service, etc. via the AndroidManifest.xml file. The application which uses the library must also declare all the used components via the AndroidManifest.xml file.

Other projects can now use this library project. This can also be set via the properties of the corresponding project.

3.2. Declaring components and auto-merge

If you define components, e.g., activities in your library project, you need to re-define these components in the consuming Android application. If you want to automatically add all components from your library project to your application, add the manifestmerger.enabled=true to your project.properties file of your application project.

3.3. Priorities for conflicting resources

The Android development tools merges the resources of a library project with the resources of the application project. In the case that a resource's ID is defined several times, the tools select the resource from the application, or the library with highest priority, and discard the other resource.

4. Prerequisite

The following example assumes that you have created a normal Android project called com.example.android.rssfeed based on the Android Fragments tutorial.

5. Tutorial: Create Android library projects

5.1. Create library project

Create a new Android project called com.example.android.rssfeedlibrary. Do not need to create an activity.

Setting the library property

Our library project will not contribute Android components but a data model and a method to get the number of instances. We will provide RSS-feed data. The following gives a short introduction into RSS.

5.2. RSS - Really Simple Syndication

An RSS document is an XML file which can be used to publish blog entries and news. The format of the XML file is specified via the RSS specification.

RSS stands for Really Simple Syndication (in version 2.0 of the RSS specification).

Typically a RSS file is provided by a web server, which RSS client read. These RSS clients parse the file and display it.

5.3. Create Model class

Create an RssItem class which can store data of an RSS entry.

package com.example.android.rssfeedlibrary;

public class RssItem {
  private String pubDate;
  private String description; 
  private String link;
  private String title;
  
} 

5.4. Generate getter and setter

Use Eclipse code generation capabilities which can be found in the menu under Source to generate the getters and setter, the constructor and a toString() method. The result should look like the following class:

package com.example.android.rssfeedlibrary;

public class RssItem {
  private String pubDate;
  private String description;
  private String link;
  private String title;

  public RssItem() {
  }  
  
  public RssItem(String title, String link) {
    this.title = title;
    this.link = link;
  }

  public String getPubDate() {
    return pubDate;
  }

  public void setPubDate(String pubDate) {
    this.pubDate = pubDate;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getLink() {
    return link;
  }

  public void setLink(String link) {
    this.link = link;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  @Override
  public String toString() {
    return "RssItem [title=" + title + "]";
  }

} 

5.5. Create instances

Create a new class called RssFeedProvider with a static method to return a list of RssItem objects.

package com.example.android.rssfeedlibrary;

import java.util.ArrayList;
import java.util.List;

public class RssFeedProvider {
  // Helper method to get a list
  // of RssItems

  public static List<RssItem> parse(String rssFeed) {

    List<RssItem> list = new ArrayList<RssItem>();

    // create some example data
    RssItem item = new RssItem("test1", "l1");
    list.add(item);
    item = new RssItem("test2", "l2");
    list.add(item);
    // TODO create a few more instances of RssItem

    return list;
  }
} 

Solve the TODOs to create example instances of the RssItem class and add it to the list. This method does currently only return test data.

5.6. Set as library project

Right-click on the Android project and select Properties. Ensure that the is Library flag is set.

Setting the library property

5.7. Define dependency to the library project

In your application project, define that you want to use the library project via the project properties. For this, right click on the project and select Properties from the context menu.

Afterwards select the Android entry in the dialog. Use the Add... button in the Library section and add a reference to your library project.

Using the library property

5.8. Use library project to update detailed fragments

Use the static method of RssFeedProvider to get the list of RssItem objects and display the number in your DetailFragment instead of current system time.

To send the new data, change your MyListFragment class:

public void updateDetail() {
  List<RssItem> list = RssFeedProvider
      .parse("http://www.vogella.com/article.rss");
    String text = String.valueOf(list.size());
  listener.onRssItemSelected(text);
} 

6. Support free vogella tutorials

Maintaining high quality free online tutorials is a lot of work. Please support free tutorials by donating or by reporting typos and factual errors.

6.1. Thank you

Please consider a contribution if this article helped you.

Flattr this

6.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

7. Links and Literature

7.1. Source Code

Source Code of Examples

7.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java, compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put all your files in a distributed version control system