0

In my Android Studio project, I receive warnings on any app:srcCompat="@drawable/..." attributes in my XML layouts:

<ImageView
    android:id="@+id/leftNav"
    android:layout_width="wrap_content"
    android:layout_height="46dp"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:adjustViewBounds="true"
    android:clickable="true"
    android:focusable="true"
    android:onClick="leftNav_Click"
    android:scaleType="fitCenter"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_chevron_left_black_24dp"<!--warning-->
    tools:ignore="ContentDescription"
    />

Warning:
Inspection info:To use VectorDrawableCompat, you need to make two modifications to your project. First, set android.defaultConfig.vectorDrawables.useSupportLibrary = true in your build.gradle file, and second, use app:srcCompat instead of android:src to refer to vector drawables.

So I take these steps, as indicated in the build.gradle app file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.testappbehaviourchart"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        android.defaultConfig.vectorDrawables.useSupportLibrary = true //added to resolve VectorDrawableCompat issue
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha03'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.viewpager:viewpager:1.0.0'
    implementation "com.android.support:support-v4:28.0.0"
    implementation "com.android.support:support-compat:28.0.0"
    implementation 'com.google.android.material:material:1.0.0'

}

That seems to suppress the warnings, however, I now receive a new fatal runtime exception on inflate:

`E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testappbehaviourchart, PID: 21284
    android.view.InflateException: Binary XML file line #165: Error inflating class <unknown>`

I've narrowed this down to any android:background="@drawable/..." attributes like the one shown here from inside a TableRow inside a TableLayout:

<View
    android:id="@+id/view_m1"
    android:layout_width="match_parent"
    android:layout_height="82dp"
    android:background="@drawable/ic_simplebox"
    android:minWidth="82dp"
    tools:layout_editor_absoluteX="105dp"
    tools:layout_editor_absoluteY="101dp"
    /> 

These background drawables were working fine before I added that line to the gradle file.

Notes:

  • I think I could just add tools:ignore="VectorDrawingCompat" to ignore the first warning, but I'm not sure of the repercussions that would bring.
  • I need to keep the minSdkVersion as 16.
  • This is the @drawable/ic_simplebox XML:
<vector android:height="24dp" android:viewportHeight="100"
    android:viewportWidth="100" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#E0171C20" android:name="Shape 1 copy" android:pathData="m20 100v-5h60v5h-60z"/>
    <path android:fillColor="#E0171C20" android:name="Shape 1 copy 2" android:pathData="m0 20h5v60h-5v-60z"/>
</vector>
  • This is the @drawable/ic_chevron_left_black_24dp XML:
<vector android:height="24dp" android:tint="#667573"
    android:viewportHeight="24.0" android:viewportWidth="24.0"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M15.41,7.41L14,6l-6,6 6,6 1.41,-1.41L10.83,12z"/>
</vector>
n00dles
  • 255
  • 3
  • 18
  • I don't think so, this will work prior to lollipop version. Can u share the drawable files. – Goutham Apr 08 '19 at 17:56
  • Hi @Goutham I added the xml above. When you say "I don't think so", are you referring to the warning or the exception? – n00dles Apr 08 '19 at 18:08
  • As you have mentioned, your minimum sdk version is 16 but app:srcCompat will work from Android Support Library 23.3.0, So you have to handle the mobiles below lollipop right. "android:src" can help I guess. – Goutham Apr 08 '19 at 18:14
  • Does it matter that the XML layout containing a `android:background="@drawable/ic_simplebox"` (causes the inflater exception) is inflated on my PagerAdapter class? I tried adding [this](https://stackoverflow.com/a/45477907/4256973) to the PagerAdapter but that didn't work. – n00dles Apr 08 '19 at 19:16

2 Answers2

0

Add these dependencies to your app level gradle file:

dependencies {

    implementation 'com.mikhaellopez:circularimageview:3.2.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
}
tshimkus
  • 1,173
  • 2
  • 17
  • 24
0

Turns out you can't use Vector Drawables on backgrounds using the Support Library.

If you read closely, it hints to this here (by 'omittance', really):
Developer.Andriod - Vector drawables backward compatibility solution

Related:
Is that right to make android.view.InflateException by using Vector drawables in android:background: line in a ImageView

n00dles
  • 255
  • 3
  • 18