This answer draws on the article AppCompat - Age of the Vectors by Chris Banes(who works on the Support Library). For this question we're looking specifically at the section titled The 'magic' way.
The crash you're experiencing is because the Support Library only allows some ways of using VectorDrawables by default, and layer-list is not one of them.
There is a specific code block you can add to the top of your Activity to enable other VectorDrawable
use such as <layer-list>
:
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Note: the linked article contains a typo in this method name, using "FromSources", it should be "FromResources" as shown above.
You would need to add this to each Activity where you want to use such drawables, or perhaps include it in a BaseActivity class that your other Activities extend from.
Per the article, this should mean the following will now work:
DrawableContainers which reference other drawables resources which contain only a vector resource.
...StateListDrawable...InsetDrawable, LayerDrawable, LevelListDrawable and RotateDrawable.
It should be noted though, this method is heavily couched with the word 'may', this may work, and it is not enabled by default, so be aware and check it's really working for you!
Now there's actually another dimension to this question, credit to other users Selim Ajimi and Rapunzel Van Winkle for addressing this in their answers. <layer-list>
has some different behaviour between the API's, in particular the width
and height
attributes of your <item>
only being supported in API 23+. This is not the cause of your crash, nor will it cause your app to crash, but will mean that your image will not look as intended once you have it functioning in earlier APIs.
The suggestion from Rapunzel Van Winkle does indeed seem to be a good way to position the drawable correctly across APIs (tested on API 16 and 24):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#bdbdbd" />
<size
android:width="60dp"
android:height="60dp" />
</shape>
</item>
<item
android:drawable="@drawable/ic_library_books_black_24dp"
android:top="10dp"
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
>
</item>
</layer-list>