androidandroid-databindingandroid-binding-adapter

@BindingAdapter not found no matter what I try


I'm new to Android Development so hopefully my issue will be an easy fix.

I've been trying to play around with @BindingAdapter in an existing project, but no matter where I place the binding adapter function and what I put in build.gradle.kts, I keep getting the error: AAPT: error: attribute foo (aka com.company.android.master:foo) not found.

This is my current attempt (project is a multiple-modules project, and this code is all within one of these modules):

FooModule.kt:

@BindingAdapter("app:foo")
fun setFoo(view: ImageView, foo: String) {
    print(foo)
}

foo_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<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/clRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primary"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/llSearchContainer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/rect_surface6_round8"
        android:elevation="24dp"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/ivHideSearch"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_margin="8dp"
            android:background="@drawable/ripple_dark_circle"
            android:contentDescription="@string/wat"
            android:padding="8dp"
            app:foo="@{`foo`}"
            app:tint="@color/icon_primary" />
    ...

build.gradle.kts:

plugins {
        alias(libs.plugins.convention.android.library)
        alias(libs.plugins.convention.compose.dependency)
        id("kotlin-kapt")
        kotlin("kapt")
        id("kotlin-parcelize")
    }

android {
    resourcePrefix = "dl_"
    namespace = "com.company.foo"
    buildFeatures {
        dataBinding = true
        viewBinding = true
    }
    buildFeatures.viewBinding = true

    viewBinding.isEnabled = true
}

dependencies {
    implementation(projects.appResources)

    implementation(libs.androidx.appcompat)
    implementation(libs.androidx.constraintlayout)
    implementation(libs.androidx.ktx)
    implementation(libs.androidx.recyclerview)
    implementation(libs.androidx.transition)
    implementation(libs.koin.android)
    implementation(libs.lottie)
    implementation(libs.lottie.compose)
    implementation(libs.playservices.maps)
    implementation(libs.result)
    implementation(libs.result.coroutines)
    implementation(libs.rxjava)

    kapt(libs.compiler)

}

Solution

  • The error is that the root layout tag is not used, that's why data binding doesn't work:

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clRoot"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/primary"
            android:clickable="true"
            android:focusable="true"
            android:orientation="vertical">
    
            <LinearLayout
                android:id="@+id/llSearchContainer"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:background="@drawable/rect_surface6_round8"
                android:elevation="24dp"
                android:gravity="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
    
            <ImageView
                android:id="@+id/ivHideSearch"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="8dp"
                android:background="@drawable/ripple_dark_circle"
                android:contentDescription="@string/wat"
                android:padding="8dp"
                app:foo="@{`foo`}"
                app:tint="@color/icon_primary" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </layout>