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

Kotlin项目用Glide遇Unresolved reference: CustomTarget错误求解决

修复Glide中"Unresolved reference: CustomTarget"错误

1. 补全核心依赖并统一版本

你的Gradle依赖存在两个关键问题:缺少Glide核心库、各依赖版本不统一。将依赖修改为统一版本(推荐使用最新稳定版,以下以4.16.0为例):

// Glide核心库
implementation 'com.github.bumptech.glide:glide:4.16.0'
// Glide注解库
implementation 'com.github.bumptech.glide:annotations:4.16.0'
// OkHttp集成库(若无需可删除)
implementation 'com.github.bumptech.glide:okhttp3-integration:4.16.0'
// 注解处理器
kapt 'com.github.bumptech.glide:compiler:4.16.0'

注意:所有Glide相关依赖的版本号必须完全一致,版本冲突会导致类加载异常。

2. 添加CustomTarget的导入语句

在使用CustomTarget的Kotlin文件顶部,补充必要的导入:

import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import android.graphics.Bitmap
import android.graphics.drawable.Drawable

3. 重新构建项目

执行以下操作确保依赖和代码生效:

  • 点击Android Studio工具栏的Build -> Rebuild Project
  • 若问题仍存在,再次执行File -> Invalidate Caches... -> Invalidate and Restart

验证代码

修改后的完整代码示例:

import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import android.graphics.Bitmap
import android.graphics.drawable.Drawable

// ... 你的业务逻辑代码
Glide.with(context)
    .asBitmap()
    .load(url)
    .into(object : CustomTarget<Bitmap>() {
        override fun onLoadCleared(placeholder: Drawable?) {
            // 资源清空时的处理逻辑
        }

        override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
            // 图片加载完成后的处理逻辑,比如使用Bitmap
        }
    })

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 00:50:32