22

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!

Grégory Jourdan
  • 367
  • 3
  • 12
  • I think it's an interesting question, but it wouldn't surprise me if it's not possible. The only thing I can think of is to add a qualifier to the directory name, but there don't seem to be any qualifiers for this, at least not in the documentation. – wvdz Oct 31 '14 at 21:59

2 Answers2

23

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>
Valter Jansons
  • 3,904
  • 1
  • 22
  • 25
  • Ah thanks for that! Do you have any experience of it working? Because so far it does seem to make a different for me, I'm wondering if it's supposed to be more than just a comment... – Grégory Jourdan Oct 31 '14 at 22:09
  • I haven't had the need to use this myself - just seems like the way the Android source does it. Looking into it now, seems like it's not working for me either... – Valter Jansons Oct 31 '14 at 22:24
  • Well thanks for that. It's unfortunate, I'm getting the vibe it's not supported as of now. – Grégory Jourdan Oct 31 '14 at 22:26
  • Android source build tools seem to parse comments, the application build tools seem to just skip all of them. It does actually seem like it is not possible to have deprecation annotations for resources using the default tools. Will update the answer to represent these findings. – Valter Jansons Oct 31 '14 at 22:44
  • 1
    There is an issue for that on code.google.com but apparently there is no update since August 2015. https://code.google.com/p/android/issues/detail?id=145663 – Eselfar Mar 22 '17 at 10:15
6

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>
SMBiggs
  • 11,034
  • 6
  • 68
  • 83
  • This works, but it's a bit of a hack, and stops you from really managing your TODOs properly... As long as you are managing a deprecation process elsewhere and would be removing this code and the TODO on the next API breaking change, this would be acceptable... – jesses.co.tt Feb 14 '18 at 22:29
  • @jesses.co.tt Yes, it certainly extends the normal definition of TODO to a much larger time-line than we normally think of. I'm curious how you are managing the deprecation process elsewhere. – SMBiggs Feb 19 '18 at 22:27
  • In this case I would probably just prefix the name of the file or resource with 'deprecated'. However, it might not be such a good idea if other projects rely on your library. – Wirling Aug 31 '18 at 06:05