Android Essentials: Creating Android-Compliant Libraries

Android libraries can be packaged and reused in other Android applications using the new Android Library feature of the Android SDK. In this quick tutorial, you will learn how to create simple Android library files that can be reused in multiple projects and increase developer efficiency.

For a long time, Android developers have struggled with how to organize their Android code such that it can be easily reused and support multiple build targets. Now in the Android SDK Tools r14 for Eclipse, we can easily create Android compatible libraries and reference them within Android applications, making code organization and reuse simple and straightforward.


Android Compatible Libraries

For a library to be compatible with Android, it can only reference classes available as part of Android and other classes implemented specifically in the library itself. See the Android Developer website SDK reference for a complete list of packages available for use with Android applications at: http://d.android.com/reference/packages.html.

Android Libraries can contain Java classes, resources, and other project information, but not assets. They can reference other libraries and leverage third party JAR files. They have Android manifest files just like regular Android projects. However, they differ from normal Android projects in an important way: they cannot be compiled into their own application packages or deployed onto devices. They can also not be exported as standalone JAR files.

Once referenced from an Android project, the library components are incorporated into the Android application that references them at build time and added to the application package. There is no need to declare the component as the library classes are rolled into the APK directly.


Step 0: Getting Started

This tutorial assumes you are using the Eclipse development environment popular with Android developers with recent versions of the Android SDK Tools (r14 or newer). The steps described use the latest version available, which, at this point, is Eclipse Indigo (3.7). The steps may vary slightly with different versions of the tools.


Step 1: Create an Android Project

First, create a new Android project. You can do this by launching the Android project wizard in Eclipse (File, New, Project, Android, Android Project). Add any classes and resources you would like to it.


Step 2: Expose Shared Components

You will need to make sure that any shared components are defined in the Android Manifest file, like normal. So, for example, activities, services, and the like will need to be defined in the Android Manifest file as they normally would be.


Step 3: Set the Library API Level

Make sure to set the API level within your library appropriately in the Android Manifest file in order to ensure compatibility with Android projects. Set the lowest API Level possible.


Step 4: Mark as a Library Project

Any Android project can be marked as an Android Library.

To mark an Android project as a Library in Eclipse, navigate to the Project, Properties, Android and check the checkbox for Is Library, as shown here:

Eclipse setting for setting an Android project as a library

Step 5: Reference the Library

Now that you have successfully marked your Android project as a library, you can reference it from other Android projects in Eclipse. In terms of compatibility, the Android project must have an API Level higher than or equal to the API Level set in the Android library.

To reference your Android library from within another Android project, select the other project. Navigate to the Project, Properties, Android and add a reference to the Android library you created, as shown here:

Using an already existing Android library

If your application references multiple Android libraries, you can set the relative priority of each library to resolve resource conflicts.


Step 6: Start Using the Library

Now that you have successfully linked the Android library to your project, you should be able to use the classes by name just as you would the core Android SDK classes. Remember you can use Ctrl+Shift+O to organize your imports in your Java classes!

Again, you will have to declare all application components used in the application that are sourced from the Android library in the Android Manifest file. You will need to use the fully qualified package names to reference these library components.


Conclusion

Android libraries are a convenient way to share code amongst multiple Android projects in Eclipse. Although there are some limitations and they are not fully-fledged standalone JAR files, libraries are a welcome feature of now enabled in the latest Android SDK tool chain for organizing code.

Have you made any useful and publicly available Android libraries for developers? If so, let us know in the comments!

About the Authors

Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams Teach Yourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to [email protected], via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon

Tags:

Comments

Related Articles