最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - @BindingAdapter not found no matter what I try - Stack Overflow

programmeradmin1浏览0评论

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 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=";
    xmlns:app=";
    xmlns: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.conventionpose.dependency)
        id("kotlin-kapt")
        kotlin("kapt")
        id("kotlin-parcelize")
    }

android {
    resourcePrefix = "dl_"
    namespace = "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.lottiepose)
    implementation(libs.playservices.maps)
    implementation(libs.result)
    implementation(libs.result.coroutines)
    implementation(libs.rxjava)

    kapt(libspiler)

}

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)

}
Share Improve this question edited Feb 6 at 14:41 Yarneo asked Feb 6 at 8:38 YarneoYarneo 3,00224 silver badges33 bronze badges 2
  • This error indicates that attributes were not added using declare-styleable, can you confirm if you have them specified or missing? – dmortal Commented Feb 6 at 17:58
  • From what I've read, declare-styleable is only needed for custom views, whereas here im using an ImageView – Yarneo Commented Feb 7 at 8:42
Add a comment  | 

1 Answer 1

Reset to default 1

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>
发布评论

评论列表(0)

  1. 暂无评论