I use vector drawables on Android 4+ from XML and code without any issues.
Setup: Android Gradle Plugin 3.2.1, Android Studio 3.2.1, Gradle 4.10.1, android-x support library with Jetifier on.
Example usage:
app:srcCompat="@drawable/some_vector_drawable"
android:drawableLeft="@drawable/some_vector_drawable"
view.setBackgroundResource(R.drawable.some_vector_drawable)
Many official or less official blogs or stack overflow posts mention usage of:
android.defaultConfig.vectorDrawables.useSupportLibrary = true
in build.gradle
and/or: AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
from code
but my vector drawables run everywhere fine without any of these.
Is there any official, final documentation on this? Are these flags necessary?
The only thing I notice is this warning in Android Studio, which I ignore:
to use vector drawable compat you need to set defaultConfig.vectorDrawables.useSupportLibrary = true
Without:
android.defaultConfig.vectorDrawables.useSupportLibrary = true
in build.gradle
, PNGs are generated for each vector drawable and become part of resulting .apk file.
With the above flag on, following is possible:
app:srcCompat="@drawable/some_vector_drawable"
in layout xmlandroid:icon="@drawable/some_vector_drawable"
in menu xmlimageView.setImageResource(R.drawable.some_vector_drawable)
in code
(but vector drawable needs to be a single vector (i.e.: start with <vector
))
getting VectorDrawable from code:
Drawable vectorDrawable = VectorDrawableCompat.create(resources, R.drawable.some_vector_drawable, null)
This can then be used as regular drawable (e.g.: imageView.setImageDrawable(vectorDrawable)
)
With both:
android.defaultConfig.vectorDrawables.useSupportLibrary = true
& AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
together, VectorDrawables
work pretty much as if they were native on Android 4+.
However, Google warns that using AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) may cause memory issues or issues with Configuration
object (I have not experienced any).