问题 在build.gradle中使用android支持库23.2崩溃app(XmlPullParserException:XmlPullParserException:无效的drawable标签向量)


当使用支持库v23.1.1插件并在Android 4.4.4(API 19)下运行时,该应用程序运行正常:

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v4:23.1.1'

但是当我使用它构建它时 较新版本(23.2) android支持库崩溃:

XmlPullParserException: Binary XML file line #17: invalid drawable tag vector

当应用想要使用简单的自定义视图来充气警报对话框时会发生这种情况。自定义视图使用包含a的XML文件 <shape> 标记作为其drawable之一。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

<solid
    android:color="@android:color/white" >
</solid>

<corners
    android:radius="5dp">
</corners>

<stroke android:width="1dp" android:color="@color/AppColor" />

</shape> 

虽然我无法在崩溃日志中明确指出这个基于SVG的drawable,但我的猜测是因为谷歌引入了支持 VectorDrawables,它以某种方式与我的应用程序发生冲突。

任何有关如何查明罪魁祸首的提示或输入都表示赞赏。


2569
2018-02-25 07:25


起源

因为我怀疑旧的支持库为.png文件创建了.png文件 <shape> drawables工作正常,但新的创建 svg 有文件的 vector 标签,以某种方式API 19无法处理它。我甚至尝试按照他们的建议打开和关闭png创建 vectorDrawables.useSupportLibrary = true 但这没有帮助。 - listboss
您会注意到这个新属性仅存在于Gradle插件的2.0版本中。如果您使用的是Gradle 1.5,则使用// Gradle Plugin 1.5 android {defaultConfig {generatedDensities = []} //这是由2.0+ Gradle Plugin aaptOptions {additionalParameters“--no-version-vectors”为您处理的}} android-developers.blogspot.ru/2016/02/... - Pavel Biryukov
我在2.0版本 - listboss
您在哪个目录中放置了SVG? - Igor Ganapolsky


答案:


我遇到了类似的问题。我得到了同样的错误,因为我在设置ImageView的源代码时仍然使用android:src属性而不是app:srcCompat

在您引用矢量drawable的任何地方更改此属性都可以解决您的问题。

旧:

<ImageView
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:src="@drawable/ic_add" />

新:

<ImageView
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  app:srcCompat="@drawable/ic_add" />

12
2018-02-25 17:44



app:srcCompat只能用于基于图像的视图,我使用基于Button的视图,根据博客文章,它不支持app:srcCompat - listboss
给出“属性缺少Android名称空间前缀”错误 - jyomin
来自@theMichaelScott答案的答案也证明可以使用FloatingActionButton以及<android.support.design.widget.FloatingActionButton app:srcCompat =“@ drawable / ic_gps_fixed_24dp”/> - RobLabs
节省了我的时间! :) - Vivek Solanki


原来有两个不同的问题:

  • 我正在使用平原 RadioButton 在我的XML文件中。将其更改为 android.support.v7.widget.AppCompatRadioButton 解决了这个问题。
  • 其次,正如博客文章所暗示我必须将一些可绘制的内容包装进去 容器 drawables所以支持库可以在进行基于矢量的绘图时使用它们。例如,在我的一个自定义按钮中,我保存了基于矢量的背景 dialog_bg.xmlbutton_round_bg.xml 和 button_round_bg_2.xml

    为了解决这个问题,我必须将它们放在其中一个容器中(例如 州名单插页 要么 LevelList)。这个包装器被保存到一个名为的文件夹中 vector_wrapper.xlm

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/dialog_bg"
        android:maxLevel="1"
        android:minLevel="1" />
    <item
        android:drawable="@drawable/button_round_bg"
        android:maxLevel="2"
        android:minLevel="2" />
    <item
        android:drawable="@drawable/button_round_bg_2"
        android:maxLevel="3"
        android:minLevel="3" />
</level-list>

现在我要改变我的自定义按钮的背景说

<customButton
    android:background="@drawable/vector_wrapper"
</customButton>

由于包装器现在有三个drawable,我可以根据我的需要在Java代码中选择它们:

customButton btnSave = (customButton) view.findViewById(R.id.btnSave);
btnSave.getBackground().setLevel(3);

如果您的视图是,则不需要容器包装器 Image基于视图的视图 其他答案 由@themichaelscott建议,在这种情况下只需将src更改为 app:srcCompat="@drawable/ic_add"


3
2018-02-26 05:33