Support library VectorDrawable Resources$NotFoundException
Solution 1
It took 3 separate things for me to get this to work using support library 23.4.0:
-
Add this to
build.gradle
defaultConfig { vectorDrawables.useSupportLibrary = true }
-
Add the following to onCreate of your
Application
classAppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
(From the reference of this link - "https://stackoverflow.com/a/45582033/10752962")
In API less then 21,use this line before
setContentView()
; -
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).
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
Arka
Updated on July 05, 2022Comments
-
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
orimageView.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 almost 8 yearsYour method setImageDrawable works. But still button drawableLeft not working.
-
Arka almost 8 yearsMy problem is that even
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
is not working. The same ResourceNotFound exception. -
Murat almost 8 years@Arka you may try calling in onCreate method of Activities.
-
Rich Luick almost 8 yearsYou should be able to add it to the onCreate of you application class. That seems to be working for me.
-
Thupten over 7 yearsdoes this apply even if my activity is based on AppCompatActivity? AppCompatActivity already uses AppCompatDelegate.
-
Harish Gyanani over 7 yearsSimply use
AppCompatImageView
instead ofImageView
. Problem solved. No need to keep pngs for lower versions. EvensrcCompat
is not needed. -
Clive Jefferies about 7 yearsThis causes crashing on older devices.
-
Sakiboy about 7 yearsThis doesn't work. Use one of the other answers here instead.
-
jmarkstar about 7 yearsyou 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 over 6 yearsI've done steps 1 to 3 (on the 3rd step used
app:srcCompat
). However, I still see an exceptionXmlPullParserException: Binary XML file line #4: invalid drawable tag vector
. It looks like thevector
tag in the XML is not recognized on this KitKat device ? -
user924 over 6 yearsIt DOESN'T work with
setImageResource
for KitKat (4.4.4) (and lower I guess), only this works stackoverflow.com/a/35918375/7767664 -
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 about 6 yearswhy replace
setImageResource
withseImageDrawable
? -
Eric B. almost 6 yearsIf you use
imageView.setImageResource(...)
thenAppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
is not needed. Tested with 25.3.1 support library. -
CaptainCrunch almost 2 yearsWith 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