Vector Drawable cannot be loaded properly in Android API 19 using Support Library (23.4.0)

Recently, I want to use Vector Drawable to manage all the icons. Vector Drawable works well in Android API 21+, but when I tried to use it on one of my test devices [Amazon KFTHWI Android 4.4.3 API 19], all the icons could not be loaded at all!

I followed the advice here and here . But still it doesn’t work.

Here is my configuration:

In the gradle script project file:

buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.0' classpath ... } } 

So the gradle plugin version should be 2.0+

In the main module gradle script file:

 android{ ... defaultConfig{ ... vectorDrawables.useSupportLibrary = true ... } } dependencies{ ... compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:gridlayout-v7:23.4.0' ... } 

This core module contains the application. The reason I am compiling the gridlayout-v7 library is because I use some Gridlayouts in the application.

Here is my implementation:

Since I need to generate some views according to the data I received, I set the image source to Java code.

 ImageView icon = new ImageView(this); icon.setImageResource(R.drawable.ic_lock_black_24dp); 

ic_lock_black_24dp.xml is created from the Icon material library in Studio Vector Asset.

My expectation:

I believe that my configuration in my main gradle script module will not allow Android Studio to generate Png for vectors and will allow Android to generate Drawables for Vectors directly at runtime.

But I'm only partially right ... My configuration actually prohibits Android Studio from creating Png when building. BUT my old Android device [API 19] cannot generate Drawables from Vectors !! Thus, there are no icons in the application now ...

Here are the exceptions: [Amazon KFTHWI Android 4.4.3 API 19]

 W/ImageView:Unable to find resource:2130837702 android.content.res.Resources$NotFoundException: File res/drawable/ic_lock_black_24dp.xml from drawable resource ID#0x7f0200c6 at android.content.res.Resources.loadDrawable(Resources.java:2206) at android.content.res.Resources.getDrawable(Resources.java:707) at android.widget.ImageView.resolveUri(ImageView.java:651) at android.widget.ImageView.onMeasure(ImageView.java:774) at android.view.View.measure(View.java:17038) at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473) at android.view.View.measure(View.java:17038) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:17038) at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473) at android.view.View.measure(View.java:17038) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052) at android.widget.LinearLayout.onMeasure(LinearLayout.java:590) at android.view.View.measure(View.java:17038) at android.support.v7.widget.GridLayout.measureChildWithMargins2(GridLayout.java:896) at android.support.v7.widget.GridLayout.measureChildrenWithMargins(GridLayout.java:906) at android.support.v7.widget.GridLayout.onMeasure(GridLayout.java:945) at android.view.View.measure(View.java:17038) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:17038) at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1273) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.widget.ScrollView.onMeasure(ScrollView.java:373) at android.view.View.measure(View.java:17038) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:17038) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:17038) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191) at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) at android.view.View.measure(View.java:17038) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2346) at android.view.View.measure(View.java:17038) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2004) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1176) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1368) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1057) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5857) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:815) at android.view.Choreographer.doCallbacks(Choreographer.java:628) at android.view.Choreographer.doFrame(Choreographer.java:598) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:801) at android.os. 

Oddly enough, all the icons will be well represented if I do not use the AppCompat support library, but I know that this is due to the fact that Android Studio creates Png when building. I can even find these Png in the APK.

My problem:

Now I still want to use Vector Drawable on older Android devices, which means I'm still trying to avoid using Pngs. Can someone give me some advice or tell me in what steps I did something wrong?

+6
source share
2 answers

You should say new AppCompatImageView(this) . When creating views manually, you must add the AppCompat prefix.

+3
source

There is a library supporting api 14+ for VectorDrawable and AnimatedVectorDrawable, I use: https://github.com/wnafee/vector-compat

+1
source

All Articles