
【错误记录】Android Studio 编译报错 ( To use data binding annotations in Kotlin, apply the ‘kotlin-kapt‘ plu )
To use data binding annotations in Kotlin,apply the 'kotlin-kapt' plugin in your module's build.gradle一、报错信息二、kotlin-kapt 插件简介三、解决方案
·
一、报错信息
在 Android Studio 中开发 DataBinding , 使用 如下代码开发 绑定适配器 加载网络图片 :
import android.widget.ImageView
import androidx.databinding.BindingAdapter
class ImageViewBindingAdapter {
companion object{
@JvmStatic
@BindingAdapter("image")
fun setImage(imageView: ImageView, url: String) {
}
}
}
在 Kotlin 中 , 实现静态函数 , 需要 在 companion object 伴生对象中 , 使用 @JvmStatic 注解 , 在上述基础上 , 再次使用 注解 为布局设置数据绑定适配器 ;
在 处报如下错误 :
To use data binding annotations in Kotlin,
apply the 'kotlin-kapt' plugin in your module's build.gradle
如下图所示 :
二、kotlin-kapt 插件简介
kapt 英文全称为 " Kotlin Annotation Processing Tool " , Kotlin 语言注解处理工具 ;
kotlin-kapt 插件 是 Kotlin 编译器插件的一种 , 其作用是在编译时处理 注解 ;
借助 kotlin-kapt 插件 可以 在编译时 基于 注解 生成代码 , 如 : 在 DataBinding 中 , 使用生成的代码将数据模型绑定到布局文件中 ;
三、解决方案
在 Module 模块下的 build.gradle 构建脚本中 , 添加 kotlin-kapt
模块 , 这是 Kotlin 注解模块 ;
plugins {
id 'kotlin-kapt'
}
添加完毕后 , 上述报错信息消失 ;
点击阅读全文
更多推荐
活动日历
查看更多
直播时间 2025-02-26 16:00:00


直播时间 2025-01-08 16:30:00


直播时间 2024-12-11 16:30:00


直播时间 2024-11-27 16:30:00


直播时间 2024-11-21 16:30:00


目录
所有评论(0)