我正在尝试使用Dagger2学习依赖注入。我创建了一些自定义范围和限定符。我创建了一个应用程序组件,我想要注入某些全局依赖项,并且我创建了一个Activity组件,它将根据活动上下文返回某些实例。
现在当我尝试在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 : 错误:无法提供com.google.firebase.auth.FirebaseAuth 没有@ Provide-或@ Produces-annotated方法。 e:
e:公共抽象无效 注入(@ 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):未解决的参考: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 用于注入活动类。
@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的正确方法。因为我是新手。我有点困惑。
答案 0 :(得分:1)
问题是您正在尝试使用组件依赖项,但您已跳过添加子范围组件可以继承的提供方法。
@ApplicationScope
@Component(modules = arrayOf(ApplicationModule::class, FirebaseAuthModule::class, NetworkModule::class))
interface FirebaseComponent {
fun firebaseAuth() : FirebaseAuth // <-- this is missing
fun inject(app: FirebaseChatApp)
}