一、报错信息



在 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 注解 , 在上述基础上 , 再次使用 @BindingAdapter("image") 注解 为布局设置数据绑定适配器 ;

@BindingAdapter("image") 处报如下错误 :

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'
}

添加完毕后 , 上述报错信息消失 ;

在这里插入图片描述

点击阅读全文
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐