You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio Dolphin中BindingAdapter类未生成编译报错求助

解决Android Studio Dolphin中@BindingAdapter编译错误的排查方案

基础配置核查

  • 确认Module级build.gradle中启用数据绑定:
    android {
        buildFeatures {
            dataBinding true
        }
    }
    
  • 确保kotlin-kapt插件正确配置:
    项目级build.gradle中声明插件版本:
    plugins {
        id 'com.android.application' version '7.3.0' apply false
        id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
        id 'kotlin-kapt' version '1.7.20' apply false
    }
    
    Module级build.gradle中应用插件:
    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
        id 'kotlin-kapt'
    }
    

@BindingAdapter代码规范检查

  • 必须使用androidx.databinding.BindingAdapter注解,避免使用旧版android.databinding.BindingAdapter
  • Kotlin中绑定方法需标记@JvmStatic(伴生对象或顶层函数均可):
    object BindingAdapters {
        @JvmStatic
        @BindingAdapter("app:customText")
        fun setCustomText(view: TextView, text: String?) {
            view.text = text ?: ""
        }
    }
    
  • XML中属性名需与注解内的名称完全匹配,同时确认根节点已声明app命名空间:
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:customText="@{viewModel.text}" />
    </layout>
    

编译缓存清理

  • 执行Build > Clean Project,再执行Build > Rebuild Project
  • 清除IDE缓存:File > Invalidate Caches... > Invalidate and Restart

版本兼容性检查

  • 确保Android Gradle Plugin(AGP)版本与Kotlin版本匹配:Dolphin对应AGP 7.3.x,建议搭配Kotlin 1.7.x
  • 无需手动添加databinding依赖,AGP会自动引入对应兼容版本的库

内容的提问来源于stack exchange,提问作者Fazal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 07:55:28