Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Using the Compatibility Libraries

Author: Ian Darwin -- Published? true -- FormatLanguage: W


You want to use new features of Android but have your application run correctly on older versions.


Use the compatibility libraries - that's what they're there for.


Android is a wonderful system for users - new features are added with every release. But there's a problem - older devices don't get the latest version of Android. Manufacturers at the low end (low budget devices) might not ever issue an update to their devices. At the higher end (so-called "flagship" devices), users usually get 2 or 3 years of updates from the manufacturer. But manufacturers (and cell phone carriers) expect users to update often, like car manufacturers who come out with new models every year to embarass owners into buying upgrades they don't really need.

Now you might think that old versions fade away quickly, but a look at https://developer.android.com/about/dashboards/index.html[the Android Versions dashboard] (shown in version-dashboard) reveals this to be true only "from a certain point of view".

label .Android Versions Dashboard image::images/dashboard.png[]

(This figure is published by Google Inc. licensed under Creative Commons Attribution 2.5.)

Notice that Froyo, API 8, is at 0.1%, which is the threshold for inclusion. Thuse you'd expect it to disappear any day now, but in fact, it's been at 0.1% for several months. There are an estimated 1.5 billion Android devices. So 0.1% of that means there are still a million and a half active users of Froyo devices. For Gingerbread there are still 25 million active devices. If you're prepared to overlook 26 million potential customers, fine. But even then it's not that simple - there are features added in Android 4, 5, 6, and 7. How doe you keep track of what feature is in what version? For the most part, you don't need to. That is, if you use the compatability libraries!

If you create a new project using Android Studio, the project will by default use the compatibility library. If you are working on a project that doesn't have "compat" support, you can add it easily. One way is to add the library manually by editing your build file to include the library with co-ordinates (see <<maven>>) 'com.android.support:design:24.1.1'; add this to pom.xml for Maven or app/build.gradle for Android Studio. On Android Studio you can also select the "app" module and select Module Properties->Dependencies, click ++Add", and select the latest version of the compatibility library.

Then the most important change is to ensure that your Activities - any that will need the latest facilities - are based on ++AppCompatActivity++ rather than the regular ++Activity++.

public class MainActivity extends AppCompatActivity {



There are some other places where the "app compat" libraries enter into view of the coder; we will mostly call these out in place in the rest of this book.

The downside of this for us as developers is that some features that have been added and modern versions such as Android 7.0, will not exist on the user's device. If you don't take this into account, you may wind up calling methods that exist in modern versions but not in the library on the user's device. This will, of course, end badly.

The solution is the compatibility libraries. These provide replacment versions of common classes (such as ++Activity++) that use only features found on the old Android version, but provide the functionality of newer versions.

No records found.