构建时PNG生成不支持对其他资源的引用

时间:2021-12-10 20:55:14

AndroidStudio 3.0 / Android Gradle plugin 3.0

AndroidStudio 3.0 / Android Gradle插件3.0

<vector>
  <path android:fillColor="@color/image_button_disabled"/>
</vector>

I get this error:

我收到此错误:

references to other resources are not supported by build-time PNG generation

构建时PNG生成不支持对其他资源的引用

Will it be possible to resolve it or is it a deprecated solution?

是否有可能解决它或者它是一个弃用的解决方案?

4 个解决方案

#1


118  

In your app build.gradle add the following line:

在您的app build.gradle中添加以下行:

defaultConfig{
   vectorDrawables.useSupportLibrary = true
}

See Android Developers: Vector Drawables Backward Compatibility Solution for details.

有关详细信息,请参阅Android开发者:Vector Drawables向后兼容性解决方案。

#2


8  

you need to use the hex code directly not referring to a resource.

您需要直接使用十六进制代码而不是引用资源。

<vector
  <path
    android:fillColor="#FFF"/></vector>

#3


2  

Do not use colorReference at fillColor attribute in Vector drawable just use hexa code

不要在Vector drawable中的fillColor属性中使用colorReference,只需使用hexa代码

use like this

像这样用

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#1abc9c"
    android:pathData="M9,11L7,11v2h2v-2zM13,11h-2v2h2v-2zM17,11h-2v2h2v-2zM19,4h-1L18,2h-2v2L8,4L8,2L6,2v2L5,4c-1.11,0 -1.99,0.9 -1.99,2L3,20c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.9,-2 -2,-2zM19,20L5,20L5,9h14v11z"/>

instead of this

而不是这个

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="@color/colorPrimary"
    android:pathData="M9,11L7,11v2h2v-2zM13,11h-2v2h2v-2zM17,11h-2v2h2v-2zM19,4h-1L18,2h-2v2L8,4L8,2L6,2v2L5,4c-1.11,0 -1.99,0.9 -1.99,2L3,20c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.9,-2 -2,-2zM19,20L5,20L5,9h14v11z"/>

#4


0  

I've been able to work around this by doing the following:

我已经能够通过以下方式解决这个问题:

  1. create a drawable-v21 folder and copy all your drawable xmls that use variables there
  2. 创建一个drawable-v21文件夹并复制所有使用变量的可绘制xmls
  3. in the original drawable folder change all the icons to use a static color
  4. 在原始drawable文件夹中更改所有图标以使用静态颜色

This way the compiler will work

这样编译器就可以工作了

#1


118  

In your app build.gradle add the following line:

在您的app build.gradle中添加以下行:

defaultConfig{
   vectorDrawables.useSupportLibrary = true
}

See Android Developers: Vector Drawables Backward Compatibility Solution for details.

有关详细信息,请参阅Android开发者:Vector Drawables向后兼容性解决方案。

#2


8  

you need to use the hex code directly not referring to a resource.

您需要直接使用十六进制代码而不是引用资源。

<vector
  <path
    android:fillColor="#FFF"/></vector>

#3


2  

Do not use colorReference at fillColor attribute in Vector drawable just use hexa code

不要在Vector drawable中的fillColor属性中使用colorReference,只需使用hexa代码

use like this

像这样用

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#1abc9c"
    android:pathData="M9,11L7,11v2h2v-2zM13,11h-2v2h2v-2zM17,11h-2v2h2v-2zM19,4h-1L18,2h-2v2L8,4L8,2L6,2v2L5,4c-1.11,0 -1.99,0.9 -1.99,2L3,20c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.9,-2 -2,-2zM19,20L5,20L5,9h14v11z"/>

instead of this

而不是这个

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="@color/colorPrimary"
    android:pathData="M9,11L7,11v2h2v-2zM13,11h-2v2h2v-2zM17,11h-2v2h2v-2zM19,4h-1L18,2h-2v2L8,4L8,2L6,2v2L5,4c-1.11,0 -1.99,0.9 -1.99,2L3,20c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.9,-2 -2,-2zM19,20L5,20L5,9h14v11z"/>

#4


0  

I've been able to work around this by doing the following:

我已经能够通过以下方式解决这个问题:

  1. create a drawable-v21 folder and copy all your drawable xmls that use variables there
  2. 创建一个drawable-v21文件夹并复制所有使用变量的可绘制xmls
  3. in the original drawable folder change all the icons to use a static color
  4. 在原始drawable文件夹中更改所有图标以使用静态颜色

This way the compiler will work

这样编译器就可以工作了