How to suppress specific Lint warning for deprecated Android function?

JJD picture JJD · Jun 13, 2014 · Viewed 26.8k times · Source

I use a version switch to support older Android versions.

int sdk = Build.VERSION.SDK_INT;
if (sdk < Build.VERSION_CODES.HONEYCOMB) {
    ColorDrawable colorDrawable = new ColorDrawable(shapeColor);
    //noinspection deprecation
    viewHolder.shape.setBackgroundDrawable(colorDrawable);
} else {
    viewHolder.shape.setColor(shapeColor);
}

When build the project with Gradle from the command line the following warning is output by Lint:

app/src/main/java/com/example/MyApp/CustomListAdapter.java:92: warning: 
[deprecation] setBackgroundDrawable(Drawable) in View has been deprecated
            viewHolder.shape.setBackgroundDrawable(colorDrawable);
                            ^

Can I annotate the specific line or method to mute the warning (since I do it on purpose)? I do not want to disable all warnings.

Answer

Martin Zeitler picture Martin Zeitler · Mar 5, 2017

I've noticed that the @SuppressLint("deprecated") inline annotation won't be picked up anymore - while @SuppressWarnings("deprecation") is being picked up.

one can disable the Deprecation checks for the Gradle linter with lintOptions within the module-level build.gradle file; while there is no chance to define individual files like that:

android {
    lintOptions {
        disable 'Deprecation'
    }
}

or on can assign one rather detailed lint.xml configuration file with LintOptions:lintConfig (when settings showAll true, it will still show the warnings - no matter the provided XML configuration):

android {
    lintOptions {
        lintConfig file("lint.xml")
        showAll false
    }
}

where one can add individual files, by adding their paths:

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="Deprecation" severity="Error">
        <ignore path="app/src/main/java/com/example/MyApp/CustomListAdapter.java" />
    </issue>
</lint>

The source code of com.android.builder.model.LintOptions might explain, what actually happens there (and confirms about 50% of what I've wrote).

in order to get rid of the inline warnings in Android Studio... that linter appears to be another linter - and these annotations do not affect the linter of the Gradle build (it may be required to use this combined with one of the methods stated above, in order to ignore known deprecated classes and methods):

//noinspection deprecation

update The Android Studio 2.3 release notes mention a new feature:

Lint Baseline: With Android Studio 2.3, you can set unresolved lint warnings as a baseline in your project. From that point forward, Lint will report only new issues. This is helpful if you have many legacy lint issues in your app, but just want to focus on fixing new issues. Learn more about Lint baseline and the new Lint checks & annotations added in this release.

here it's explained, how to create a Lint warnings baseline - which records the detected warnings into an XML file and then mutes them (which is way better than to have the code annotations inline, distributed all over the place); I'd assume, that options lintConfig and baseline should be combine-able (depending on the requirements).

android {
    lintOptions {
        baseline file("lint-baseline.xml")
    }
}