Android Kotlin: Overload Resolution Ambiguity

July 24, 2020

This happends when you define the same view id in two different layout, and you try to import both layout.

Layout

Notice the following layout shared the same view id of imageDraweeView and selectionImageView.

res/layout/batch_item_image.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tool="http://schemas.android.com/tools"
    xmlns:fresco="http://schemas.android.com/apk/res-auto">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/imageDraweeView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        fresco:viewAspectRatio="1"
        fresco:placeholderImage="@drawable/placeholder_100"
        />

    <ImageView
        android:id="@+id/selectionImageView"
        android:layout_gravity="right|top"
        android:layout_margin="8dp"
        android:src="@drawable/ic_baseline_radio_button_unchecked_24"
        android:tint="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

</FrameLayout>

res/layout/batch_item_image_with_text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tool="http://schemas.android.com/tools"
    android:orientation="horizontal"
    xmlns:fresco="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/imageDraweeView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            fresco:viewAspectRatio="1"
            fresco:placeholderImage="@drawable/placeholder_100"
            />

        <ImageView
            android:id="@+id/selectionImageView"
            android:layout_gravity="right|top"
            android:layout_margin="8dp"
            android:src="@drawable/ic_baseline_radio_button_unchecked_24"
            android:tint="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            />
    </FrameLayout>


    <TextView
        android:id="@+id/imageTextView"
        android:padding="16dp"
        tool:text="Hello World"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Body1"
        android:layout_height="wrap_content" />

</LinearLayout>

Solution 1: use Different View Id

For res/layout/batch_item_image.xml, define imageDraweeView and selectionImageView as per current.

For res/layout/batch_item_image_with_text.xml, define withTextImageDraweeView and withTextSelectionImageView instead.

Solution 2: explicit import

Use explicit kotlin synthetic import.

import kotlinx.android.synthetic.main.batch_item_image.selectionImageView   // explicit import to solve overload resolution ambiguity
import kotlinx.android.synthetic.main.batch_item_image.imageDraweeView      // explicit import to solve overload resolution ambiguity
import kotlinx.android.synthetic.main.batch_item_image.*
import kotlinx.android.synthetic.main.batch_item_image_with_text.*
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.