What is the benefit of adding annotations suggested by the lint such as "#ExperimentalApi" or other recommended stuff like this?
Does it mean in the future it can warn us if the API changed?
Yes, it means that the API can change or be removed in future.
Experimental APIs are good for testing around new functionalities but usually their use is discouraged in production code.
PS: Kotlin now uses #RequiresOptIn (and #OptIn) instead of #Experimental which is deprecated. You can find more info here
Related
I was about to use the android class Sound Pool when I noticed it has become deprecated. Should I try to find the newest version every-time or just use the deprecated method?
Real answer? Never.
It is deprecated for a reason. Deprecated methods are methods that will not be updated and may cease to be supported at all. Try to find the newest way of doing what you want to do with the most up-to-date API.
From: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Deprecated.html
A program element annotated #Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
There is a better way to do it, but that doesn't mean you cant do it. You are probably safe, but you will have to keep an eye on performance, especially for android.
Some methods are marked with a line on it. Is this some kind of error ?? this thing bothers me but the applications work fine.
Deprecated methods. That means that there are newer alternative methods available to be used which are more compatible. Deprecated methods do work, but Google does not guarantee their proper functioning. And support for those methods may be ended in future versions of android.
They are deprecated. Consider using ones who aren't.
I read Android documents and also I find deprecated methods and classes in that.Can I use deprecated methods and classes in android studio?
Can I use them like other methods and classes that are not deprecated?
Yes you can use deprecated methods as long as the depreciated method exists in the framework. By deprecating a method the platform developers are trying to tell you that either something is wrong with the method or there is already better way for doing the task.
Depricating a method is like giving a warning to the developers not to use that method as the chances are high that the deprecated methods will be removed in the future release and your application which uses that method may no longer work when your users updates the platform to the latest release.
Of course you can use deprecated methods, they should still work as intended. But you have to be careful, because they could be removed in future versions. So read the comments.
This discussion gives you further information Is it wrong to use Deprecated methods or classes in Java?
The description of the Java Deprecated annotation can be found here http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Deprecated.html
It says
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.
Of course you can use them just that they can be removed in future update which makes it dangerous for your apps and end user.
To get more info read the android documentation on it. https://developer.android.com/reference/java/lang/Deprecated
As a victim, my advice for you is NO because there's a time i used a deprecated method of androidx ViewPager instead of the modern one. Although it worked fine but after keeping the app idle for some days, then it could no longer open if i tried to open it
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.
I'm calling a function through reflection which is available since froyo. How can I mark that this call requires froyo?
There is the annotation #TargetApi(Build.VERSION_CODES.FROYO) but this does just prevent warnings I want to generate a warning if this version does not match.
If there is no way from the SDK site is it possible to write such an annotation? I'm not familiar with that.
Annotations are just metadata and need tools to actually process them, there's no lint check to do the reverse of what you want: indicating that something requires a certain API according to http://tools.android.com/tips/lint-checks.
If you are providing an API, it would be best to document the requirement in the Javadoc of the function so that a caller is aware that that particular call requires Froyo. In the function you should probably throw an exception if this pre condition is not satisfied or handle it in some other way.
Another option, if you are creating a library, is to set the minSdk version of that project to 8 or up.
You also could write your own lint rule that would do a custom check I guess. More details on writing lint rules are here: http://tools.android.com/tips/lint-custom-rules. The downside is that users of your api have to perform some custom setup to activate the rule.
I totally forgot to mention this, but this annotation #RequiresApi was actually introduced in Support Lib v24 back in 2016, after raising a bugreport.