Karim Fikani
Karim Fikani

Reputation: 356

invalid drawable tag animated-vector

I've looked all over stackoverflow and no question was able to solve my problem.

I'm trying to play an animated vector drawable on a device with API 15 but I keep getting the following runtime exception:

    com.karim.test E/AvdcInflateDelegate: Exception while inflating <animated-vector>
android.content.res.Resources$NotFoundException: File res/drawable-ldpi-v4/feedback.png from xml type xml resource ID #0x7f020066
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2152)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getXml(Resources.java:906)
at android.support.graphics.drawable.VectorDrawableCompat.create(VectorDrawableCompat.java:408)
at android.support.graphics.drawable.AnimatedVectorDrawableCompat.inflate(AnimatedVectorDrawableCompat.java:356)
at android.support.graphics.drawable.AnimatedVectorDrawableCompat.createFromXmlInner(AnimatedVectorDrawableCompat.java:162)
at android.support.v7.widget.AppCompatDrawableManager$AvdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:905)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:356)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
at com.karim.test.MainActivity.initialize(MainActivity.java:428)
at com.karim.test.MainActivity.onCreate(MainActivity.java:210)
     Caused by: java.io.FileNotFoundException: Corrupt XML binary file
at android.content.res.AssetManager.openXmlAssetNative(Native Method)
at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:487)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2134)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107) 
at android.content.res.Resources.getXml(Resources.java:906) 
at android.support.graphics.drawable.VectorDrawableCompat.create(VectorDrawableCompat.java:408) 
at android.support.graphics.drawable.AnimatedVectorDrawableCompat.inflate(AnimatedVectorDrawableCompat.java:356) 
at android.support.graphics.drawable.AnimatedVectorDrawableCompat.createFromXmlInner(AnimatedVectorDrawableCompat.java:162) 
at android.support.v7.widget.AppCompatDrawableManager$AvdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:905) 
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:356) 
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197) 
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192) 
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66) 
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71) 
at com.karim.test.MainActivity.initialize(MainActivity.java:428) 
at com.karim.test.MainActivity.onCreate(MainActivity.java:210)

    android.content.res.Resources$NotFoundException: File res/drawable/animated_feedback_left.xml from drawable resource ID #0x7f020065
at android.content.res.Resources.loadDrawable(Resources.java:1923)
at android.content.res.Resources.getDrawable(Resources.java:664)
at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:128)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:45)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:201)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
at com.karim.test.MainActivity.initialize(MainActivity.java:422)
at com.karim.test.MainActivity.onCreate(MainActivity.java:204)

    Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag animated-vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:863)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
at android.content.res.Resources.loadDrawable(Resources.java:1920)
at android.content.res.Resources.getDrawable(Resources.java:664) 
at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:128) 
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:45) 
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323) 
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:201) 
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191) 
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66) 
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71) 
at com.karim.test.MainActivity.initialize(MainActivity.java:422)
at com.karim.test.MainActivity.onCreate(MainActivity.java:204)


I'm using the same resources from my previous question that you can find over here Animated Vector Drawable not working with API 15

Also I've tested instead of using ImageView to use AppCompatImageView but still same error.

I've been debugging this error and it fails on the following statement:

final XmlPullParser parser = res.getXml(resId);

inside:

public static VectorDrawableCompat create(@NonNull Resources res, @DrawableRes int resId, @Nullable Theme theme)

method of VectorDrawableCompat verion 23.3.0.

It seems that I'm having an issue with compatibility. At some point Android decides to use the XML parsing of API 15 instead of using the compat version. Any ideas on how I can solve this annoying problem?

Update: I followed Chris Banes blog and still no luck. Basically I've updated the compat libraries to 23.4.0 and I used the following in my activity:

static {
    AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}

Upvotes: 3

Views: 1493

Answers (2)

blinker
blinker

Reputation: 698

There is support library 25.4.0 and it's intended to solve all our problems. For example it finally supports path morphing!

To use it add:

allprojects {
    repositories {
        maven {
           url "https://maven.google.com"
        }
    }
}

inside your project gradle file, and accordingly dependency itself:

compile 'com.android.support:support-v4:25.4.0'

After that you can use vector drawable animation with path morphing (which is often the culprit behind "invalid drawable tag animated-vector") in below manner (a little Kotlin code):

val drawable = AnimatedVectorDrawableCompat.create(this, R.drawable.vector_drawable)
imageView.setImageDrawable(drawable) // You may use usual ImageView class, no Compat necessary.
drawable.start()

Upvotes: 1

Jon Goodwin
Jon Goodwin

Reputation: 9153

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'
    }
    

    Upvotes: 1

    Related Questions