[Fixed] AAPT: error: '@tools:sample/avatars' is incompatible with attribute src (attr) reference|color – Android

by
Maya Patel
android android-developer-api android-studio

Quick Fix: Look for the android:src="@tools:sample/avatars" line in the ImageView tag and remove it.

The Problem:

I am new to Android development and I’m having trouble setting up an image view with my own image. I am getting the error message ‘AAPT: error: ‘@tools:sample/avatars’ is incompatible with attribute src (attr) reference|color’. I’d like to know what this error means, how to troubleshoot it, and where my mistake lies.

The Solutions:

Solution 1: Remove Redundant Image Source Reference

In Android Studio, when you drag and drop an ImageView component to the layout, it automatically includes a default avatar image source reference:
`android:src="@tools:sample/avatars"`.
This reference is intended to serve as a placeholder until you replace it with your own custom image. However, if you add your custom image by setting `app:srcCompat="@drawable/custom_image"`, but forget to remove the default `android:src=` line, it can cause the error you’re encountering: `’android:src=’ is incompatible with attribute src (attr) reference|color`.

To resolve this, simply remove the default `android:src="@tools:sample/avatars"` line from the ImageView tag in your layout XML. This will ensure that the ImageView only references your custom image, eliminating the incompatibility and allowing the layout to render correctly.

Solution 2: Simple Attribute Change

Instead of:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@tools:sample/avatars"
    app:srcCompat="@drawable/custom_img" />

Do:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/custom_img" />
  • Here, the error lies in the usage of tools:sample attribute. We’re using an incompatible attribute (@tools:sample/avatars) with the src attribute.

  • Instead of using the @tools:sample/avatars attribute, we need to use a drawable resource from our project. This is the @drawable/custom_img.

  • Note: Keep in mind that this solution is specific to the issue caused by the @tools:sample/avatars attribute. Make sure to thoroughly check the root cause of the error before applying this solution.

Q&A

What does the error ‘AAPT: error: ‘tools:sample/avatars’ is incompatible with attribute src (attr) reference|color’ mean?

In Android Studio, there is a default avatar image added when you drag and drop an ImageView, causing a conflict with custom images.

How to troubleshoot this error?

Remove the line ‘android:src="@tools:sample/avatars"’ from the ImageView tag in the XML layout file.

Where might my mistake lie?

You may have added both ‘android:src’ and ‘app:srcCompat’ attributes to the ImageView tag. Remove the ‘android:src’ attribute.

Video Explanation:

The following video, titled "AAPT: error: duplicate attribute. - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

AAPT: error: duplicate attribute. In this video your will learn what is reason of this error And How you resolve this error in Android ...