Saturday, February 18, 2012

Android: On using Android Support Package

As Android keeps improving and Google comes up with new versions of Android SDK, new features get introduced into Android (some call it the "Fragmentation" issue in Android SDK). As a developer, soon or late you will come across features in newer versions of SDK which you wish you could have used in your old code developed based on older versions of Android.


To make it possible for developers to use the new features of Android and still be able to run on older versions of Android, the Android "Support Package" has been introduced. Below some of typical examples on how this package can be used:

  • You have written your Android application based on API level 4 (Android 1.6) but want to rewrite your code to start using Fragments (Introduced since API level 11, Andorid 3.0), and at the same time make sure your application keeps running on old Android devices running on Android 1.6. Using Android Support Package you can go ahead and rewrite your application using Fragments.
  • You want to use some of those nifty UI patterns that were introduced later in Android (like NavUtils) . You can achieve this by adding the Support package into your existing code.
  • You want to start managing some of your processes (which consume more time and are long-running actions) using the new Android utilities. You can integrate LoaderManager in combination with your Activities/Fragments by using the Support package.
Luckily Google has extensive details and sample code on how to integrate Android Support Package into your project. Check out the Support Package page on Android developers site for explanation and sample code.
Just to add some more useful hints on how to get the package and how to debug it with its own source code, below is a brief illustration together with screenshot.

How to get it

Easiest way is to use the Android SDK Manager. In Eclipse you can use the Android plugin to trigger the Android SDK Manager. Once Android SDK Manager is started, go to Extras, select "Android Support package", and install it.

How to get the Android Support Package from SDK Manager
Once installation is done, Support package is installed on your machine, together with sources and examples. At the current state Support package includes two versions; version 4 and version 13.
Available Support Packages, v4 and v13
See Support Package page on Android developers site for more details on differences between these two versions and when to choose what version.

How to include Android Support package as a user library


One alternative way to include the Support package to your project is to add it as a "user library" in your project. Screen shots below illustrate this.
Define a new user library

  • Select the proper jar file to user library:

Assign the proper jar to user library

  • After assigning the jar file, in order to make sure during debugging you will be able to debug in the source from Support package as well, we need to set the "Source attachement" property for this user library.

Added JAR, but source is not set yet

  • Select "Source attachment:(None)" and click on "Edit" button on the right. In "Source Attachment Configuration" window, select "External Folder"

Assign source folder to user library for better debugging
  • Once "Folder Selection" is opened. Browse to where SDK source is installed. Normally this can be found within android SDK installation directory, and in a folder called "extras/android/support/v13/src/". select the folder and click OK. click OK again.

Setting up the source attachment
This should do the trick. You can start refactoring your existing code to use new set of Android features. Happy hacking into it. 
Again, check out the Support Package page on Android developers site for more details and sample code to make life easier.

1 comment: