7

I have a project that is just using Android 1.5 for programming, but with the proliferation of other handsets and some cool features in Android 2.2, we'd like to support the features without losing support for 1.5 or forking a new code base. Is it possible to do with Android SDK?

I do have some sense of the "ugly" way to do it, as in keeping the same code base but have a build system that builds different versions for the platforms and keep different Java files around that get added in our out of the build based on which version is selected. I'm hoping someone else has solved the problem based on the many versions of apps in the market that run on multiple Android versions.

prasanna
  • 1,887
  • 3
  • 20
  • 26
  • duplicate of http://stackoverflow.com/questions/4968988/what-if-i-want-to-release-an-update-with-higher-minsdk-than-the-one-on-the-market/4969477#4969477 – rds Mar 24 '11 at 17:07

1 Answers1

6

we'd like to support the features without losing support for 1.5 or forking a new code base. Is it possible to do with Android SDK?

Sure. The techniques have been around for about a decade or so.

Option #1: Use reflection to access new classes (or new methods of existing classes) at runtime. See here for an example.

Option #2: Use conditional class loading, by creating an interface plus two implementations (one for an older API, one for a newer API), and loading the right implementation at runtime. Since the newer implementation will not be loaded on a older device, the fact that the newer implementation uses classes or methods that the older device lacks will not pose a problem. See here for an example.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Also note that for XML attributes you can freely use them and older versions of the platform will ignore them. For example, you can build against the 2.2 SDK to use the new attribute to say you are allowed to be installed on the SD card, and your app will continue to run on older platforms without doing anything else. – hackbod May 27 '10 at 06:25
  • Commonsware, your other example, PickDemo.java is more helpful for displaying contacts on API 4 and later APIs. – Yar Nov 08 '12 at 09:56
  • @Yar: I don't think that the `PickDemo` existed in March 2010. :-) Here's a link to it anyway: https://github.com/commonsguy/cw-omnibus/tree/master/Contacts/Pick – CommonsWare Nov 08 '12 at 10:33