Android Lint – How to hide a warning 'The resource Xxx appears to be unused'-ThrowExceptions

Exception or error:

I’d like to disable the (new) Android Lint warning ‘The resource Xxx appears to be unused’ for some specific resources.

For other Lint warning I was ableto take advantage of Quick Assist, which showed 3 choices to disable the warning, one of those was for that particular file.

But this warning does not show any Quick Assist, it appears in Eclipse with the generic yellow warning color on top of a file (the one defining the resource).

I tried also manually editing the lint.xml file like in the following:

<lint>
  <issue id="UnusedResources">
    <ignore path="res\layout\my_layout.xml" />
  </issue>
<lint>

but with no luck (I picked up the id from an Android Lint reference here).

How to solve:

I had this problem today and found the Improving Your Code with lint page to be very helpful. In the “Configuring lint checking in XML” section, it describes how to ignore specific resources:

You can use the tools:ignore attribute to disable lint checking for specific sections of your XML files. In order for this attribute to be recognized by the lint tool, the following namespace value must be included in your XML file:

namespace xmlns:tools=”http://schemas.android.com/tools”

Then you are able to add tools:ignore="UnusedResources" to the resources to ignore.

###

Here is an example lint.xml file to ignore warnings for specific ids. The file should be placed inside app folder of your project.

<?xml version="1.0" encoding="UTF-8"?>
<lint>

    <!-- Ignore the UnusedResources issue for the given ids -->
    <issue id="UnusedResources">
        <ignore regexp="ga_trackingId|google_crash_reporting_api_key" />
    </issue>
</lint>

###

I think you’re looking for this:

Go to Preferences -> Android -> Lint Error Checking

There you can read about the what the message means and if required,
turn off the warning.

###

Similar to this question you can try this bugfix. With this you can ignore Warnings from a specific folder.
I haven’t tested it myself, because my situation isn’t as severe as yours appears to be and because also the bugfix seems complicated to use.

###

From a help:

Suppressing Warnings and Errors Lint errors can be suppressed in a
variety of ways:

  1. With a @SuppressLint annotation in the Java code
  2. With a tools:ignore attribute in the XML file
  3. With a //noinspection comment in the source code
  4. With ignore flags specified in the build.gradle file, as explained below
  5. With a lint.xml configuration file in the project
  6. With a lint.xml configuration file passed to lint via the –config flag
  7. With the –ignore flag passed to lint.

To suppress a lint warning with an annotation, add a
@SuppressLint(“id”) annotation on the class, method or variable
declaration closest to the warning instance you want to disable. The
id can be one or more issue id’s, such as “UnusedResources” or
{“UnusedResources”,”UnusedIds”}, or it can be “all” to suppress all
lint warnings in the given scope.

To suppress a lint warning with a comment, add a //noinspection id
comment on the line before the statement with the error.

To suppress a lint warning in an XML file, add a tools:ignore=”id”
attribute on the element containing the error, or one of its
surrounding elements. You also need to define the namespace for the
tools prefix on the root element in your document, next to the
xmlns:android declaration:
xmlns:tools=”http://schemas.android.com/tools”

To suppress a lint warning in a build.gradle file, add a section like
this:

android {
lintOptions {
disable ‘TypographyFractions’,’TypographyQuotes’
} }

Here we specify a comma separated list of issue id’s after the disable
command. You can also use warning or error instead of disable to
change the severity of issues.

To suppress lint warnings with a configuration XML file, create a file
named lint.xml and place it at the root directory of the module in
which it applies.

The format of the lint.xml file is something like the following:

<!-- Disable this given check in this project -->
<issue id="IconMissingDensityFolder" severity="ignore" />

<!-- Ignore the ObsoleteLayoutParam issue in the given files -->
<issue id="ObsoleteLayoutParam">
    <ignore path="res/layout/activation.xml" />
    <ignore path="res/layout-xlarge/activation.xml" />
    <ignore regexp="(foo|bar).java" />
</issue>

<!-- Ignore the UselessLeaf issue in the given file -->
<issue id="UselessLeaf">
    <ignore path="res/layout/main.xml" />
</issue>

<!-- Change the severity of hardcoded strings to "error" -->
<issue id="HardcodedText" severity="error" /> </lint>

To suppress lint checks from the command line, pass the –ignore flag
with a comma separated list of ids to be suppressed, such as: $ lint
–ignore UnusedResources,UselessLeaf /my/project/path

For more information, see
http://g.co/androidstudio/suppressing-lint-warnings

So use @SuppressLint("UnusedResources") in code or tools:ignore="UnusedResources" in XML, for instance.

Leave a Reply

Your email address will not be published. Required fields are marked *