Android 抑制特定属性的 UnusedAttribute 警告

标签 android android-lint

我的整个项目中都有一些针对“UnusedAttribute”的 linter 警告。

  • 属性 elevation 仅用于 API 级别 21 及更高级别(当前最低为 16)
  • 属性 breakStrategy 仅用于 API 级别 23 及更高级别(当前最低为 16)
  • 属性 hyphenationFrequency 仅用于 API 级别 23 及更高级别(当前最小值为 16)
  • 属性 letterSpacing 仅用于 API 级别 21 及更高级别(当前最小值为 16)

我知道我可以抑制所有属性的警告。

tools:ignore="UnusedAttribute"

lintOptions {
    disable 'UnusedAttribute'
}

但是,我只想抑制特定属性的警告。我尝试执行以下操作但没有成功。

tools:ignore="UnusedAttribute:elevation"

lintOptions {
    disable 'UnusedAttribute:elevation'
}

我在文档中找不到对此的任何提及 here , here , here , 或 here .有什么办法吗?

最佳答案

为此,您已通过 lint.xml 配置 lint 并使用正则表达式 see docs for details .让我们一步一步来。

首先,指定您将使用文件配置。 在你的 build.gradle

中有类似的东西
lintOptions {
    lintConfig file("../config/lint/lint.xml")
}

在您的情况下,路径会有所不同。

其次,使用正则表达式排除特定属性。这是 lint.xml

的示例
<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="UnusedAttribute">
        <ignore regexp="elevation"/>
        <ignore regexp="breakStrategy"/>
    </issue>
</lint> 

另一种方法是为每次使用添加 tools:targetApi="n"。其中“n”是属性首次出现的版本。

关于Android 抑制特定属性的 UnusedAttribute 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943008/

相关文章:

java - 如何从代码中使用 Google 翻译 API

android - 如何判断android设备是否已关闭

java - Android IllegalAccessError java.util.concurrent.TimeUnit.toHours

android - Android 中的其他 lint 规则

ubuntu上的android lint与mac不同

android - 在构建 android studio 项目时运行 lint

android - 当我将 lint.xml 文件从模块目录移动到项目目录时找不到

Java Android getApplicationContext() 从上一个类返回一个 null 对象

android - 加载图片出现OutOfMemoryException

安卓 Lint 警告 : "Redundant array creation for calling varargs method"