Support library VectorDrawable Resources$NotFoundException
Asked Answered
V

14

74

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?

Vagabond answered 3/6, 2016 at 13:17 Comment(3)
facing same problem as you .did you got any solution for android:drawableLeft ?Laryngitis
@Laryngitis No. I had to remove the graphic from the button altogether. Though you can provide separate png as drawable.Vagabond
yes.. i already did it same as your suggestion.Laryngitis
I
89

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://mcmap.net/q/270788/-is-that-right-to-make-android-view-inflateexception-by-using-vector-drawables-in-android-background-line-in-a-imageview")

    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(...);
    
Indamine answered 16/6, 2016 at 16:20 Comment(7)
does this apply even if my activity is based on AppCompatActivity? AppCompatActivity already uses AppCompatDelegate.Leigha
Simply use AppCompatImageView instead of ImageView. Problem solved. No need to keep pngs for lower versions. Even srcCompat is not needed.Cursor
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 ?Joktan
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); this was the killer line of code fo me. I had to add that in my application.Andesite
why replace setImageResource with seImageDrawable?Brooks
If you use imageView.setImageResource(...) then AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) is not needed. Tested with 25.3.1 support library.Tenrec
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. ThxLolland
G
65

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

Guss answered 25/1, 2017 at 15:47 Comment(1)
It DOESN'T work with setImageResource for KitKat (4.4.4) (and lower I guess), only this works https://mcmap.net/q/270789/-23-2-0-set-vector-drawable-as-background-in-4-xEldrid
F
32

Try using:

imageView.setImageDrawable(VectorDrawableCompat.create(getResources(), drawableRes, null));

You don't have to add AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); this way.

Just inflate your vector drawables using VectorDrawableCompat and you're all set.

Fieldfare answered 8/12, 2016 at 8:11 Comment(1)
This has 2 args instead of 3: AppCompatDrawableManager.get().getDrawable(context, resId);Roue
P
25

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

Patrimony answered 5/6, 2016 at 16:11 Comment(3)
My problem is that even AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); is not working. The same ResourceNotFound exception.Vagabond
@Vagabond you may try calling in onCreate method of Activities.Patrimony
You should be able to add it to the onCreate of you application class. That seems to be working for me.Indamine
A
16

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();
    }
}
Astyanax answered 10/1, 2018 at 11:46 Comment(0)
P
9
defaultConfig {
  vectorDrawables.useSupportLibrary = true
}

use this in app.gradle

Then use AppCompatDrawableManager to setDrawable and getDrawable. Works for me

Posturize answered 17/1, 2017 at 14:12 Comment(0)
A
7

Support for vector drawables in places like android:drawableLeft was disabled in support library 23.3. It was announced on Google+:

we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1. Using app:srcCompat and setImageResource() continues to work.

Links to issues:

However, if you can live with those issues, in 23.4 you can re-enable this functionality using AppCompatDelegate.setCompatVectorFromResourcesEnabled().

If you're curious how this works, the best person to learn from is Chris Banes, who authored this functionality. He explains in detail on his blog.

Agribusiness answered 5/6, 2016 at 16:31 Comment(0)
C
5

change

imageView.setImageResource(R.drawable.drawable_image)

to

imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.drawable_image));

if you want to use vectordrawable in xml, use this:

app:srcCompat="@drawable/drawable_image"
Cho answered 3/6, 2016 at 13:24 Comment(4)
Your method setImageDrawable works. But still button drawableLeft not working.Vagabond
This causes crashing on older devices.Cuff
This doesn't work. Use one of the other answers here instead.Cincture
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 askCho
B
1

I had a similar problem long ago, it did not work by setting

vectorDrawables.useSupportLibrary = true

only worked when I created the "mipmap" folder, and the code used

imageView.setImageResource (R.mipmap.drawable_image)

It has more Info here

Belittle answered 3/6, 2016 at 13:29 Comment(0)
C
1

API 16 animation
Inflating Drawable's

`VectorDrawable` and `AnimatedVectorDrawable` in this support library can be inflated in this way:

  • Calling static getDrawable() methods:
//This will only inflate a drawable with <vector> as the root element
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);

//This will only inflate a drawable with <animated-vector> as the root element
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);

// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

If inflating the Drawable in java code, it is recommended to always use ResourcesCompat.getDrawable() as this handles Lollipop fallback when applicable. This allows the system to cache Drawable ConstantState and hence is more efficient.
The library has the following morph (bi-directional) animations :

  • Play-Pause morph animation
  • Play-Stop morph animation
  • Arrow-Hamburger menu morph animation

  • As you can see, I produced the above image on my API 16 phone:
    import com.wnafee.vector.compat.AnimatedVectorDrawable;
    mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
    

    Look at the github README for vector-compat here: https://github.com/wnafee/vector-compat
    This will fix your problem (down to API 14) if you merge it with your app module's build.gradle dependencies (usually at the end of file):

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.android.support:design:25.0.0'
    //not needed
    //  compile 'com.android.support:support-vector-drawable:25.0.0'
        compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat
    //  Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0
    //not needed
    //  compile 'com.android.support:support-animated-vector-drawable:25.0.0'
    }
    
    Cottle answered 18/12, 2016 at 9:1 Comment(0)
    W
    1

    Do not put your vectors in drawable-anydpi , old devices does not support that

    put them in drawable

    Wheresoever answered 15/11, 2017 at 6:21 Comment(1)
    This Works for me.Brandon
    O
    1

    In my particular case, I had this problem because I was using a drawable selector as the image resource with several vectors in the selector, as in:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@drawable/vector_select_blue"/>
        <item android:state_pressed="true" android:drawable="@drawable/vector_select_black"/>
        .
        .
        etc
    </selector>
    

    Yes, pretty bad, but didn't know better at the time.

    So, the right way of doing this is using the tint property in your vector file, as in:

    <vector ..vector properties..
            android:tint="@color/vector_color_selector">
                  <path ..path properties../>
    </vector>
    

    (You can also use the app:tint attribute in the AppCompatImageView)

    And now, your vector_color_selector file should have the colors you want, as in:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:color="@color/blue"/>
        <item android:state_pressed="true" android:color="@color/black"/>
        .
        .
        etc
    </selector>
    

    I hope this helps someone if previous answers didn't work for you. Stating the obvious, but I must say that you still need to set vectorDrawables.useSupportLibrary = true in gradle, use AppCompatImageView and use app:srcCompat or setImageDrawable + AppCompatResources.getDrawable to avoid any troubles with the vector compat library.

    Oldline answered 15/3, 2019 at 6:10 Comment(0)
    C
    0

    Use AppCompatImageView instead of ImageView as said by Harish Gyanani in comments , it works fine with this for me.

    Official docs

    Comparator answered 7/10, 2018 at 20:44 Comment(0)
    W
    0

    I had the same problem and actually what was missing is I was using app:srcCompat on AppCompatTextView except of AppCompatImageView.

    The way I have found the problematic part:

    My error looks like:

    Fatal Exception: android.content.res.Resources$NotFoundException
    Resource ID #0x7f0700d1
    

    Here are the steps I followed the resource id of the mentioned drawable :

    • APK Analyzer -> classesXXX.dex
    • In this dex file I opened the directory of my apps package name and went to R$drawable file
    • R$drawable -> Show as byte code.
    • Search for ID [0x7f0700d1] (check your own ID)
    • Find the image and check for all the usages (CMD + F7) of the resource
    • Fix

    Hope it will help somebody.

    Willingham answered 22/10, 2020 at 10:21 Comment(0)

    © 2022 - 2024 — McMap. All rights reserved.