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>