25

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

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
superjos
  • 12,189
  • 6
  • 89
  • 134
  • 2
    Why do you want to disable it? The question is why do you want to ignore a warning instead of fixing it? – WarrenFaith Dec 21 '11 at 09:25
  • 1
    See http://stackoverflow.com/questions/8575834/warnings-in-xml-resources. I had the same problem yesterday. – Code Poet Dec 21 '11 at 09:26
  • 2
    @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. – superjos Dec 21 '11 at 09:49
  • @ManishGupta: as I also commented to Sander, I want to leave the warning as a general rule. I'd like to disable it just for one/some resources. – superjos Dec 21 '11 at 09:50
  • @WarrenFaith In some cases the resource is used but Lint seems to not rencogniez it, in my case the identifier I use to call the drawables is a variable, I do something like this: resources.getIdentifier("IdConstant$idVariablePart", "drawable", packageName)) to build the resource id, where idVariablePart is always changing, so I could call image_1, image_2, ... image_n depending on my needs. – Salim Mazari Boufares Apr 15 '21 at 12:23
  • Well, yeah, Lint can't know what value those variables will have so resources that are used only this way will always be reported as a false positive – WarrenFaith Apr 16 '21 at 07:28
  • @WarrenFaith A library containing resources that may or may not be used by the current project is a good example of why one may want to suppress this warning (perhaps there's a way to exclude the module?) – JCutting8 Jun 15 '23 at 02:33

5 Answers5

29

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.

meisteg
  • 678
  • 8
  • 15
  • 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! – superjos Mar 18 '13 at 18:31
23

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>
Stephen Niedzielski
  • 2,497
  • 1
  • 28
  • 34
Eugene Popovich
  • 3,343
  • 2
  • 30
  • 33
3

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.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
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.

Sander van't Veer
  • 5,930
  • 5
  • 35
  • 50
  • 1
    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. – superjos Dec 21 '11 at 09:47
  • 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. – Sander van't Veer Dec 21 '11 at 09:49
1

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.

Community
  • 1
  • 1
Michi
  • 681
  • 1
  • 7
  • 25
  • 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 May 24 '12 at 09:05