Vector Drawables flag doesn't work on Support Library 24+

18,253

Solution 1

Ugh... I hate it when this happens. You ask a question and then answer it yourself a few hours later. Anyways, it appears as though I was using an outdated build tools version. All I had to do was change one line in my gradle:

buildToolsVersion "24.0.1"

Solution 2

In addition to the Gradle configuration, for me the trick was to add this line within the activity onCreate():

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

Your build.gradle (project) if using version 2.0+ add below code in your build.gradle (app)

// Gradle Plugin 2.0+  
 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

and, if using version 1.5 add below in your build.gradle (app)

// Gradle Plugin 1.5  
 android {  
   defaultConfig {  
     generatedDensities = []  
  }  

  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 }

and, of course within the layout files, you should use srcCompat attribute:

<ImageView  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  app:srcCompat="@drawable/ic_add"/>

Solution 3

In 24.2.0 the v4 Support Library has been splitted by google into several smaller modules:

com.android.support:support-compat:24.2.0

Provides compatibility wrappers for new framework APIs, such as Context.getDrawable() and View.performAccessibilityAction().

com.android.support:support-core-utils:24.2.0

Provides a number of utility classes, such as AsyncTaskLoader and PermissionChecker.

com.android.support:support-core-ui:24.2.0

Implements a variety of UI-related components, such as ViewPager, NestedScrollView, and ExploreByTouchHelper.

com.android.support:support-media-compat:24.2.0

Backports portions of the media framework, including MediaBrowser and MediaSession.

com.android.support:support-fragment:24.2.0

Backports the fragment framework. This module has dependencies on support-compat, support-core-utils, support-core-ui, and support-media-compat.

You can see all the changes here

Solution 4

So the answer to this problem is to remind Android maintainers that they've dropped the ball and ask them to fix their libraries.

The file abc_vector_text.xml is missing from the project at runtime, but the missing file won't be flagged in the build if your project isn't using it in the first place. The name of the file indicates it's just part of some test suite.

I'm using the following dependency: compile 'com.android.support:appcompat-v7:25.0.1'

Because this page indicates that the file is found in that library: https://github.com/dandar3/android-support-v7-appcompat/blob/master/res/drawable/abc_vector_test.xml.

and yet, I still see the error at runtime.

Notice that's a github link of a project that is not the official Android project. I can't seem to find the file in the official project build anywhere. Which, yet again, indicates this is just a problem of sloppy maintenance. Here's my stacktrace for comparison:

ontent.res.Resources$NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f020052
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.os.Looper.loop(Looper.java:137)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at java.lang.reflect.Method.invoke(Method.java:511)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at dalvik.system.NativeStart.main(Native Method)
01-02 00:02:23.130 E/AndroidRuntime( 3037): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f020052
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.content.res.Resources.loadDrawable(Resources.java:1953)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.content.res.Resources.getDrawable(Resources.java:660)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:723)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:81)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:127)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:74)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at com.ccc.lib.activity.LoggingAppCompatActivity.onCreate(LoggingAppCompatActivity.java:416)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at com.ccc.lib.debugMode.DebugModeActivity.onCreate(DebugModeActivity.java:95)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at com.ccc.lib.activity.IBusActivity.onCreate(IBusActivity.java:46)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at com.ccc.lib.activity.MainActivity.onCreate(MainActivity.java:173)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.app.Activity.performCreate(Activity.java:5104)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     ... 11 more
01-02 00:02:23.130 E/AndroidRuntime( 3037): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     at android.content.res.Resources.loadDrawable(Resources.java:1950)
01-02 00:02:23.130 E/AndroidRuntime( 3037):     ... 33 more

Solution 5

I had the same problem with dexguard. Add this line on your configuration file:

-keepresourcexmlattributenames vector/*
Share:
18,253

Related videos on Youtube

Brigham Byerly
Author by

Brigham Byerly

Updated on July 09, 2020

Comments

  • Brigham Byerly
    Brigham Byerly almost 4 years

    Today, it seems as though Android Nougat was released. Thus, I am more excited than ever to optimize my app for the new features like split-screen. I would like to push a version of my app that targets SDK version 24 so that users aren't notified that my app may not work in split-screen. However, doing so means that I should also update to version 24 of the Support Library. Like many others, I experienced a problem when updating to version 23.2.0 of the Support Library. However, I followed this answer and it fixed my issue. Now the issue is returning as of version 24.0.0 and up of the Support Library. In all of my tests I am using the gradle flag described in the linked answer:

    vectorDrawables.useSupportLibrary = true
    

    It is also important to note that this is only happening on pre-Lolliop devices (Kitkat and below). Lollipop and up works perfectly. When using the following dependencies, the flag works fine:

    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    

    But when using these dependencies, I get a crash similar to the one I got before using the flag:

    compile 'com.android.support:support-v4:24.2.0'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
    compile 'com.android.support:cardview-v7:24.2.0'
    

    Here is the stack trace of the crash:

    FATAL EXCEPTION: main
    Process: com.badon.brigham.time, PID: 2070
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.badon.brigham.time/com.badon.brigham.time.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f02004f
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
          at android.app.ActivityThread.access$800(ActivityThread.java:135)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:136)
          at android.app.ActivityThread.main(ActivityThread.java:5017)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
          at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f02004f
          at android.content.res.Resources.loadDrawable(Resources.java:2101)
          at android.content.res.Resources.getDrawable(Resources.java:700)
          at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:346)
          at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:194)
          at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:182)
          at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:717)
          at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:187)
          at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
          at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:127)
          at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
          at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
          at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
          at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
          at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
          at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
          at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
          ...
    

    Am I totally missing something? Or is this already a known issue (I couldn't find anything on Google)? Any help would be appreciated.

    • Jorge Mendez
      Jorge Mendez over 7 years
      That looks like the resource couldn't be found, make sure it is in the correct folder
    • lionscribe
      lionscribe over 7 years
      Is this happening in Debug version of app, or only in Release?
    • Brigham Byerly
      Brigham Byerly over 7 years
      @JorgeMendez It's actually a resource that can't be found internally within the Support Library. It has to to with the new vector capabilities for pre-Lollipop devices. More Here
    • Brigham Byerly
      Brigham Byerly over 7 years
      @lionscribe I am having this problem in the Debug version of the app - let me see about the Release version.
    • Brigham Byerly
      Brigham Byerly over 7 years
      @lionscribe Yup, release too.
  • Brigham Byerly
    Brigham Byerly over 7 years
    I'm not using DexGuard. I am using ProGuard, but that is only enforced for release builds and my error occurs on debug builds.
  • lionscribe
    lionscribe over 7 years
    @ialfa I assume you got that solution from my previous post. That was the reason I asked if it was in Release version only.
  • Brigham Byerly
    Brigham Byerly over 7 years
    This can't be the reason I'm experiencing the crash as I start seeing it as early as 24.0.0 . It is interesting, though, that at version 24.2.0 the xml file of the not-found resource changes which might have something to do with this "split".
  • lionscribe
    lionscribe over 7 years
    @ialfa I'm happy I have helped someone!
  • JuiCe
    JuiCe over 7 years
    Does this mean that if we remove com.android.support:support-v4:24.2.0 from our gradle file and add the 6 dependencies listed above we would technically get all of the same functionality?
  • Reuben Tanner
    Reuben Tanner over 7 years
    this did not fix my issue on pre-Lollipop APIs. I am using support lib version 24.2.0, build tools 24.0.2 and i am supplying --no-version-vectors when using aapt.
  • Brigham Byerly
    Brigham Byerly over 7 years
    @HypotheticalintheClavicle Do you have the same stack trace and everything?
  • Coder Roadie
    Coder Roadie over 7 years
    I'm seeing the same issue today after updating my project to 25.0.1 verison of support libraries. Honestly, the Android library maintainers have no excuse for yet another debacle in library design. There's no indication yet (i'll keep looking) about the library that I need to include so that my project can find abc_vector_test.xml
  • FindOut_Quran
    FindOut_Quran over 7 years
    @Thanks. For me, compileSdkVersion was 25, but buildToolsVersion was 21.1.2. I correct it to 25.0.1 and worked
  • AppiDevo
    AppiDevo over 7 years
    didnt help me too
  • Dan Dar3
    Dan Dar3 over 7 years
    Althought that is not the official release, that is a conversion of the AAR that Google releases into an Eclipse library project. See the notes in the REAMDE.md and you may be able to find the resource file in the AAR locally on your machine to confirm. github.com/dandar3/android-support-v7-appcompat/tree/25.0.1
  • mikejonesguy
    mikejonesguy over 7 years
    The secret sauce here is that compileSdkVersion, buildToolsVersion, and the support library versions all need to be on the same major version number (all begin with the same major number, like 24, for example).
  • shweta c
    shweta c almost 7 years
    It works well for imageview but actually what i need for background image of textview, Please help
  • Mr.Moustard
    Mr.Moustard almost 7 years
    @shwetac I don't know if you can do that, but instead of only use a textView, you could try to use a imageView as a background image and a textView on top of it.
  • shweta c
    shweta c almost 7 years
    moustard I used same android:background but got error.
  • Mr.Moustard
    Mr.Moustard almost 7 years
    I didn't try it, but I'm pretty sure you cannot do that, you must use app:srcCompat
  • G0dsquad
    G0dsquad about 6 years
    This helped me fix an Android 4 issue with React Switch. Thanks!
  • Mr.Moustard
    Mr.Moustard about 6 years
    @G0dsquad I’m glad to hear that.