Hey everybody,
i just wanted to share my way of making Android apps backwards compatible with older Android versions.
In my case i have the „App Update Notifier“ app. Basically it checks your installed apps for available updates. Therefore it queries the Android Market (via Android Market API). To be able to do this one has to provide a valid google account login. On Android 2.x devices there is the AccountManager class that you can query for the phone’s account information and use this information. This way the user doesn’t have to provide his login details again for your app. He just needs to accept once that your app might use his login details. But this feature is not available on Android 1.x. So the user has to provide valid google login details in the settings of my app. Because of that there were two versions of „App Update Notifier“ in the Android Market at the beginning. „App Update Notifier (for 1.x)“ and „App Update Notifier (for 2.x)“.
But then i thought of a way to make both functionalities available in one app. So i had to think of a way to check whether AccountManager is available on the current device or not. So i came up with using Java Reflection. I wrote this simple function:
[sourcecode language=“java“]
public static boolean classAvailable(String name) {
try {
Class.forName(name);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
[/sourcecode]
[sourcecode language=“java“]
if (classAvailable(„android.accounts.AccountManager“)) {
addPreferencesFromResource(R.xml.settingsscreen_2x);
} else {
addPreferencesFromResource(R.xml.settingsscreen_1x);
}
[/sourcecode]
This way you can check if a class you want to use is available on the device. But now you have a problem. The compiled DEX file contains stuff that might not exist on the current device. Android will then throw a VerifyException when it’s loading the DEX file. Now you have two options: use Reflection only to call your stuff (which is ok when only have to call a single method or so) or you need to seperate the code for different Android versions. So you only have supported references in you invoked dex file. For example i have a class with the AccountManager implementation called Helper2x and a class with the old way called Helper1x. This way on an Android 1.x device, the Helper2x DEX file will never be loaded and therefore will not throw an exception.
5 Antworten auf „Backward Compatible Android Programming“
[…] […]
Hey, danke fürs Erklären… ich befürchte uns blüht ein ähnliches Problem da das Milestone zwischen Android Versionen 2.0 – 2.0.1 – 2.1 sehr komische Sachen mit Kamera und GPS Listenern macht 😉
Grüße Pa
grad noch ein Update zu dem Thema vom Dev-Blog:
http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html
[…] Dieser Tipp wurde unter anderem auch schon von Goddchen gepostet – viele Entwickler setzen ihn gerne ein, da er sehr gut einzubinden ist und gerade durch die […]
Thanks for one’s marvelous posting! I quite enjoyed reading it, you happen to be a great author.I will make certain to bookmark your blog and will often come back in the future. I want to encourage you to ultimately continue your great work, have a nice weekend!