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

自作多情 提交于 2019-12-03 01:22:42

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"

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"/>

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

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

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

-keepresourcexmlattributenames vector/*
zaPlayer

for me it was the old build tools wrapper in the project root build.gradle file

dependencies {
    ....
    classpath 'com.android.tools.build:gradle:2.1.2'
}

instead of

classpath 'com.android.tools.build:gradle:1.X.X'
yzxclh 0205
 classpath 'com.android.tools.build:gradle:2.1.0'
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!