156

AndroidStudio 3.0 / Android Gradle plugin 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

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

Giorgio
  • 1,973
  • 4
  • 36
  • 51
langme
  • 1,672
  • 2
  • 9
  • 18
  • Related thread - [Updated Android Studio to 3.0 and getting this issue](https://stackoverflow.com/q/46947008/465053). Though solution is exactly same. – RBT Aug 30 '18 at 05:30

9 Answers9

371

To support API SDK < 24 add the following line to your app build.gradle inside the android block:

defaultConfig {
   vectorDrawables.useSupportLibrary = true
}

If your minSdkVersion is 24 or higher, then your issue will be resolved on its own, because VectorDrawables have full support starting with this API.

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

Sharp
  • 1,335
  • 1
  • 12
  • 27
mwa91
  • 3,949
  • 1
  • 13
  • 10
  • 4
    But why is the issue coming up and is the above solution a quickfix or a good strategy ? – Pranaysharma Nov 23 '17 at 03:24
  • @Pranaysharma See [Android Developers: Vector Drawables Backward Compatibility Solution](https://developer.android.com/guide/topics/graphics/vector-drawable-resources.html#vector-drawables-backward-solution) for details. – PointedEars Jan 23 '18 at 07:46
  • 1
    note: useSupportLibrary is wrong solution for me (min SDK 16, target 26), app crashes on start. I had have to remove this, remove @color from vectors and just search and replace all android:fillColor= pieces manually. It is very fast actually, take 10 seconds – djdance Feb 15 '18 at 10:02
  • Where do I add this line? – Foobar May 17 '18 at 21:56
  • @Roymunson inside android key. Look at Wonil's [answer](https://stackoverflow.com/a/52022896/465053) in this thread itself. – RBT Aug 30 '18 at 05:15
  • @mwa91 i stuck with this error form two hour and finally i solved thanks – Hassan jan Dec 16 '19 at 09:44
  • This is work for me api sdk version 28. it is best soluiton for use. – Randika Wanninayaka Mar 09 '21 at 03:09
15

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

<vector
  <path
    android:fillColor="#FFF"/></vector>
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
  • 2
    it is a regression version... if you want to change color, you are able to change one by one... it is easier to make a reference in color file and change it. – langme Oct 27 '17 at 15:40
  • there unfortunately this is the only way to make your app works or you have to downgrade your gradle version – Oussema Aroua Oct 27 '17 at 15:43
  • 3
    I too have the same issue. Why would a feature that was previously working fine be disabled? Is this maybe a bug? – joaomgcd Oct 27 '17 at 17:51
  • I just updated to AS3 and got this issue, too. Why would that feature be removed? – Marty Oct 29 '17 at 18:20
  • no other solution beside injecting the color hex directly to the vector – Oussema Aroua Oct 29 '17 at 19:52
  • 2
    This works as a workaround, but the "right" answer is @mwa91's answer to add `vectorDrawables.useSupportLibrary = true` to your `build.gradle`. – Sean Barbeau Dec 22 '17 at 19:51
  • @Marty it never did work correctly. When generating png's, they would just become black instead of the color you marked in the SVG. They just made it an error now to prevent you from missing this issue – Boy Apr 10 '18 at 06:19
  • @Marty are you sure that the icons on android 4.x were not black, while they were the desired colors for 5 and up? – Boy Apr 11 '18 at 17:32
10

Little bit more context for this error:

  • Android 5.0 (API level 21) was the first version to officially support vector drawables.
  • If you use minSdkVersion lower than 20, there are two solutions for vector drawable
    • Android Studio's Vector Asset Studio generate PNG. Please take a look Android Studio document. But, references to other resources are not supported by build-time PNG generation.
    • Use support library
  • Or, use 21 or above for minSdkVersion

For support library, add a statement to your build.gradle file:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

dependencies {
  compile 'com.android.support:appcompat-v7:23.2.0'
}
Wonil
  • 6,364
  • 2
  • 37
  • 55
  • @MikeHanafey Could you please point out doc why minSdkVersion must be 24? Thanks. – Wonil Nov 23 '18 at 02:08
  • The message below occurs unless minSdkVersion is increased to 24. I do not have an explanation, and found this by simply incrementing the value until the gradle build succeeded. Error: java.lang.RuntimeException: java.lang.RuntimeException: Error while processing ./src/main/res/drawable/ic_selection_right_widen.xml : Can't process attribute android:fillColor="@color/kb_key_long_press_available": references to other resources are not supported by build-time PNG generation. See http://developer.android.com/tools/help/vector-asset-studio.html for details. – Mike Hanafey Nov 24 '18 at 22:45
  • 5
    @MikeHanafey minSdkVersion must be 24 only if you use android:filltype, which sometimes the Android Vector Asset automatically puts in. Just get rid of it. – Simone Apr 02 '19 at 07:55
  • 1
    @Simone Thank you for the suggestion. That was the issue in my case. – Abdul Rehman Yawar Khan Sep 17 '19 at 09:07
  • 1
    default `fillType` is `nonZero` but it only differs from `evenOdd` in certain cases: https://www.w3.org/TR/SVG/painting.html#FillRuleProperty – hmac Jun 09 '20 at 10:17
5

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

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"/>

saigopi.me
  • 14,011
  • 2
  • 83
  • 54
  • 2
    This is a patch, I want to be able to re-theme my app without duplicating all drawables, also it's creating png instead of keeping vectorial. People should use @mwa91's solution. – Kikiwa Jul 25 '18 at 14:36
4

If your minSdkVersion is 21 you can disable the generation of PNG by adding this line:

// set to an empty list to disable the feature
vectorDrawables.generatedDensities = [] 

Removing this line will still generate the PNGs.

Source: http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.VectorDrawablesOptions.html

Pol
  • 482
  • 3
  • 11
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. in the original drawable folder change all the icons to use a static color

This way the compiler will work

joaomgcd
  • 5,287
  • 4
  • 28
  • 39
  • 1
    Yes, probably because API 21 supports Android 5.0 (Lollipop) which supports vector drawables natively. However, devices with earlier versions might then have no proper images at all: https://developer.android.com/guide/topics/graphics/vector-drawable-resources.html – PointedEars Jan 23 '18 at 07:42
0

I think you are using android:fillColor="@color/image_button_disabled" this code for vector drawable.

// image_button_disabled.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/circular_image_color_pressed" android:state_pressed="true" />
    <item android:color="@color/circular_image_color_normal" />
</selector>

It's not supported.

Just replace it with android:fillColor="#c4ca5e"

Ahamadullah Saikat
  • 4,437
  • 42
  • 39
0

To add up to @mwa91 answer.

In case you need dynamic theming of drawable, but you don't have time to update your layouts and go with @mwa91 answer and change all android:src="@drawable/..." attributes to app:srcCompat="@drawable/...", you can always use hex color value in drawable and then later tint the drawable in ImageView:

<ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/your_drawable"
      android:tint="@color/image_button_disabled"
      />
TheTechWolf
  • 2,084
  • 2
  • 19
  • 24
0

Remove the filltype and the warning will disappear. That's because:

minSdkVersion must be 24 only if you use android:filltype

The default fillType is nonZero but it only differs from evenOdd in certain cases

Make sure you don't need filltype, of course. In some cases it may affect the appearance of your vector drawable. You most likely don't need it if your icons are simple. It's sometimes added automatically, for instance when you import vector drawables from SVG.

Check this page for an explanation what it does w3.org/TR/SVG/painting.html#FillRuleProperty

Credit goes to @Simone and @hmac


A simple alternative is to use a color value directly, instead of using a color reference.

android:fillColor="#FF00AABB" // use this
android:fillColor="@color/image_button_disabled" // instead of this

Another alternative is settings this, but I'd rather avoid it:

defaultConfig {
   vectorDrawables.useSupportLibrary = true
}

The last resort alternative is to increase your minSdkVersion to 24.

Stan
  • 2,151
  • 1
  • 25
  • 33