Support library VectorDrawable Resources$NotFoundException

38,207

Solution 1

It took 3 separate things for me to get this to work using support library 23.4.0:

  1. Add this to build.gradle

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    
  2. Add the following to onCreate of your Application class

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    

    (From the reference of this link - "https://stackoverflow.com/a/45582033/10752962")

    In API less then 21,use this line before setContentView();

  3. For all XML views in which you are setting a vector drawable replace

    android:src
    

    with

    app:srcCompat
    

    and in the code replace this:

    imageView.setImageResource(...);
    

    with

    imageView.setImageDrawable(...);
    

Solution 2

To complement some of the answers here: backward-compatible support for VectorDrawables comes with a price and doesn't work in all cases.

In which cases does it work? I've made this diagram to help (valid for Support Library 23.4.0 to at least 25.1.0).

VectorDrawable cheatsheet

Solution 3

We had the same issue. Vector drawables were not visible on Kitkat. I solved this issue by adding AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); to the onCreate method of Activities.

Before that dont forget to add:

defaultConfig {
    vectorDrawables.useSupportLibrary = true
}

and call setImageResource for the view that you use the vector drawable. My view is ImageButton. I have Android SDK build tools version 23.0.3

Solution 4

Sorry for being late to the party but this answer may help users who want to enable the flag AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); for all activities.

1. Create a class which extends to Application (android.app.Application)

public class MyApplicationClass extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
    }
}

2. Head over to Manifest.xml and add the following line to your tag

<application
    android:name=".MyApplicationClass"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    ...
</application>

3. Add the following code above onCreate in MyApplicationClass.java

// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

Complete code for MyApplicationClass.java

import android.app.Application;
import android.support.v7.app.AppCompatDelegate;

/**
* Created by Gaurav Lonkar on 23-Dec-17.
*/

public class MyApplicationClass extends Application
{
    // This flag should be set to true to enable VectorDrawable support for API < 21
    static
    {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
    }
}

Solution 5

defaultConfig {
  vectorDrawables.useSupportLibrary = true
}

use this in app.gradle

Then use AppCompatDrawableManager to setDrawable and getDrawable. Works for me

Share:
38,207
Arka
Author by

Arka

Updated on July 05, 2022

Comments

  • Arka
    Arka almost 2 years

    I am using Design Support Library version 23.4.0. I have enabled the gradle flag:

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    

    I am using build tools version 23.0.2, but still, I am getting Resources$NotFoundException on KitKat or lower.

    It is occurring when I use android:drawableLeft or imageView.setImageResource(R.drawable.drawable_image).

    And yes, I am putting this on every activity where I am using drawables

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
    

    Is this a bug of the support library?

  • Arka
    Arka almost 8 years
    Your method setImageDrawable works. But still button drawableLeft not working.
  • Arka
    Arka almost 8 years
    My problem is that even AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); is not working. The same ResourceNotFound exception.
  • Murat
    Murat almost 8 years
    @Arka you may try calling in onCreate method of Activities.
  • Rich Luick
    Rich Luick almost 8 years
    You should be able to add it to the onCreate of you application class. That seems to be working for me.
  • Thupten
    Thupten over 7 years
    does this apply even if my activity is based on AppCompatActivity? AppCompatActivity already uses AppCompatDelegate.
  • Harish Gyanani
    Harish Gyanani over 7 years
    Simply use AppCompatImageView instead of ImageView. Problem solved. No need to keep pngs for lower versions. Even srcCompat is not needed.
  • Clive Jefferies
    Clive Jefferies about 7 years
    This causes crashing on older devices.
  • Sakiboy
    Sakiboy about 7 years
    This doesn't work. Use one of the other answers here instead.
  • jmarkstar
    jmarkstar about 7 years
    you should have this vectorDrawables.useSupportLibrary = true on your build.gradle and this AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); on your onCreate of your Application Class or {AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)‌​; } on your activity. Check the question. He added those lines on his code before he ask
  • Someone Somewhere
    Someone Somewhere over 6 years
    I've done steps 1 to 3 (on the 3rd step used app:srcCompat). However, I still see an exception XmlPullParserException: Binary XML file line #4: invalid drawable tag vector. It looks like the vector tag in the XML is not recognized on this KitKat device ?
  • user924
    user924 over 6 years
    It DOESN'T work with setImageResource for KitKat (4.4.4) (and lower I guess), only this works stackoverflow.com/a/35918375/7767664
  • shadowsheep
    shadowsheep about 6 years
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); this was the killer line of code fo me. I had to add that in my application.
  • Boy
    Boy about 6 years
    why replace setImageResource with seImageDrawable?
  • Eric B.
    Eric B. almost 6 years
    If you use imageView.setImageResource(...) then AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) is not needed. Tested with 25.3.1 support library.
  • CaptainCrunch
    CaptainCrunch almost 2 years
    With the new androidX libs, I only had to use "app:srcCompat" to fix the crash. Everything else stayed the same. Only used the XML. For ref. the ress. was: "@drawable/ic_done_white" that only crashed in 4.4. Thx