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

Hilt 2.44.2为GlanceAppWidgetReceiver注入依赖时出现类找不到异常

问题:Glance组件结合Hilt注入时构建失败,找不到Hilt生成的Receiver类

我使用Glance API开发应用桌面组件,尝试给继承自GlanceAppWidgetReceiver的OverviewReceiver添加@AndroidEntryPoint注解实现依赖注入,但Debug模式下构建失败,抛出FileNotFoundException,提示找不到Hilt_OverviewReceiver.class文件。目前Activity和ViewModel的依赖注入功能正常,希望排查问题原因。

错误核心日志

Caused by: java.io.FileNotFoundException: /Users/hieuvu/Projects/go-focus/app/build/intermediates/javac/debug/classes/com/hieuwu/gofocus/widget/Hilt_OverviewReceiver.class (No such file or directory)

相关代码

@AndroidEntryPoint
class OverviewReceiver: GlanceAppWidgetReceiver() {
...
}

已尝试的无效方案

  • 参考广播接收器结合Hilt注入的实现方案
  • 参考Glance组件开发的相关教程

可能的原因及排查方向

  1. Hilt注解处理器未正确生成代理类

    • 检查项目根目录和模块的build.gradle,确认已正确应用Hilt插件(id 'com.google.dagger.hilt.android'),且hilt-compiler依赖版本与Hilt核心库版本一致。
    • 执行Clean Project后再Rebuild Project,清理构建缓存,避免缓存导致的生成文件缺失。
  2. GlanceAppWidgetReceiver的特殊性导致Hilt生成逻辑失效
    GlanceAppWidgetReceiver属于Jetpack特殊组件,Hilt可能对其直接注解支持存在兼容性问题。可以尝试手动实现注入逻辑替代@AndroidEntryPoint:

    class OverviewReceiver: GlanceAppWidgetReceiver() {
        private lateinit var yourDependency: YourDependency
    
        override fun onReceive(context: Context?, intent: Intent?) {
            context?.let {
                val entryPoint = EntryPointAccessors.fromApplication(
                    it.applicationContext,
                    YourEntryPoint::class.java
                )
                yourDependency = entryPoint.getYourDependency()
            }
            super.onReceive(context, intent)
        }
    }
    
    @InstallIn(SingletonComponent::class)
    @EntryPoint
    interface YourEntryPoint {
        fun getYourDependency(): YourDependency
    }
    
  3. 构建配置或路径问题

    • 确认模块build.gradle已开启Java 8+支持,Hilt注解处理器依赖该版本:
      compileOptions {
          sourceCompatibility JavaVersion.VERSION_1_8
          targetCompatibility JavaVersion.VERSION_1_8
      }
      kotlinOptions {
          jvmTarget = "1.8"
      }
      
    • 检查OverviewReceiver的包名与错误日志中的路径是否一致,避免包名拼写错误导致生成类的路径不匹配。
  4. Hilt与Glance版本不兼容
    检查Hilt和Glance的版本组合是否存在冲突,尝试升级或降级两者到官方推荐的兼容版本(例如Hilt 2.44+搭配Glance 1.0.0+)。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 21:34:59