Search code examples
androidandroid-lint

Android Suppress UnusedAttribute Warning For Specific Attributes


I have a few linter warnings for "UnusedAttribute" throughout my project.

  • Attribute elevation is only used in API level 21 and higher (current min is 16)
  • Attribute breakStrategy is only used in API level 23 and higher (current min is 16)
  • Attribute hyphenationFrequency is only used in API level 23 and higher (current min is 16)
  • Attribute letterSpacing is only used in API level 21 and higher (current min is 16)

I know that I can suppress the warning for ALL of the attributes.

tools:ignore="UnusedAttribute"

or

lintOptions {
    disable 'UnusedAttribute'
}

However, I only want to suppress the warning for specific attributes. I tried to do the following without success.

tools:ignore="UnusedAttribute:elevation"

or

lintOptions {
    disable 'UnusedAttribute:elevation'
}

I can't find any mention of this in the docs here, here, here, or here. Is there any way to do this?


Solution

  • For that you have configure lint via lint.xml and use regexp see docs for details. Let's go step by step.

    Firstly, specify that you will use file configuration. Something like that in your build.gradle

    lintOptions {
        lintConfig file("../config/lint/lint.xml")
    }
    

    In your case path will be different.

    Secondly, use regexp to exclude specific attributes. Here is example of lint.xml

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

    Another way to do it is to add tools:targetApi="n" for each usage. Where "n" is version where attribute first appeared.