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

15,312

Solution 1

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.

Solution 2

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>

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
15,312

Related videos on Youtube

superjos
Author by

superjos

The nice thing about standards is that you have so many to choose from. - Andrew S. Tanenbaum Ready! #SOreadytohelp

Updated on June 04, 2022

Comments

  • superjos
    superjos almost 2 years

    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).

    • WarrenFaith
      WarrenFaith over 12 years
      Why do you want to disable it? The question is why do you want to ignore a warning instead of fixing it?
    • superjos
      superjos over 12 years
      @WarrenFaith: I'm for the school of fix the warning, not hide it. But in this case I'm using a custom library which of course is not supported by Lint. The library uses the resource in such a way that Lint cannot pick it up and hence it complains about the resource not being used. And I'm not expecting Lint to support this custom library anytime soon.
    • Code Poet
      Code Poet
  • superjos
    superjos over 12 years
    thanks for your feedback, but as I said I want to disable that warning for one particular resource. I do not want to disable it completely.
  • Sander van't Veer
    Sander van't Veer over 12 years
    Ah sorry, misread that. Seen a lot of similar questions lately so I just went with the general answer. In that case I'm afraid I'm not able to help, I haven't experienced this problem myself yet. However if I find anything I'll let you know.
  • superjos
    superjos almost 12 years
    I wouldn't say it's severe, just annoying :) But I think my issue is/was strictly related to how Android Lint processes XML res files and checks them. The mentioned question and bug seem to me to be useful only when disabling warnings coming from the Java-side compile. Not XML & Lint-side.
  • superjos
    superjos about 11 years
    thanks a lot for your input. I'm not able to reproduce the issue at the moment. If anyone is interested in trying out your solution and confirm it works, I can happily set this as the answer!