How to Deal with Deprecated Classes in Android to Keep Compatibility

How to deal with deprecated classes in Android to keep compatibility

You can do that (checking the API version).

You can also use reflection to call the newer classes.

I wouldn't worry about using deprecated methods as all Android versions are backwards compatible, saying that you want to watch when things are for 3.0 Honeycomb as these are a little different.

Here's an explanation of how to use reflection: (yes it's been on SO before, so maybe search for reflection)

http://www.youtube.com/watch?v=zNmohaZYvPw&feature=player_detailpage#t=2087s

I'm looking at making the project this is in available but until then here's some code:

(You could do this in a class that extends Application i.e. one time setup)

 public static Method getExternalFilesDir;

static {
try {
Class<?> partypes[] = new Class[1];
partypes[0] = String.class;
getExternalFilesDir = Context.class.getMethod("getExternalFilesDir", partypes);
} catch (NoSuchMethodException e) {
Log.e(TAG, "getExternalFilesDir isn't available in this devices api");
}
}

Now getExternalFilesDir() is only available on API level 8 or above, so I want to use this if they have (Froyo), but otherwise I need another method.

Now I have my test for the method I can go ahead and attempt to use it:

  if(ClassThatExtendsApplication.getExternalFilesDir != null){
Object arglist[] = new Object[1];
arglist[0] = null;
File path = (File) ClassThatExtendsApplication.getExternalFilesDir.invoke(context, arglist);
// etc etc
} else {
// Not available do something else (like your deprecated methods / or load a different class / or notify they should get a newer version of Android to enhance your app ;-))
}

Hope that helps and shortcuts a lot of googling :-)

P.S. if in the else you want to use your deprectated methods still, just add the @SuppressWarnings("deprecation") annotation above it, This will get rid of the warning and you have done it for the right reasons as you are using the latest API's when possible.

How can I avoid deprecated methods and maintain backwards compatibility?

Given a Display object named display, this should work:

int width=-1;

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size=new Point();
size=display.getSize(size);
width=size.x;
}
else {
width=display.getWidth();
}

IOW, use Build.VERSION.SDK_INT to branch between the "before" and "after" cases for where a new API is introduced.

This will require your build target (Project > Properties > Android in Eclipse) to be set to API Level 13+, so you can call getSize().

Can I use deprecated classes?

From the documentation:

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.

So you can use some deprecated methods but it won't be the best practice because there are better alternative exists(but in some cases this can even be dangerous)

Is it OK to have so many deprecated methods in backward-compatible code?

You have to ask yourself a few questions:

  • Was it deprecated for the API levels that will be executing this code?
  • If so, is an alternative suggested or available?

In your case, getWidth() is deprecated in favor of using getSize(Point), which requires API level 13. So prior to API level 13, all you have is getWidth(), which at the time was not deprecated. The reason these deprecated methods are kept around is largely with backwards compatibility in mind (along with avoiding breaking everyone's apps which depend on it).

So to answer your questions, yes, this is fine in this case, and yes, it's a good use of @SuppressWarning("deprecation").

Deprecated Classess on Android

Deprecated means that they are likely to be removed in a future version of the platform and so you should begin looking at replacing their use in your code.

If they just removed the types then builds would break and people wouldn't be happy!

In terms of the effect they will have on your application's users, there shouldn't be any effects at all. However, when you come to update your software to the next version of Android you may find that the deprecated types are no longer there and your build will break.

Wikipedia has a good article on it here: http://en.wikipedia.org/wiki/Deprecation

How to get rid of deprecation warnings when providing backwards compatibility?

In order to get rid of such deprecation warnings, when providing backwards compatibility,
one has to remove the import, to which one cannot apply @SuppressWarnings("deprecation"):

// import android.net.NetworkInfo;

And then use it's fully qualified class name android.net.NetworkInfo instead of NetworkInfo. The point is, that one can only apply @SuppressWarnings("deprecation") to methods, but not imports.

If something is deprecated, can I still use it?

Essentially deprecated is a warning to you as a developer that while the method/class/whatever is there and works it is not the best way to do it. Either a newer and better alternative is available or (sometimes) there is something subtly broken about it that makes it not advisable to use.

You can still use deprecated things - but you should look to see why they are deprecated first and see if the new way of doing things is better for you.

In theory deprecated things can or will in future be removed. That rarely happens (some Java libraries have deprecated stuff in them that is more than ten years old) but is a risk you should be aware of.



Related Topics



Leave a reply



Submit