Tutorial details

Android Library Projects - Tutorial | App Code for Sale | Preview

Android Library Projects - Tutorial | Android Tutorial

How to create and use library projects in Android

Overview PAGE TOP

Lars Vogel

Version 1.3

Copyright © 2011, 2012, 2013 Lars Vogel



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.2.

1.1. Motivation PAGE TOP

Android library projects allow to store source code and resources which are used by several other Android projects. The Android development tools compile the content of library into the Android project by creating a JAR file.

Library projects cannot be compiled to Android applications directly.

Using library projects help 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.

1.2. Creating and using library projects PAGE TOP

If the Android development tools build a project which uses a library project, it also builds the components of the library and adds them to the .apk file of the compiled application.

Therefore a library project can be considered to be a compile-time artifact. A 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.

1.3. Priorities for conflicting resources PAGE TOP

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

2. Using JAR files in Android PAGE TOP

To use a Java library inside your Android project directly, you can create a folder called libs and place your JAR into this folder.


3. Prerequisite PAGE TOP

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

4.1. Create library project PAGE TOP

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


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

4.2. RSS - Really Simple Syndication PAGE TOP

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 webserver, which RSS client read. These RSS clients parse the file and display it.

4.3. Create Model class PAGE TOP

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;


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 + "]"; }


4.4. Create instances PAGE TOP

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");
    item = new RssItem("test2", "l2");
    // 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.

4.5. Set as library project PAGE TOP

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


4.6. Use library project PAGE TOP

In your application project defines that you want to use the library project via the project properties.


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.

// Update the method updateDetail() {
// more code....

// Reading the RSS items
List<RssItem> list = RssFeedProvider
String text = String.valueOf(list.size());
// TODO send text to the Detail fragment

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.