Recent Changes‎ > ‎

    Ignoring Lint Warnings

    posted Feb 15, 2012, 3:34 PM by Tor Norbye
    In addition to adding a lot of new lint checks, the new version of Lint in Tools 17 adds improved support for ignoring issues that you have manually verified and want to suppress.

    The easiest way to do it is probably via the Lint toolbar window:
    These buttons let you (from right to left) :
    • Disable the specific issue check completely
    • Disable the specific issue in this project
    • Disable the specific issue in this file
    • Disable this specific issue by adding a suppress annotation or attribute
    If you disable an issue in a project or file, this gets recorded in a special file named lint.xml in the project root directory. You can check this file into version control and share it with the rest of your team. For more details on this, see the Suppressing Lint Warnings document. This facility works in Tools 16. The remainder of this blog post deals with the new suppress support coming in Tools 17.

    The first button lets you ignore a specific issue encountered in a Java or XML file.

    If the error is in a Java file, it adds a @SuppressLint annotation. In addition to the button in the lint view, you can also do this directly while editing Java files by just invoking Quick Fix when the cursor is on a line containing a lint warning:

    As you can see, this offers to add the @SuppressLint annotation in three different places: the surrounding method and class contexts. The annotation will suppress any lint warnings of the given type for the scope that the annotation is annotating. (For this specific issue, "NewApi", there is an additional available annotation, @TargetApi, so the quickfix offers it. See the Lint API Check blog entry for details.)

    The annotation takes a String parameter which lists the id of the issue to suppress. While the quickfix automatically supplies it, if you are editing manually you can also find the id listed at the end of each lint error message in the command line output as well as in the HTML reports. 

    To suppress more than one issue, supply a String array like this:
    @SuppressLint({"NewApi","StringFormatInvalid"})
    and you can also use @SuppressLint("all") to suppress everything. (The new annotations is provided in the new annotations.jar file which ships with Tools 17).

    In XML files, you can use the new tools:ignore attribute in a similar way to suppress errors (see the third quickfix in the list which offers the suppress attribute):
    This will modify the code as follows:
    • If necessary, it adds the namespace xmlns:tools="http://schemas.android.com/tools"
    • If adds the attribute tools:ignore="HardcodedText" on the given element. 
    Just as with Java annotations, this attribute is inherited, so you can specify it on the root of an XML document to suppress all warnings of the given type within the document. And just like the annotation, you can supply a comma separated list of issues (or "all") to suppress the given list of issues.

    Note that the new tools namespace is special. AAPT in Tools 17 will deliberately skip these attributes, so they do not end up in the compiled XML shipped with your application, so there is no cost at runtime.
    Comments