Android Lint - 如何隐藏警告“资源Xxx似乎未被使用”

25

我想要禁用 Android Lint 新版本中的“资源Xxx似乎未被使用”的警告,但只针对某些特定的资源。

对于其他 Lint 警告,我可以利用快速协助(Quick Assist)来禁用它们,其中有三个选项可供选择以禁用该警告,其中一个是针对该特定文件的。

但是这个警告没有显示任何快速协助,它会在 Eclipse 中以通用的黄色警告颜色出现在文件(定义资源的文件)的顶部。

我还尝试了手动编辑 lint.xml 文件,像下面这样:

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

但一直没有成功(我从 Android Lint 参考文献这里获取了 id)。


2
你为什么想要禁用它?问题是,为什么你想要忽略一个警告而不是修复它呢? - WarrenFaith
1
看 https://dev59.com/oF7Va4cB1Zd3GeqPKHtv。我昨天也有同样的问题。 - Code Poet
2
@WarrenFaith:我是修复警告的派别,而不是隐藏它。但在这种情况下,我正在使用一种自定义库,当然,该库不受 Lint 支持。该库以一种使 Lint 无法捕获资源的方式使用资源,因此它会抱怨资源未被使用。而且我并不指望 Lint 很快支持这个自定义库。 - superjos
@WarrenFaith 在某些情况下,资源被使用但 Lint 似乎无法识别它,在我的情况下,我用于调用可绘制对象的标识符是一个变量,我做了这样的事情:resources.getIdentifier("IdConstant$idVariablePart", "drawable", packageName)) 来构建资源 ID,其中 idVariablePart 始终在变化,因此根据需要我可以调用 image_1、image_2、... image_n。 - Salim Mazari Boufares
嗯,是的,Lint 不知道这些变量将具有什么值,因此仅以这种方式使用的资源将始终报告为误报。 - WarrenFaith
显示剩余2条评论
5个回答

29
今天我遇到了这个问题,发现使用 lint 改进你的代码页面非常有帮助。在“配置 XML 中的 lint 检查”部分中,它描述了如何忽略特定资源:

您可以使用tools:ignore属性来禁用XML文件中特定部分的lint检查。为了让这个属性被lint工具识别,必须在您的XML文件中包含以下命名空间值:

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

然后您就可以将tools:ignore="UnusedResources"添加到要忽略的资源中。

非常感谢您的输入。目前我无法重现这个问题。如果有人有兴趣尝试您的解决方案并确认它有效,我很乐意将其设置为答案! - superjos

23

这里是一个例子 lint.xml 文件,可用于忽略特定 id 的警告。该文件应放置在项目的 app 文件夹内。

<?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>

3

我认为您需要的是这个:

进入“首选项” -> “Android” -> “Lint错误检查”

在那里,您可以了解消息的含义,如果需要,关闭警告。


1
谢谢您的反馈,但正如我所说,我想要禁用一个特定资源的警告。我不想完全禁用它。 - superjos
啊,抱歉,我看错了。最近看到很多类似的问题,所以我只是给出了一般性的答案。在这种情况下,恐怕我无法提供帮助,因为我自己还没有遇到过这个问题。不过,如果我找到了什么,我会告诉你的。 - Sander van't Veer

3

来自一个帮助请求:

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

因此,在代码中使用@SuppressLint("UnusedResources")或在XML中使用tools:ignore="UnusedResources"


1

类似于这个问题,你可以尝试使用错误修复。通过使用它,你可以忽略特定文件夹中的警告。 我自己没有测试过它,因为我的情况似乎没有你的那么严重,而且这个错误修复看起来也很复杂。


我不会说这很严重,只是有点烦人 :) 但我认为我的问题/曾经的问题严格与Android Lint如何处理XML资源文件并检查它们有关。在我看来,所提到的问题和错误似乎只在禁用来自Java端编译的警告时有用。而不是XML和Lint方面的。 - superjos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接