如何检查缺少默认值的android资源? [英] How to check for android resources that are missing default values?

查看:1450
本文介绍了如何检查缺少默认值的android资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am curious about how to find android resources that are missing default values. For example, it is possible to define corner_radius in dimens-sw600dp.xml, without defining it in dimens.xml. This would cause a runtime crash on any device whose smallest width is less than 600 dp.

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

In the above example, it's not obvious that the default value is missing. After aapt runs, you are able to reference the dimension in code and in xml, despite the missing default value. (via R.dimen.corner_radius and @dimen/corner_radius). The error is not discoverable until runtime.

Android Studio包含了一次棉绒检查,以检查是否缺少翻译,但似乎并未检查其他资源类型.在构建时是否有一种简便的方法来检查其他丢失的资源(尺寸,布局等)?

Android Studio includes a lint check for missing translations, but doesn't seem to check for other resource types. Is there an easy way to check for other missing resources (dimens, layout, etc.) at build time?

推荐答案

您现在可以依赖MissingDefaultResource棉绒规则了(找到此处):

You can now depend on the MissingDefaultResource lint rule (found here):

如果仅在带有-land或-en等限定符的文件夹中定义资源, 并且基本文件夹中没有默认声明(布局或值等), 那么如果在设备上访问该资源的应用程序将崩溃,则该应用程序将崩溃 设备处于缺少给定限定符的配置中.

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.

在特殊情况下,不必在基本文件夹中指定可绘制对象. 如果在密度文件夹(例如drawable-mdpi)中存在匹配的图像 将被使用和缩放.但是请注意,如果仅在 像drawable-en-hdpi这样的文件夹,该应用将在非英语语言环境中崩溃.

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.

在某些情况下,您拥有资源,例如-fr drawable, 仅从其他具有相同限定词的资源中引用 (例如-fr样式),它本身具有安全的后备.但是,这仍然 使某人可能意外地引用了可绘制对象和 崩溃,因此在基本文件夹中创建默认的虚拟后备更为安全. 或者,您可以通过添加来抑制此问题 元素上的tools:ignore ="MissingDefaultResource".

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.

(这种情况经常发生在字符串翻译中,在这种情况下, 删除代码和相应的资源,但是忘记删除 翻译.该场景有一个专用的问题ID,其中包含ID 额外翻译.)

(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资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆