如何禁用已弃用Android功能的特定Lint警告? [英] How to suppress specific Lint warning for deprecated Android function?

查看:1021
本文介绍了如何禁用已弃用Android功能的特定Lint警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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);
}

命令行使用Gradle构建项目 Lint输出以下警告:

  app / src / main / java / com / example / MyApp / CustomListAdapter.java :92:警告:
[deprecation] View中的setBackgroundDrawable(Drawable)已被弃用
viewHolder.shape.setBackgroundDrawable(colorDrawable);
$

我可以注释特定的行或方法来将警告静音(因为我它是故意的)? 所有警告

新:不确定关于Android Studio,但是,要从此行删除此警告,您可以使用:

  // noinspection deprecation 

这将删除下一行中的警告。
例如:

  // noinspection deprecation 
e.setBackgroundDrawable(editTextDrawable);

它不会显示错误。 然而,正如@JJD所说,这仍然会向控制台输出警告。但至少你可以有一个很好的无错代码,例如对于Git可能很有用。并且,这可以避免 @SupressWarnings 这个问题,它会忽略该方法中的所有警告。因此,如果您有某些您不知道的内容, @SupressWarnings 会隐藏它,您将不会收到警告。这是 // noinspection


的优点

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.

解决方案

Just something new: Not sure about Android Studio, but, to remove this warning from this line, you can use:

//noinspection deprecation

This removes the warning from the next line. E.g:

//noinspection deprecation
e.setBackgroundDrawable(editTextDrawable);

It won't show an error. However, as @JJD said, this still outputs the warning to the console. But at least you can have a nice error-less code which can be useful like for Git for example. And, this prevents the problem with @SupressWarnings, which is it ignores all warnings in the method. So if you have something deprecated that you are not aware of, @SupressWarnings will hide it and you will not be warned. That is the advantage of the //noinspection

这篇关于如何禁用已弃用Android功能的特定Lint警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆