Smartphones

Android library projects: Use common code in multiple apps

Android library projects allow you to easily create reusable code and resources. Learn more about library projects, including how to implement and use them.

Once you begin maintaining more than one Android app, it becomes apparent that a little organization can go a long way. Maintenance issues tend to grow worse over time, and failure to plan can take a terrible toll later.

A common problem to avoid in the software development world is duplicated code. Every line of code that exists in two places must be maintained and fixed in two places, which increases the work that must be done. The obvious solution is to use the same code multiple times, and luckily there is a simple mechanism in Android development to help you do that -- library projects.

What are Android library projects?

These are Android projects that can be included in other projects. The resulting app contains the necessary code from the library project as if it was in the same project.

While similar to simple libraries (e.g., jar files), there is a distinct advantage to library projects: Resources are also layered into the current project, which means you can use resources from a base project or even override them. There are a number of uses for this, and I'll cover a few of them later in this post.

How are library projects implemented?

Straight from the mouth of the Android team, these are the instructions for setting up a library project. Put simply: view the project's properties and then go to the Android section. At the bottom there is a Library section that contains everything you need on both the library side and the including project side.

A properly configured project can be converted to a library project by selecting the Is Library check box (Figure A). The library project can now be added to any other project by using the Add button on this same settings page for that project. Figure A

The instructions for setting up a library project are short, because they're easy to set up. The real magic is what you do with this capability.

Where are library projects useful?

Here's where the real fun begins. These are some of the ways I have used library projects in my apps:

  • Share common code between apps
  • Share common resources (e.g., strings and icons) between apps
  • Create a paid and free variant of the same app without duplicating much code
  • Create a different variation of an app to handle a specific app market or ad network (read my post on implementation options for ads in Android apps)
  • Distribute code and resources to other developers (some open source and commercial libraries are distributed in this way when they include assets)

What is most important here is that library projects allow me to easily create reusable code and resources. In fact, I regularly pull code from my newer apps down into the parent library projects, so that the code can be shared with my other apps. I even open sourced my code to handle Android app markets as a library project.

This can drastically reduce the amount of time to create a paid ad-free version of an app, or to create an app that uses much of the same code as a previous one. Some of my top-level projects are merely a thin layer holding all of the library projects together and adding a few project-specific strings, icons, and methods.

That's just the starting point

The uses of library projects go far beyond these examples. I'll leave it to you to get creative with your apps and make the best use of this technology. Now that you know how to create your own Android library projects to use in you apps, go see what you can do with it!

About

Tim Mackenzie, author of the Android Income Series books, is a software engineer that escaped the cubicle world at a large company to go solo with Android app development. He uses this freedom to teach others how to make money with Android apps. Visi...

0 comments

Editor's Picks