Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress specific Lint warning for deprecated Android function?

Tags:

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.

like image 607
JJD Avatar asked Jun 13 '14 15:06

JJD


People also ask

How do I turn off deprecation warning Android?

You can use the @SuppressWarnings annotation to suppress warnings whenever that code is compiled. Place the @SuppressWarnings annotation at the declaration of the class, method, field, or local variable that uses a deprecated API.

How do I stop a lint warning?

@SuppressLint("NewApi") Just invoke the quickfix for the warning, and one of the options will be to ignore the issue with an annotation; this will annotate either the local declaration, or the method, or the class, or the field, depending on the location of the error.

How do you stop deprecation warning in Kotlin?

Instead of a single statement, you can also mark a function, a class or a file ( @file:Suppress("DEPRECATION") in its beginning) with the annotation to suppress all the deprecation warnings issued there. In IntelliJ IDEA this can also be done through Alt + Enter menu with caret placed on code with deprecation warning.


2 Answers

Case is important, use the following either inline or class-wide:

@Suppress("DEPRECATION") 

This is in Kotlin.

like image 110
Codeversed Avatar answered Dec 14 '22 16:12

Codeversed


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")     } } 
like image 38
Martin Zeitler Avatar answered Dec 14 '22 17:12

Martin Zeitler