Jorge Gil
Jorge Gil

Reputation: 2865

ResourcesNotFoundException for vector drawable in API 21+ in library module

I'm seeing a few crashes in production from a missing vector drawable. I've seen quite a few similar questions but they're all related to pre-lollipop compatibility and my app's minSDK is 21.

It might be a bad misunderstanding of when to use android:src vs app:srcCompat and vectorDrawables.useSupportLibrary true

My :app's build.gradle file has vectorDrawables.useSupportLibrary true:

android {
  compileSdkVersion androidCompileSdkVersion
  buildToolsVersion androidBuildToolsVersion
  defaultConfig {
    applicationId "com.company.packagename"
    minSdkVersion androidMinSdkVersion
    targetSdkVersion androidTargetSdkVersion
    versionCode appVersionCode
    versionName appVersionName
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    vectorDrawables {
        useSupportLibrary true
    }
  }
}

While the library module that this fragment/xml/drawable lives is does not:

apply plugin: 'com.android.library'
android {
    compileSdkVersion androidCompileSdkVersion
    defaultConfig {
        minSdkVersion androidMinSdkVersion
        targetSdkVersion androidTargetSdkVersion
        versionCode appVersionCode
        versionName appVersionName
    }
}

Could that be the cause? Is that flag even relevant if my minSDK is 21?

My XML:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/blue_radial_gradient"
    tools:context=".home.AuthHomeFragment">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="47dp"
        android:layout_marginBottom="40dp"
        app:srcCompat="@drawable/logo"
        app:layout_constraintBottom_toTopOf="@+id/authWithFacebookButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent" />

    <include layout="@layout/facebook_button"
        android:id="@+id/authWithFacebookButton"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:background="@drawable/kinedu_button_background"
        android:backgroundTint="@color/com_facebook_blue"
        android:focusable="true"
        android:foreground="@drawable/ripple_action_black_foreground"
        android:gravity="center"
        android:maxWidth="300dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the stacktrace:

Caused by android.view.InflateException: Binary XML file line #11: Error inflating class ImageView
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
   at com.company.auth.home.AuthHomeFragment.onCreateView(AuthHomeFragment.kt:71)
   at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2440)
   at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:885)
   at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1229)

Caused by android.content.res.Resources$NotFoundException: Resource ID #0x7f0801f1
   at android.content.res.Resources.getValue(Resources.java:1266)
   at androidx.appcompat.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
   at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
   at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
   at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
   at androidx.appcompat.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
   at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
   at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
   at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
   at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
   at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
   at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
   at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:180)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
   at com.company.auth.home.AuthHomeFragment.onCreateView(AuthHomeFragment.kt:71)
   at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2440)

Upvotes: 1

Views: 145

Answers (1)

nsaudria
nsaudria

Reputation: 697

I think the resource not found is coming from android:background. Are you use drawable vector? I experienced this before, you can wrap the drawable vector use layer list. for example we named this file as bg_kinedu_btn on drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/kinedu_button_background"/>
</layer-list>

Then you can call it with:

<include layout="@layout/facebook_button"
    android:id="@+id/authWithFacebookButton"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginStart="32dp"
    android:layout_marginEnd="32dp"
    android:background="@drawable/bg_kinedu_btn"
    android:backgroundTint="@color/com_facebook_blue"
    android:focusable="true"
    android:foreground="@drawable/ripple_action_black_foreground"
    android:gravity="center"
    android:maxWidth="300dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Hope this helps.

Upvotes: 1

Related Questions