Dagger2编译问题在kotlin

我正在尝试使用Dagger2学习依赖注入。 我创建了几个自定义范围和限定符。 我创建了一个应用程序组件,我想要注入某些全局依赖关系,并创建了一个基于活动上下文返回某些实例的Activity组件。

现在,当我尝试在活动中注入全局实例时,出现如下错误:

e:/home/arifur/source_code/me/myprojects/Learn/studio/Android/kotlin/FirebaseChat/app/build/tmp/kapt3/stubs/debug/com/example/arifur/firebasechatkotlin/dependecies/component/ActivityComponent.java :7:error:com.google.firebase.auth.FirebaseAuth不能提供@ Provide或@ Produces注释的方法。 E:

e:public abstract void inject(@ org.jetbrains.annotations.NotNull()e:
^ e:com.google.firebase.auth.FirebaseAuth在e:
com.example.arifur.firebasechatkotlin.BaseActivity.firebaseAuth e:
com.example.arifur.firebasechatkotlin.MainActivity在e:
com.example.arifur.firebasechatkotlin.dependecies.component.ActivityComponent.inject(P0)

w:警告:以下选项未被任何处理器识别:'[kapt.kotlin.generated]'w:

e:/home/arifur/source_code/me/myprojects/Learn/studio/Android/kotlin/FirebaseChat/app/src/main/java/com/example/arifur/firebasechatkotlin/BaseActivity.kt:(29,9):Unresolved参考:DaggerActivityComponent e:/home/arifur/source_code/me/myprojects/Learn/studio/Android/kotlin/FirebaseChat/app/src/main/java/com/example/arifur/firebasechatkotlin/FirebaseChatApp.kt:(20,13 ):未解决的参考:DaggerFirebaseComponent e:/home/arifur/source_code/me/myprojects/Learn/studio/Android/kotlin/FirebaseChat/app/src/main/java/com/example/arifur/firebasechatkotlin/MainActivity.kt 🙁 1,1):处理注释时发生了一些错误。 请看上面的错误信息。


预选赛

ActivityContext.kt

@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class ActivityContext 

ApplicationContext.kt

 @javax.inject.Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class ApplicationContext 

领域

ActivityScope.kt

 @Scope @Retention(AnnotationRetention.SOURCE) annotation class ApplicationScope 

ApplicationScope.kt

 @Scope @Retention(AnnotationRetention.SOURCE) annotation class ApplicationScope 

组件

ActivityComponent.kt用于注入Activity类。

 @ActivityScope @Component(modules = arrayOf(ActivityModule::class), dependencies = arrayOf(FirebaseComponent::class)) interface ActivityComponent { fun inject(mainActivity: MainActivity) } 

FirebaseComponent.kt用于注入Application类。

 @ApplicationScope @Component(modules = arrayOf(ApplicationModule::class, FirebaseAuthModule::class, NetworkModule::class)) interface FirebaseComponent { fun inject(app: FirebaseChatApp) } 

模块

ActivityModule.kt

 @Module class ActivityModule(val activity : Activity) { var mActivityRef : WeakReference<Activity> ; init { mActivityRef = WeakReference<Activity>(activity) } @Provides @ActivityScope @ActivityContext public fun getActivityContext() : Context? = mActivityRef.get() @Provides @ActivityScope @ActivityContext public fun getActivityInstance() : Activity? = mActivityRef.get(); @Provides @ActivityScope public fun getGlideActivityInstance(@ActivityContext context : Context?) : Glide { Log.i("activityModule", "Returning activity glide") return Glide.get(context); } } 

ApplicationModule.kt

 @Module class ApplicationModule(val app : FirebaseChatApp) { @ApplicationContext @ApplicationScope @Provides fun provideContext() : Context = app @ApplicationInstance @ApplicationScope @Provides fun provideApplication() : Application = app } 

FirebaseAuthModule.kt

 @Module class FirebaseAuthModule { @Provides @ApplicationScope public fun getFirebaseAuth() : FirebaseAuth = FirebaseAuth.getInstance() } 

NetworkModule.kt

 @Module class NetworkModule { @Provides @ApplicationScope public fun getGlideApplicationInstance(@ApplicationContext applicationContext: Context ) : Glide { return Glide.get(applicationContext); } } 

FirebaseChatApp.kt一个应用程序类

 class FirebaseChatApp : Application() { val firebaseComponent: FirebaseComponent by lazy { DaggerFirebaseComponent .builder() .applicationModule(ApplicationModule(this)) .build() } @Inject lateinit var firebaseAuth : FirebaseAuth @Inject lateinit var glide : Glide override fun onCreate() { super.onCreate() firebaseComponent.inject(this) Log.i("application", "APP->Firebase Auth = " + firebaseAuth) Log.i("application", "APP->Glide = " + glide) } public fun getApplicationComponent() : FirebaseComponent = firebaseComponent; } 

BaseActivity.kt所有活动的超类

 public abstract class BaseActivity : AppCompatActivity(){ @Inject @ActivityScope protected lateinit var glide : Glide @Inject @ApplicationScope protected lateinit var firebaseAuth : FirebaseAuth //ERROR HERE protected val activityComponent: ActivityComponent by lazy { DaggerActivityComponent .builder() .activityModule(ActivityModule(this)) .firebaseComponent((this.application as FirebaseChatApp).getApplicationComponent()) .build() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) injectComponent(activityComponent) } abstract fun injectComponent(activityComponent: ActivityComponent) } 

MainActivity.kt对象应该被注入的地方。

 class MainActivity : BaseActivity() { override fun injectComponent(activityComponent: ActivityComponent) { activityComponent.inject(this) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } 

我该怎么做才能解决这个错误。 那么使用DI的正确方法是什么? 因为我是新手。 我感到有点困惑。

问题在于您正在尝试使用组件依赖关系,但是您已经跳过添加您的子范围组件可以继承的供应方法。

 @ApplicationScope @Component(modules = arrayOf(ApplicationModule::class, FirebaseAuthModule::class, NetworkModule::class)) interface FirebaseComponent { fun firebaseAuth() : FirebaseAuth // <-- this is missing fun inject(app: FirebaseChatApp) }