Android为特定属性抑制UnusedAttribute警告

mpk*_*uth 7 android android-lint

在整个项目中,我有一些"UnusedAttribute"的催眠警告.

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

我知道我可以禁止所有属性的警告.

tools:ignore="UnusedAttribute"
Run Code Online (Sandbox Code Playgroud)

要么

lintOptions {
    disable 'UnusedAttribute'
}
Run Code Online (Sandbox Code Playgroud)

但是,我只想抑制特定属性的警告.我试图做以下事情但没有成功.

tools:ignore="UnusedAttribute:elevation"
Run Code Online (Sandbox Code Playgroud)

要么

lintOptions {
    disable 'UnusedAttribute:elevation'
}
Run Code Online (Sandbox Code Playgroud)

我在这里,这里,这里这里的文档中找不到任何提及.有没有办法做到这一点?

MyD*_*Tom 7

为此,您可以通过配置lint lint.xml并使用regexp 来了解详细信息。让我们一步一步走。

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

lintOptions {
    lintConfig file("../config/lint/lint.xml")
}
Run Code Online (Sandbox Code Playgroud)

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

其次,使用regexp排除特定属性。这是例子lint.xml

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="UnusedAttribute">
        <ignore regexp="elevation"/>
        <ignore regexp="breakStrategy"/>
    </issue>
</lint> 
Run Code Online (Sandbox Code Playgroud)

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