Barry Fruitman
Barry Fruitman

Reputation: 12656

Backwards compatibility in Android

I need to call a method in Android SDK v9 while maintaining compatibility with older versions.

The Android developer blog recommends using reflection or wrapper classes, but is that all really necessary? Why can't I just do this?

if (Build.VERSION.SDK_INT >= 9)
    callNewMethod();

It seems to me this will work due to Java runtime linkage, since I am building with SDK 9. Is there anything wrong with this approach?

Thanks in advance...

Upvotes: 0

Views: 134

Answers (2)

Gökhan Barış Aker
Gökhan Barış Aker

Reputation: 4535

No not at all. Actually it is promoted as best approach while developing applications with wide API level target.

Reflection class is the most solid way, if you have no idea what the class content is and the method exist. But in Android, you know what is supported and what is not supported.

As a result, i didn't like the blog you gave :p

Upvotes: 1

Kaediil
Kaediil

Reputation: 5535

Not necessarily a better answer, but the check you are doing there assumes that the builder of the OS/ROM set that value correctly. If it was not set correctly, then you may try to access a method in SDK 9 that really isn't there. Using reflection is the only way to be 100% sure you do not generate a runtime error by trying to call a non-existent method.

Upvotes: 1

Related Questions