1. Android library projects and Java libraries

Android project can use code contained in
JAR
files (Java libraries). It is also possible to create libraries
modules which can be used as dependencies in Android projects.
These modules 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. Depending on your IDE this automatically makes
your JAR available or your have to do an additional step.

2.2. Using Eclipse ADT

The Eclipse ADT 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.

This automatic addition to the classpath requires that you call the
folder
libs.

2.3. Using Android Studio

In Android Studio you have to

Right-click the JAR file in the libs folder and select
Add as library.

Check that you have a new entry
compile files('libs/YOURJAR.jar'
in your
build.gradle
file.

To ensure that the change is picked up, perform a clean
build.

2.4. 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. Custom Android library modules

3.1. Using custom library modules

If an Android application project uses an Android
library module, 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 module
can be
considered to
be a compile-time
artifact. An Android library module 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.

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.

3.2. 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. Creating custom Android library modules in Android Studio

To create a new libary
module in Android Studio, select
File → New Module
and select
Android Library.

5. Creating custom Android library modules

5.1. Defining Libraries modules in Eclipse ADT

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.

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.

6. Android library project

The Android team introduced a new binary distribution format
called
Android ARchive(AAR). The
<filenname>.aar</filenname>
bundle is the binary distribution of an Android Library
Project.

An AAR
is similar to a JAR file, but it can contain resources
as
well
as
compiled byte-code. This allows that an AAR
file is
included
in the build
process of an Android application similar to
a
JAR
file

8. Prerequisite

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

9. Exercise: Create Android library module

9.1. Target

Our library project will not contribute Android components but a
data
model and a method to get the number of instances. The library
provides access to (fake) RSS data. 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.

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.

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

9.3. Using Android Studio: Create library module

For Android Studio each library is a module. To create a new libary
module in Android Studio, select
File → New Module
and select
Android Library.

Use
com.example.android.rssfeedlibrary
as module name.

9.4. Using Eclipse ADT: Create library module

Eclipse ADT defines a library module via project which an additional
property. Therefore
create a new Android project called
com.example.android.rssfeedlibrary. You do not need to create an activity.

On the second page of the wizard select the option that the new
project is a library project.

To ensure that the project is configured as library project,
right-click on the Android project and select
Properties. Ensure that the
is Library
flag is set.

9.5. Create Model class

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

Generate the getters and setter, the constructor and a
toString()
method. The result should look like the following class:

10. About this website

10.1. Donate to support free tutorials

Please consider a contribution
if this article helped you. It will help to maintain our content and our Open Source activities.

10.2. Questions and discussion

Writing and updating these tutorials is a lot of work.
If this
free community service was helpful,
you can support the cause by
giving
a tip
as well as reporting typos and factual errors.

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.