Android: Crash when using vector Drawables on Pre Lollipop.

Leo N
2 min readMar 28, 2019

--

First of all, we need to know one thing that

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://goo.gl/u5suZB, https://goo.gl/fW5Tyd]. Using app:srcCompat and setImageResource() continues to work.

Second, Happy to advertise that They re-enabled that VectorDrawable in Android Support Library 23.4.0

For AppCompat users, we’ve added an opt-in API to re-enable support Vector Drawables from resources (the behavior found in 23.2) via AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) — keep in mind that this still can cause issues with memory usage and problems updating Configuration instances, hence why it is disabled by default. If you want re-enable this, just stick this at the top of your Activity:

Finally, what thing we need to do

static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

Still need

I’m guessing that most of you will be using Gradle so let’s quickly walk through that. If you’re using v2.0 or above of the Gradle plugin, we have a handy shortcut to enable everything:

android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}

If you have not updated yet, and are using v1.5.0 or below of the Gradle plugin, you need to add the following to your app’s build.gradle:

android {
defaultConfig {
// Stops the Gradle plugin’s automatic rasterization of vectors
generatedDensities = []
}
// Flag to tell aapt to keep the attribute ids around
aaptOptions {
additionalParameters "--no-version-vectors"
}
}

If you want to do something in your code

Drawable drawable = AppCompatResources.getDrawable(getContext(), R.drawable.your_vector_drawable);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
}

Thanks for reading, please feel free to comment with this post.

--

--

Leo N
Leo N

Written by Leo N

🇻🇳 🇸🇬 🇲🇾 🇦🇺 🇹🇭 Engineer @ GXS Bank, Singapore | MSc 🎓 | Technical Writer . https://github.com/nphausg

No responses yet