gpt4 book ai didi

android - kotlin 中的 Dagger2 编译问题

转载 作者:行者123 更新时间:2023-11-29 01:08:47 27 4
gpt4 key购买 nike

我正在尝试使用 Dagger2 学习依赖注入(inject)。我创建了一些自定义范围和限定符。我创建了一个应用程序组件,我想在其中注入(inject)某些全局依赖项,我还创建了一个 Activity 组件,它将根据 Activity 上下文返回某些实例。

现在,当我尝试在 Activity 中注入(inject)全局实例时,出现如下错误:

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 cannot be provided without an @Provides- or @Produces-annotated method. e:

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

w: warning: The following options were not recognized by any processor: '[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 reference: 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): Unresolved reference: 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): Some error(s) occurred while processing annotations. Please see the error messages above.


限定符

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 用于注入(inject) Activity 类。

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

FirebaseComponent.kt 用于注入(inject) 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 所有 Activity 的父类(super class)

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 对象应该被注入(inject)的地方。

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)
}

关于android - kotlin 中的 Dagger2 编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44913841/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com