4

TL;DR: Can Android's @SuppressWarnings("deprecation"), or similar, be applied to a single statement, rather than an entire method?

I have a method myMethod that uses deprecated method ImageView.setAlpha():

public void myMethod(ImageView icon) { icon.setAlpha(0xFF); }

To avoid use of the deprecated method in Jelly Bean and subsequent releases, whilst providing backward compatibility, method myMethod can be rewritten as follows:

public void myMethod(ImageView icon) { 
  if (android.os.Build.VERSION.SDK_INT 
                                >= android.os.Build.VERSION_CODES.JELLY_BEAN)
     icon.setImageAlpha(0xFF);
  else 
     icon.setAlpha(0xFF);
}

Moreover, command line warnings generated by Gradle/Lint can be suppressed by prepending method myMethod with @SuppressWarnings("deprecation"). But, that suppresses all deprecated warnings generated by method myMethod, rather than just the single warning generated by statement icon.setAlpha(0xFF).

Can I suppress the single deprecation warning generated by statement icon.setAlpha(0xFF), rather than suppressing all deprecation warnings generated by method myMethod?

user2768
  • 794
  • 8
  • 31

1 Answers1

2

You can achieve it as follows if you are using Android Studio:

 //noinspection deprecation
 icon.setAlpha(0xFF);

For your future reference: The correct format can be easily generated in Android-Studio as follows:

  1. Press alt+Enter on the statement which is throwing warning.
  2. Then Expand the option Deprecated API usage options
  3. Click on Suppress for statement

Following Image shows the process:

enter image description here

In your case since you are not using IDE:

  • Unfortunately there is no direct way to achieve it at method body level. Since you have already moved the deprecated part in individual method and marked it with @SuppressWarnings this should be best you can achieve.
  • There are some posts which claim to have solved it by using fully qualified class name instead of import. But looks like the issue has been fixed in Java 9. Since current popular java version for android is 8.x this should help in short term. You can refer this SO for more details
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • 1
    That doesn't work, I get `warning: [deprecation] setAlpha(int) in ImageView has been deprecated`. Perhaps I need to do something else since I work from the command line, rather than an IDE (e.g., Android-Studio). – user2768 May 10 '18 at 11:10