Support library VectorDrawable Resources$NotFoundException
AndroidAndroid Support-LibraryAndroiddesignsupportAndroid VectordrawableAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
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 - Android
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 - Android
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.
Solution 4 - Android
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 5 - Android
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
<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 6 - Android
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
use this in app.gradle
Then use AppCompatDrawableManager
to setDrawable and getDrawable. Works for me
Solution 7 - Android
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:
- https://code.google.com/p/android/issues/detail?id=205236
- https://code.google.com/p/android/issues/detail?id=204708
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.
Solution 8 - Android
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"
Solution 9 - Android
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
Solution 10 - Android
and VectorDrawable
in this support library can be inflated in this way:AnimatedVectorDrawable
- 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'
}
Solution 11 - Android
Do not put your vectors in drawable-anydpi
, old devices does not support that
put them in drawable
Solution 12 - Android
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.
Solution 13 - Android
Use AppCompatImageView
instead of ImageView
as said by Harish Gyanani in comments , it works fine with this for me.
Solution 14 - Android
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.