为什么 API 19 台设备(以及 API 21 台以下的其他设备)需要 vectorDrawables.useSupportLibrary 加载图标?

Why do API 19 devices (and other devices below API 21) need vectorDrawables.useSupportLibrary to load icons?

这个问题我已经有一段时间了,但我一直想知道:为什么我需要申报

vectorDrawables.useSupportLibrary = true

我的 API 19 设备而不是我的 API 29 设备?

它们都加载了我导入的相同图标,但为什么 API 19 台设备需要声明其他设备,这会给您带来编译时错误。

原因是在 API 21 (Android 5.0 Lollipop) 中添加了对矢量绘图的原生支持(如 Android 框架中内置的)。

vectorDrawables.useSupportLibrary 选项启用 backward-compatibility feature in the Android support library (now known as AndroidX AppCompat) that allows it to load vector drawables at Runtime via the VectorDrawableCompat and AnimatedVectorDrawableCompat 类。

也可以配置 Android Gradle 插件以在构建时将矢量图像转换为 PNG 文件,以便在 API 21 之前的设备上使用,但这会失败首先使用矢量绘图的许多好处,例如更小的应用程序大小。