如何解决在Android中实现Dagger 2的InjectedFieldSignature错误?

问题描述

在使用dagger 2之前,我已经在android中实现了依赖注入,但是最近,我尝试在新项目中使用它,但是出现以下错误

错误:找不到符号 导入dagger.internal.InjectedFieldSignature; ^ 符号:类InjectedFieldSignature 位置:包dagger.internal / location / to / App_MembersInjector.java:30:错误:找不到符号

这是我的应用程序组件:

@Singleton
@Component(
    modules = [
        (AndroidInjectionModule::class),(VmModule::class),(InjectorModule::class),]

)
interface ApplicationComponent: AndroidInjector<Application> {

@Component.Builder
interface Builder{
    @BindsInstance
    fun application(application: App): Builder
    fun build() : ApplicationComponent
}

    fun inject(home: Home)
}

然后在我的App课中:

class App: Application(),HasAndroidInjector {

@Inject
lateinit var  anAndroidInjector: dispatchingAndroidInjector<Any>



override fun onCreate() {
    super.onCreate()
    DaggerApplicationComponent.builder().application(this).build().inject(this)
}

override fun androidInjector(): AndroidInjector<Any> {
    return anAndroidInjector
}

}

然后是进样器模块:

@Module
abstract class InjectorModule {
  @ContributesAndroidInjector
  abstract fun bindHomeActivity(): Home
}

以下是我的应用Gradle的一小段摘录,以显示匕首版本:

implementation 'com.google.dagger:dagger-android:2.24'
implementation 'com.google.dagger:dagger-android-support:2.24'
kapt 'com.google.dagger:dagger-android-processor:2.24'
kapt 'com.google.dagger:dagger-compiler:2.28'

如果您有任何线索,请告诉我问题可能在哪里。

解决方法

您的Dagger神器版本不匹配。具体来说,您正在使用dagger-compiler:2.28生成代码,但改为包含对Dagger 2.24的依赖。

dagger.internal.InjectedFieldSignature的特定情况下,该类似乎已在Dagger 2.25.3中引入。 Dagger编译器的任何更高版本都期望InjectedFieldSignature存在并且可以在生成的代码中使用。但是,由于您仅在项目中包括Dagger 2.24,因此生成的代码最终将引用一个不存在的类。

要解决此问题,请确保所有Dagger依赖项都使用相同的版本。