Cogitas Blog:
Musings on developing Android apps,
machine learning and misc tech stuff.

Using Android Library projects

Filed under: google android — December 19, 2012

Android library projects are very useful in a number of situations. For example, you are a company providing a SDK that includes UI elements, such as Facebook (Facebook SDK), or you create a UI widget that developers can drop in into their projects, such as a TimePicker that looks like a wheel (Android wheel).

As a developer, you may also want to use Android library projects to easily create several spin offs of the same app. The obvious example is when you create free and paid apps.

Library projects are very easy to set up. It’s a normal Android project but in the properties Android tab, you just tick “is Library”. In the project calling the library, in the properties Android tab, you click on add and a list of library projects in your workspace will show up. The first thing to know is that all library projects used within a project must be in your Eclipse workspace and open.

You can use library projects to override graphics, strings, styles etc Think of it as “skinning”. Let’s say you have a library project with a drawable called company_logo.png. If in your project calling your library project, you put a file with the same name in the drawable folder, this file will be used instead of the one in the library. Basically, you can override library resources in your main project.

There are a couple of issues you should be aware of:

If you have company_logo.png, you won’t be able to override it with company_logo.xml (a drawable shape file), files must have exactly the same extension.

Also, the actual overriding doesn’t always work in code. Let’s say that your code does something like myButton.setBackgroundColor(getResources().getColor(R.color.myColor)) – well, it sometimes work, and sometimes it doesn’t. I had some code doing that and it worked fine on my Samsung S2 (it showed myColor as defined in colors.xml of main project) but it turned out not to work on HTC One S and on Samsung S3 (it showed the color as defined in colors.xml of library project).

Lastly, a couple of tips for building your projects that contains Android libraries:

Eclipse is prone to crashing when exporting signed apk for projects that contain library projects, particularly if the library projects are big in themselves. So in the menu “project”, unselect “build automatically” before exporting.

Those projects are prone to breaking easily whenever Eclipse updates the workspace. The trick is to clean all projects involved, starting from the bottom one. Let say you use Facebook SDK (a library project in itself) in a library project you have set up which you then call from your free and paid app projects: clean up Facebook SDK first, then clean up your library project, then clean up your free app and paid app projects. When cleaning in Eclipse, there are a few options: choose to clean only the project selected and not the entire workspace.

Despite a few annoying quirks, Android library projects are easy to use, I highly recommend them!

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment