如何使应用程序的矢量可绘制对象覆盖库的矢量可绘制对象?

How to make an application's vector drawable override a library's vector drawable?

我有一个库项目和一个应用程序项目。除了其他内容之外,库项目还包含一些 png 和一些矢量绘图。现在我可以通过给它相同的名称轻松覆盖应用程序项目中的 png 可绘制对象,并且它会正确显示。不过,这不适用于矢量可绘制对象:该应用程序始终显示库中定义的矢量可绘制对象,包括 Android 4 和 5。该应用程序永远不会显示应用程序项目的矢量可绘制对象。

Google claims an application's resources always have priority over a library's resources:

Since the tools merge the resources of a library module with those of a dependent application module, a given resource ID might be defined in both modules. In this case, the tools select the resource from the application, or the library with highest priority, and discard the other resource. As you develop your applications, be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.

但正如我所说,对于矢量绘图,出于某种原因,情况正好相反。知道我可以做些什么来确保像普通可绘制对象和其他资源一样覆盖矢量可绘制对象吗?

更新Resolved in support library v23.2!现在无事可做:)

构建库项目时,VectorDrawable 为每个密度创建 PNG 文件并将原始 VectorDrawable 放在 drawable-anydpi-v21.

如果您将 VectorDrawable 放在应用程序的 drawable-anydpi-v21 中,那么它将覆盖您的库项目中的可绘制对象。这似乎是一个错误,应该创建一个新问题(如果尚不存在的话)。


注意:这不会替换库中生成的 PNG 文件。您还需要将它们添加到您的应用中以覆盖它们。

基于 Jared Rummler 的回答中的 "NOTE",由于这不适合发表评论,我将 post 在这里为难以找到的人提供一个小教程生成的 PNG 文件:

您可以在 build/intermediates/res/merged/debug 中找到包含所需文件的文件夹,或者,如果您使用的是产品口味 build/intermediates/res/merged/<flavor>/debug。现在,将 PNG 文件复制到应用程序的最简单方法是完全复制它们的文件夹,它们是:

drawable-ldpi-v4
drawable-mdpi-v4
drawable-hdpi-v4
drawable-xhdpi-v4
drawable-xxhdpi-v4
drawable-xxxhdpi-v4

作为最后一个繁琐的步骤,您应该删除不需要的所有文件,即那些不是从矢量可绘制对象生成的文件。如果您使用的是 VCS,只需添加所需的 PNG 即可轻松完成此操作。这会将所有冗余文件置于未版本化文件下。

你去吧,加上 drawable-anydpi-v21 现在有 7 个额外的文件夹只是因为这个愚蠢的错误:(

更新Resolved in support library v23.2!到今天为止,终于不用再做上面的任何事情了。只需确保在任何地方都使用 app:srcCompat 而不是 android:src。