javaandroidmaterial-components-android

Deprecation of Drawable.getOpacity()


I am getting a deprecation warning for Drawable.getOpacity().

...\progressbar\MaterialProgressDrawable.java:304: warning: [deprecation] getOpacity() in Drawable has been deprecated
    public int getOpacity() {
               ^
1 warning

This class (MaterialProgressDrawable) extends Drawable and implements Animatable. Since getOpacity() is abstract in the parent class (@Deprecated public abstract @PixelFormat.Opacity int getOpacity();) I can't remove this method altogether either. What is the way out?


Solution

  • What is the way out?

    Well, since the javadoc implies that the method is not used anymore, one solution would be to implement it as follows:

    @SuppressWarnings("deprecation")
    public int getOpacity() {
        throw new UnsupportedOperationException("getOpacity is deprecated");
    }
    

    Well, I'm running ./gradlew lint so it should go in lint.xml with severity=ignore. But I don't want to suppress every deprecation warning either ...

    I'm not sure it is possible, but you could try reading:

    (It looks like the best you could do is suppress all deprecation warnings in specific classes or packages.)

    Alternatively, you can selectively suppress specific deprecation warnings at the site where they are triggered; i.e. by adding @SuppressWarnings("deprecation") annotations as shown above. For a once-off suppression this would be the simplest way.

    If the deprecation warnings are coming from the Gradle lint task rather than the javac compiler, then you could use //noinspection comments instead of @SuppressWarnings annotations. But according to the documentation, @SuppressWarnings annotations are respected by the lint task.