android - 如何检查缺少默认值的 android 资源?

标签 android lint

我很好奇如何找到缺少默认值的 android 资源。例如,可以在 dimens-sw600dp.xml 中定义 corner_radius,而无需在 dimens.xml 中定义它。这会在最小宽度小于 600 dp 的任何设备上导致运行时崩溃。

在上面的示例中,缺少默认值并不明显。 aapt 运行后,您可以在代码和 xml 中引用维度,尽管缺少默认值。 (通过 R.dimen.corner_radius@dimen/corner_radius)。错误直到运行时才被发现。

Android Studio 包含对缺失翻译的 lint 检查,但似乎不检查其他资源类型。有没有一种简单的方法可以在构建时检查其他缺失的资源(维度、布局等)?

最佳答案

您现在可以依赖 MissingDefaultResource lint 规则(找到 here ):

If a resource is only defined in folders with qualifiers like -land or -en, and there is no default declaration in the base folder (layout or values etc), then the app will crash if that resource is accessed on a device where the device is in a configuration missing the given qualifier.

As a special case, drawables do not have to be specified in the base folder; if there is a match in a density folder (such as drawable-mdpi) that image will be used and scaled. Note however that if you only specify a drawable in a folder like drawable-en-hdpi, the app will crash in non-English locales.

There may be scenarios where you have a resource, such as a -fr drawable, which is only referenced from some other resource with the same qualifiers (such as a -fr style), which itself has safe fallbacks. However, this still makes it possible for somebody to accidentally reference the drawable and crash, so it is safer to create a default dummy fallback in the base folder. Alternatively, you can suppress the issue by adding tools:ignore="MissingDefaultResource" on the element.

(This scenario frequently happens with string translations, where you might delete code and the corresponding resources, but forget to delete a translation. There is a dedicated issue id for that scenario, with the id ExtraTranslation.)

关于android - 如何检查缺少默认值的 android 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45662406/

相关文章:

c++ - "{ }"的 Visual Studio 代码格式

Android:Lint 说 - 布局文件中引用的类,在项目或库中找不到

java - EditText 的空值

android - 如何从 Jetpack Compose Modifier 引用 xml 样式?

android - 如何在Flutter上禁用或处理最小化应用程序功能

Android Wear - 通知 - setContentAction() 不工作

python - 无法完全禁用 python linting Pylance VSCODE

android - 找不到 lint-checks.jar

android - 在 android 中加载我的 Cordova 应用程序时显示 URL?

c++ - 使用模板时出现 PC Lint 错误