1

My Java code contains the following lines:

Window window = getWindow();
View decor = window.getDecorView();
Context context = decor.getRootView().getContext();
Resources resources = context.getResources();

...

ActivityManager.TaskDescription description;

if (android.os.Build.VERSION.SDK_INT >= 28)
    description = new ActivityManager.TaskDescription(
            resources.getString(R.string.app_name), 
            R.drawable.app_icon,
            ContextCompat.getColor(this, R.color.colorTaskBar));
else
    //noinspection deprecation
    description = new ActivityManager.TaskDescription(
            resources.getString(R.string.app_name), 
            getBitmap(this, R.drawable.app_icon),
            ContextCompat.getColor(this, R.color.colorTaskBar));

setTaskDescription(description);

My project build.gradle contains the following lines:

allprojects {
    repositories {
        google()
        jcenter()
    }
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}

Since I have -Xlint:deprecation option in my build.gradle file, I can find where I use deprecation in my code.

When I rebuild my project, I get the following warning:

[deprecation] TaskDescription(String,Bitmap,int) in TaskDescription has been deprecated

...and it points to use of

@Deprecated
public TaskDescription(String label, Bitmap icon, int colorPrimary)

Well, I am aware of that and as you can see in my Java code on top, I check version and I use

public TaskDescription(String label, @DrawableRes int iconRes, int colorPrimary)

for API 28+, as this version of TaskDescription is available just from API 28.


So what am I suppose to do to get rid of the warning? I want to have my code clean and so I want to recode this part to have it 100% correct? What is wrong in my code?

Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • @Andreas - I **don't want to suppress** deprecated import warning in Java. I want to make my code clean! – Ωmega Oct 08 '18 at 21:49
  • I think your only option is to suppress the warning. See for example https://stackoverflow.com/questions/50675122/i-keep-getting-deprecated-api-warning-even-with-correct-check – Magnus Oct 08 '18 at 21:54
  • You can't. You need to call old method for old phones, but the method is marked deprecated, so you know not to use it on new phones. You have to call it, and you're aware of the problem, so you add suppress to tell compiler that you're aware and have taken appropriate steps. Having a `@SuppressWarnings` annotation in your code doesn't make the code *unclean*. – Andreas Oct 08 '18 at 21:55
  • @Andreas - Why a single line `//noinspection deprecation` is not working for this kind of warning? – Ωmega Oct 08 '18 at 22:00
  • @Ωmega Why would it? It's a compilation warning, not an inspection warning. – Andreas Oct 08 '18 at 22:02

0 Answers0