Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle prints warning although SuppressWarnings is set

I have an android app imported to Android Studio. It has some Java libraries included. Everything works so far.

The following method:

            @SuppressWarnings("deprecation")
        private Drawable getDrawable() {
            if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH)
                return activity.getResources().getDrawable(R.drawable.separator_gradient, activity.getTheme());
            else
                return activity.getResources().getDrawable(R.drawable.separator_gradient);
        }

prints always a depreciation warning:

:androidAnsatTerminal:compileDebugJava
C:\...\src\main\java\de\ansat\terminal\activity\widgets\druckAssistent\FahrkartenArtSelector.java:131: warning: [deprecation] getDrawable(int) in Resources has been deprecated
                return activity.getResources().getDrawable(R.drawable.separator_gradient);
                                               ^

1 warning

This is not the only @SuppressWarnings("deprecation") in my project. In other places the warning is not printed...

For example:

    @SuppressWarnings("deprecation")
private void setBackgroundToNull(ImageView imgRight) {

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        imgRight.setBackgroundDrawable(null);
    } else {
        imgRight.setBackground(null);
    }
}

From my AndroidManifest:

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="21" />

How can I get rid of this warning message? I don't want to turn of warnings globally or something.

EDIT: If I just call getDrawable with Theme parameter, of cause this happens on SDK15 device:

java.lang.NoSuchMethodError: android.content.res.Resources.getDrawable
        at de.ansat.terminal.activity.widgets.druckAssistent.FahrkartenArtSelector$3.getDrawable(FahrkartenArtSelector.java:128)
like image 884
Markus Kreth Avatar asked Nov 10 '22 16:11

Markus Kreth


1 Answers

I have found that for reasons unknown this code triggers the warning:

private Drawable getShadow(Context context) {
    @SuppressWarnings("deprecation")
    final Drawable drawable = context.getResources().getDrawable(R.drawable.shadow_top);
    return drawable;
}

While this equivalent code does not:

private Drawable getShadow(Context context) {
    final int resId = R.drawable.shadow_top;
    @SuppressWarnings("deprecation")
    final Drawable drawable = context.getResources().getDrawable(resId);
    return drawable;
}

Extracting a helper method also seems to work and solved the problem for me:

@SuppressWarnings("deprecation")
private Drawable getDrawable(Context context, int resId) {
    return context.getResources().getDrawable(resId);
}
like image 60
devconsole Avatar answered Nov 14 '22 21:11

devconsole