Android VectorDrawables.useSupportLibrary = True Is Stopping App


Answer :

You cannot use Vector Drawables in any other views except ImageView in pre-lollipop.

Please see this SO Answer by google developer advocate.

For AppCompat users, we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1 [ https://code.google.com/p/android/issues/detail?id=205236, https://code.google.com/p/android/issues/detail?id=204708 ]. Using app:srcCompat and setImageResource() continues to work.

If you want to use the Vector Drawables pre-lollipop, use can set it programatically by converting it into a drawable.

Drawable drawable;  if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {      drawable = context.getResources().getDrawable(drawableResId, context.getTheme()); } else {      drawable = VectorDrawableCompat.create(context.getResources(), drawableResId, context.getTheme()); }  button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null); 

I was on one of practice projects trying to learn background tasks and services in Android. When I pulled their sample code then initially it wasn't compiling due to this error. So I added below tag in app's build.gradle file to get rid of compilation issue:

android {     defaultConfig{         vectorDrawables.useSupportLibrary = true     } } 

Now, the moment I added this configuration then my app also started to crash in Microsoft Android Simulator exactly the way OP has described.

Since my current focus was to learn about background tasks so I wanted to get started with debugging my app. So, I removed above configuration setting again. In addition to that I also removed an attribute named android:fillColor in path tag of one of my drawable resources (or you can also replace the value @color/colorAccent with a hexadecimal code such as #FF000000). After removing(or changing with hexadecimal code) it, my initial compilation error didn't come:

<?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="http://schemas.android.com/apk/res/android"     android:width="80dp"     android:height="80dp"     android:viewportWidth="24"     android:viewportHeight="24">      <path         android:fillColor="@color/colorAccent"         android:pathData="M16.01 7L16 3h-2v4h-4V3H8v4h-0.01C7 6.99 6 7.99 6 8.99v5.49L9.5 18v3h5v-3l3.5-3.51v-5.5c0-1-1-2-1.99-1.99z" />     <path         android:pathData="M0 0h24v24H0z" /> </vector> 

After removal it looks like:

<?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="http://schemas.android.com/apk/res/android"     android:width="80dp"     android:height="80dp"     android:viewportWidth="24"     android:viewportHeight="24">      <path         android:pathData="M16.01 7L16 3h-2v4h-4V3H8v4h-0.01C7 6.99 6 7.99 6 8.99v5.49L9.5 18v3h5v-3l3.5-3.51v-5.5c0-1-1-2-1.99-1.99z" />     <path         android:pathData="M0 0h24v24H0z" /> </vector> 

My MS Android emulator had Kitkat(4.4). My code was crashing as my app was trying to use vector drawables on a pre-lollipop version of Android as mentioned in Vipul's accepted answer.


To use VectorDrawableCompat, you need to set:

android.defaultConfig.vectorDrawables.useSupportLibrary = true 

To use VectorDrawableCompat, you need to make two modifications to your project. First, set android.defaultConfig.vectorDrawables.useSupportLibrary = true in your build.gradle file, and second, use app:srcCompat instead of android:src to refer to vector drawables.

  1. Go to your build.gradle(Module: app) and add following line to android block. It should look like that.

    android {     compileSdkVersion 27     defaultConfig {         applicationId "..."         minSdkVersion 15         targetSdkVersion 27         versionCode 1         versionName "1.0"         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"         vectorDrawables.useSupportLibrary = true // <------     }     buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'         }     } } 
  2. Refer to the source as srcCompat to allow your application to use vector graphics.

    app:srcCompat="@drawable/plane"


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?