Converting an android app to a library

Nemanja Kovacevic
Apr 28, 2013 · 2 min read

Are you trying to convert your android app to library? It’s very simple, just go, Properties->Android, thick Is Library and that’s it. At least in theory, but here are couple of advices to make it work in real life as well…

  • Replace all switch blocks with if-else blocks. Switch can not be used in an android library with values like R.id.xxx These ints are final in an android app project but they can not be in a library I guess since app can override them.
  • If you are rolling your own custom views with custom xml attributes you’ll get a “No resource identifier found for attribute” error for each one. When you add this kind of elements in your layout files you must off course define xml namespace for environment to find attributes specifications. Usually that looks something like xmlns:custom=”http://schemas.android.com/apk/res/your_app_package_name" Problem is that your_app_package_name is not the real package name anymore since this is a library. You can not set here a real package name for obvious reasons :). The solution is to put xmlns:custom=”http://schemas.android.com/apk/res-auto" and this acts like a placeholder for the package name of the app using this library.
  • If you are using Application object, that code will be dead unless you reference it from the manifest of the app using the library.

There is an interesting use case for doing this. You’ve made an app but now you want to publish branded versions — “X app”, “Y app” with the same codebase but with branded UI. The easisest way to do this is to convert original project to thel ibrary and the reference it from the brand projects. More about this in one of the next posts.

Nemanja Kovacevic

Software engineer specialized in creating native android…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store