Хочу получить экземпляр Glide, для создания которого нужен context.
Мой AppComponent:
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
@Component.Factory
interface Factory{
fun create(@BindsInstance applicationContext: Context): AppComponent
}
fun getPreviewComponent(): PreviewComponent.Factory
fun getDetailComponent(): DetailComponent.Factory
fun inject(mainActivity: MainActivity)
fun inject(searchSettingsFragment: SearchSettingsFragment)
}
Application класс:
class BaseApplication : Application() {
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.factory().create(applicationContext)
}
}
AppModule
@Module
class AppModule () {
@Provides
fun provideApplicationContext(applicationContext: Context): Context {
return applicationContext
}
@Singleton
@Provides
fun provideGlideInstance(applicationContext: Context): Glide {
return Glide.get(applicationContext)
}
}
При инжекте Glide в активити получаю ошибку компиляции. Что я делаю не так?
/home/x/GIT/Nasa/app/build/tmp/kapt3/stubs/debug/com/nasa/app/di/AppComponent.java:8: error: [Dagger/DependencyCycle] Found a dependency cycle:
public abstract interface AppComponent {
^
android.content.Context is injected at
com.nasa.app.di.AppModule.provideApplicationContext(applicationContext)
android.content.Context is injected at
com.nasa.app.di.AppModule.provideGlideInstance(applicationContext)
com.bumptech.glide.Glide is injected at
com.nasa.app.ui.MainActivity.glide
com.nasa.app.ui.MainActivity is injected at
> Task :app:kaptDebugKotlin FAILED
com.nasa.app.di.AppComponent.inject(com.nasa.app.ui.MainActivity)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at
https://help.gradle.org
BUILD FAILED in 20s