title: android.view.InflateException: Class is not a Drawable x
body: <p>Our app has next exception in production:</p>
  <blockquote>
  <p>android.content.res.Resources$NotFoundException: File
  res/drawable/ic_snowflake.xml from drawable resource ID #0x7f080170 at
  android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:837)
  at
  android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:659)
  at
  android.content.res.Resources.getDrawableForDensity(Resources.java:891)
  at android.content.res.Resources.getDrawable(Resources.java:833)  at
  android.content.Context.getDrawable(Context.java:605)  at
  androidx.core.content.ContextCompat$Api21Impl.getDrawable(ContextCompat.java:862)
  at
  androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:480)</p>
  </blockquote>
  <p>caused by:</p>
  <blockquote>
  <p>android.view.InflateException: Class is not a Drawable x at
  android.graphics.drawable.DrawableInflater.inflateFromClass(DrawableInflater.java:218)
  at
  android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:143)
  at
  android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1322)
  at
  android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1281)
  at
  android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:824)
  at
  android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:659)
  at
  android.content.res.Resources.getDrawableForDensity(Resources.java:891)
  at android.content.res.Resources.getDrawable(Resources.java:833) at
  android.content.Context.getDrawable(Context.java:605) at
  androidx.core.content.ContextCompat$Api21Impl.getDrawable(ContextCompat.java:862)
  at
  androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:480)</p>
  </blockquote>
  <p>caused by:</p>
  <blockquote>
  <p>java.lang.ClassCastException: class x cannot be cast to
  android.graphics.drawable.Drawable at
  java.lang.Class.asSubclass(Class.java:2467) at
  android.graphics.drawable.DrawableInflater.inflateFromClass(DrawableInflater.java:205)
  at
  android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:143)
  at
  android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1322)
  at
  android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1281)
  at
  android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:824)
  at
  android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:659)
  at
  android.content.res.Resources.getDrawableForDensity(Resources.java:891)
  at android.content.res.Resources.getDrawable(Resources.java:833) at
  android.content.Context.getDrawable(Context.java:605) at
  androidx.core.content.ContextCompat$Api21Impl.getDrawable(ContextCompat.java:862)
  at
  androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:480)</p>
  </blockquote>
  <p>Analyze production apk I found that file <em>res/drawable/ic_snowflake.xml</em> has next content:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;x /&gt;
  </code></pre>
  <p>This happens because resource shrinker thinks that the specified resource is not used anywhere and replace it original content to <code>&lt;x/&gt;</code>. But that drawable is used by our next code:</p>
  <pre><code>class StateToViewModel() {
          
  }
          
  fun MyEnum.getIcon(): Int? {
      return when (this) {
          MyEnum.UNKNOWN -&gt; null
          MyEnum.ONE -&gt; R.drawable.ic_snowflake
          MyEnum.TWO -&gt; R.drawable.ic_lock
      }
  }
  </code></pre>
  <p>If we are place <em>getIcon</em> extension function inside companion object file <em>res/drawable/ic_snowflake.xml</em> save original content and crash goes away</p>
  <pre><code>class StateToViewModel() {
      companion object {
          fun MyEnum.getIcon(): Int? {
              return when (this) {
                  MyEnum.UNKNOWN -&gt; null
                  MyEnum.ONE -&gt; R.drawable.ic_snowflake
                  MyEnum.TWO -&gt; R.drawable.ic_lock
              }
          }
      }
  }
  </code></pre>
  <p>My question - why this happens? Why place where function is declared so important?</p>
  <pre><code>buildToolsVersion: 30.0.2 
  kotlin-gradle-plugin: 1.4.30
  minSdkVersion: 21
  compileSdkVersion: 30
  targetSdkVersion: 30
  </code></pre>