I have an android library for which I would like to mark some resources as deprecated. Drawables, dimensions, durations... I read somewhere that I could add deprecated="deprecated" to the resource definition but it doesn't seem to do anything.
In Android R you can see things such as
@java.lang.Deprecated
public static final int autoText = 16843114;
I'm trying to produce something similar by editing durations.xml, values.xml, or even public.xml...
Thanks!
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.
An element may be deprecated for any of several reasons, for example, its usage is likely to lead to errors; it may be changed incompatibly or removed in a future version; it has been superseded by a newer, usually preferable alternative; or it is obsolete.
It's better to use the new method names. That way you can eventually get rid of your compatibility class when you raise your min sdk version to 23, without having to change any code other than your imports.
In Android deprecation usually means “ We will continue to support this, but we think there are better solutions ”. Most of the time features are deprecated rather than immediately removed, to provide backward compatibility, and to give programmers time to bring affected code into compliance with the new standard.
1 Click the target app module in the Project window (while in either the Android or Project view), and then select File > New > Android resource file. 2 Fill in the details in the dialog: File name: Type the name for the XML file (does not require the .xml suffix). ... 3 Once you've added all the qualifiers you want, click OK.
If you use a new resource qualifier, but maintain code compatibility with older versions of Android, then when an older version of Android runs your app, it will crash if you don't provide default resources, because it cannot use the resources named with the new qualifier.
Each time a resource is requested, Android checks for alternative resource directories that contain the requested resource file, then finds the best-matching resource (discussed below).
Short answer: Not possible.
@deprecated
tags are used by the Android source inside comments as it can be seen in fw/base core/res/values/attrs.xml (see line 3427 for autoText as referred to in the original post), in fw/base core/res/values/public.xml and in other places throughout the project, but it appears that the build tools used for building applications simply skip all comments meaning the annotiation tags get skipped as well in the process making this method fail.
Example usage of deprecation annotations based on Android source:
<!-- {@deprecated Use foobar instead.} -->
<string name="foo">abc</string>
<string name="foobar">abcd</string>
What I do is simply add a TODO statement. It does not flag the resource, but the doc keyword TODO does show up in Git and as marks in the gutter. Hopefully those warnings will be enough to keep you and others from using it.
But not always. As we know, @Deprecated is essentially a stop-gap until something is really removed for good and the code is cleaned up properly.
Here's an example of a shape.xml file that I no longer use (and want to @Deprecate).
<?xml version="1.0" encoding="utf-8"?>
<!--
Border around card elements. For now this is used only within the
cards of the Questionnaire RecyclerView.
TODO: This file is obsolete. Use CardView instead.
-->
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="@dimen/tiny_card_margin"
android:color="@color/colorPrimaryDark" />
<solid
android:color="@android:color/white" />
<corners
android:radius="4dp" />
</shape>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With