Libraries in Android. Part I

Libraries are one of the best mechanisms to get reusable code. Knowing how to create libraries is a basic step when we have any common functionality for several apps or we want to share it with other developers. The two simplest ways to share code in Android are:

  • Android library projects. We can get any standard Android project to act as a library by activating this property. The library can be imported into any project that needs to use it. The library project is distributed with its source code and resources. To read a complete description of library projects, see the official documentation here.
  • JAR files. A .jar file contains the compiled classes. The source code can be included in it or not. We can’t share Android resources using a .jar file, just code classes.

The decision to choose between one of them depends on our requirements. Do we want to share xml resources? Do we mind sharing the source code?
Library types

How can we share a project with resources but not to share the source code? As the Android documentation states,

A library cannot be distributed as a binary file (such as a JAR file). This will be added in a future version of the SDK Tools.

I propose two solutions (surely there are more of them):

  • Create a Library Project to include the resources, but include the source code using a JAR file.
  • Create the resources programmatically instead of using the XML files and export it to a JAR file.

These proposed solutions will be shown in detail in the next posts about this topic:

Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.