I have been making a project on Android 4.0 and I have faced some deprecated types for the newer versions of Android. A black line has occured on the name of the deprecated things. However, in spite of the black line and the deprecation warnings, I can still use those deprecated classes and project is running successfully. I got confused about the deprecation. If they are deprecated how can I still use them and what does deprecation mean exactly? If I use the deprecated classes in my project, what can be the possible disadvantages that the project users can encounter?
Thanks for reading.
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
Long story short only use the deprecated method on systems running too low of an API to support the updated method. This way you can support a wider range of devices while only using the deprecated methods when you have to to support the older devices. Code example below.
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundDrawable(); // deprecated method for older devices
} else {
setBackground(); // updated method for newer devices
}
CHEERS :)
Don't forget that there is probably a reason for these classes to be deprecated. It might affect the security or stability of your app.
If you find out there is a bug or a flaw it will probably never be corrected.
Also checkout what classes they advise to use instead, if any.
Concerning your worries about newer Android versions:
Android versions are backward compatible
Confirmed here by an Android engineer in a topic about an other deprecated class. Therefore you should be able to use your app on newer versions.
If you want your application to be compatible with older versions of android while using a "new" class in your code, check this related topic.
often some types of classes are deprecated but you can still continue using them because to update applications and implement those classes you would have to modify the source code.
But if you find that there are unused classes is better than in coming soon applications no longer use them, personally I spend it with ClipboardManager and use applications where previously it still works.
Related
I am working on a huge android project with tons of classes. Until now we are compiling with Android 3.2 and giving support to Android 2.3.
Now I am testing to compile with Android 5.1.1, but still giving support to Android 2.3. My surprise was that a lot of code is deprecated now... (getWith(), getDrawable(), setBackgroundDrawable(), HttpRequest, ActivityGroup, etc...). Those deprecated functions does not give compile error, but it gives deprecated warning.
I know that I can duplicate code and make special calls to deprecated methods if SDK_INT is lower than XX and calls to new methods if SDK_INT is newer than XX, but this is a huge patch, that will need a lot of duplicate code in a lot of functions in a ton of classes and packages, so, if it is possible, we prefeer to wait until we don't need to give support to oldest versions of android. It means to continue using deprecated methods until for example we only need to give support from 4.4, that will means munch less duplicated code and functions will be needed.
The question is: If i compile with Android 5.1.1 but still use those deprecated functions, this huge project will continue to work on all devices (from 2.3 to 5.1.1)? Now it's working compiling with Android 3.2.
I understand that 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, but if it is still supported and does not give a compile error, it will work well as until now. It is what I understand of deprecation. It is right?
Thanks.
Deprecated means two things:
There is another (maybe better) solution
It isn't fully support anymore
That means that your code could be run fine. But Google/Android don't guarantee that.
According to Java documentation the #Deprecated annotation says:
#Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the #Deprecated annotation. [...]
So please stop using deprecated methods. :)
Have you looked or do you know that there are support-libraries to help you with backward compatibility?
Yes it should work fine. But you really should use the latest methods instead of deprecated ones and use support libraries to make it compatible to the previous versions.
All the deprecated methods that I have used have worked fine, but Google does not guarantee that they will.
You can continue using deprecated methods, they should still work as intended. But you have to be careful, because they could be removed in future versions.
As mentioned in the documention over developer.android.com
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.
It may stop working with new version.
Example: LocationManager.addNmeaListener(GpsStatus.NmeaListener listener)
Deprecated from API 24.
Stop Working in API 29 (Android 10). No implementation inside (returns false).
In documentation: No-op method to keep backward-compatibility.
Had trouble to find why old code stop working.
I like to use the findAll/ findAllAsync method in android.webkit.WebView. findAll is deprecated and Google suggests to use findAllAsync which requires Jelly Bean or higher. However, I like my application to support 2.2+. I tried to the following, but I get warning for findAll (deprecation) and error for findAllAysnc (need to increment minimum SDK version):
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
myWebView.findAll(query);
else
{
myWebView.findAllAsync(query);
}
What's the best way to deal with this? Should I just use findAll and ignore the deprecation warning?
I believe the answer goes in several ways:
What are you setting as min SDK version and target SDK version in the manifest ?
Same question, but in Eclipse (or whatever IDE you're using) for Android build API properties ?
(I'm answering as if your question is "how do I get rid of the android lint warning", rather than "how do I fix the warning correctly" .. )
Use findAll and if the warning is really too annoying add a #SuppressWarning("deprecation") annotation to suppress it explicitly.
One problem with this is that when you use this annotation on your method you might miss other deprecated calls as it will apply to the whole method.
There's a very interesting and powerful way of doing what you want, and it is called reflection. From the Oracle's Java documentation:
Reflection is commonly used by programs which require the ability to
examine or modify the runtime behavior of applications running in the
Java virtual machine. This is a relatively advanced feature and should
be used only by developers who have a strong grasp of the fundamentals
of the language.
In short, reflection allows you to find out if class is defined, you can find out its methods and properties, and invoke a class' functionality... all at runtime!
I have an app that needs to handle the audio focus on Android 2.1 devices but the Audio Focus is not available for those versions, so I use this technique. It is, by the way, a bumpy road. I would suggest reading carefully the documentation and try to follow some examples.
how detect deprecated method in android application? which resource is needed, for example class files..
My problem is the derecated error is reported in local eclipse but not reported in server for same source. Then what should I check for this problem? please help
Your problem is not really clear.
If Eclipse says the method is deprecated, then it is.
The documentation often tells you what is the new and better way to do what the deprecated method did.
In android doc (http://developer.android.com/reference/packages.html), you can choose the target version on the top of the list on the left, so you should check the doc for the version you are targeting.
It might be acceptable for you to keep a deprecated method if you absolutely need it and want your min version to be an old one. You can also check the support package that provide you compatibility functions of newer versions
Extending #Toaster a bit, deprecated functions are those that are currently supported but may not be supported by future versions. In simpler words, current android versions such KitKat (4.4) will be able to run that function but another update may find that function doesn't exist anymore.
The eclipse gives us warning not show problem that this particular version is working fine now but may be removed in the future releases, so change that function with a function that is not depcrecated and will certainly be present in future versions so that your app may work properly even in case or an OS update.
As far as problem showing the deprecated functions on your server is concerned, i don't know it. Although you might not need it, if you upgrade your functions, but if you do then post a different question and i am sure someone will be able to help you. Happy Coding!!!
Android platform sample codes and reference from the Android developer site is based on platform 1.5 I understand that newer platforms can support applications developed on older platforms but the reciprocal is not applicable which makes sense but is the coding different? Are codes that were used for developing 1.5 apps still useful in newer platforms or have newer classes and methods replaced them? It seems that eclipse is producing a lot of coding errors in its samples in relation to classes and methods also if a app that was developd by a IME is unable to be viewed on the emulator or how can it be tested or retrieved on the device? Any advice is welcome...sorry it's so long
If you look in the SDK folders, on windows it will be c:\<SDK location>\samples\android-x the samples are located according to api level so they will definitely be compatible there so I would look at these.
To answer your other questions, yes there are api changes as you go up an api level so they should cause warnings or compilation errors and some classes may even be completely removed. Generally the lower level stuff shouldn't change too much but the most important thing is that the semantics change rarely unless there was a design flaw in the original implementations.
The release notes for each version usually points out what has changed and the online documentation is generally superb in my opinion in informing you what exactly is deprecated. If you are just targeting old devices then your emulator is just set to target those api levels but if you are concerned about functionality then you could code using api 1.5 say, and run an ICS api level 15 emulator and check everything works OK, if not then you decide what the best strategy should be. Generally I would advise to target Android 2.2 and above for mobile devices and 3.0 for tablets but really it is up to you.
three are classes that are deprecated and can't be used anymore, like Contacts.People. There are also classes that are deprecated, they can still be used but they should be avoided in new projects. And there are new classes that were not available before. In some cases like for Fragment there are compatibility support libs to use the new features on the old platform but this is not true for classes like for example PreferenceFragment that are not supported on old platforms.
I see quite a few good old useful methods or even entire classes being "deprecated and obsolete".
But code that used to call those methods continues to work. So, what does this mean to me, as an Android applications developer?
Continue using this method as long as I want, because newer SDKs
will always remain backward compatible.
It will work as long as I build for older targets (e.g. API 8), but
if I build from API 14 up, the compiler will refuse to complete
the build.
Both (1) and (2)
Other?
This is especially confusing when no alternatives are provided, as in the case of WebView.PictureListener.html#onNewPicture.
It usually means that there's either a better way of doing things or that the deprecated functionality had some irreparable flaw and should be avoided. You can usually keep using deprecated methods, but you are advised to either switch to some new API (in the first case) or find some other way of doing what you want (in the second).
Regarding onNewPicture in particular, the entire PictureListener interface is deprecated. There's no sign of what, if anything, is supposed to replace it. A comment by #CommonsWare in this thread is food for thought:
It is conceivable that upstream changes in WebKit are driving the deprecation and that support for PictureListener might be totally lost in some future release.
I would go with 4:
It will basically tell you that the use of the method or class is discouraged; it is NOT 100% that they will keep backward compatibility (they can decide to not include that method in future releases), so you should try to use the replacement of the method or class. This is sometimes not possible to use the new methods (for instance, if you want to support devices running older versions).
Some other times it is actually possible. For instance, the showDialog method is now deprecated and they recommend to use DialogFragment class. You can achieve that even in older versions of Android by using the compatibility library.
Deprecated methods are not guaranteed to remain backwards compatible. They might remain in there for a few more releases just to give everyone a chance to migrate away from them before the developers remove them. The fact that they're deprecated means that the developers think that there's an easier, faster, neater, or otherwise better way to do whatever that class or method does.
It's probably better to change your code to use a non-deprecated interface now, since if you wait and it does get removed, your users will see crashes and errors.
Even when they are deprecated, they may compile but not work. Google has decided to delete various functionality at the low OS level.
Case in point. Google, at android release 2.3 deprecated many but not all method API's that allowed call recording. They compile OK but do not function since Android 2.3 and forward on any android phone device, or tablet with phone capabilities.
As an example for a deprecated interface that has been removed in a later API level, consider the org.apache.http package: It has been deprecated in API level 22 and removed in API level 23.
Of course on actual Android devices, the classes contained in that package will still be available in the system libraries (otherwise, applications targeting an older Android release would no longer run on that device).
They are however not available in the SDK anymore, so compilation will fail unless you either change the target/build SDK to an older version (or manually include the deprecated classes).
If Google were really determined to discourage use of those libraries, they could modify the implementation so that the affected classes check the target API version of the running application and complain and/or throw a runtime exception.