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

你。 提交于 2019-12-20 11:21:08

问题


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.


回答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"



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



回答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




回答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



回答5:


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

-keepresourcexmlattributenames vector/*



回答6:


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'



回答7:


 classpath 'com.android.tools.build:gradle:2.1.0'
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip


来源:https://stackoverflow.com/questions/39091521/vector-drawables-flag-doesnt-work-on-support-library-24

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!