O
O
overfortune2021-03-28 12:26:24
Android
overfortune, 2021-03-28 12:26:24

How to DI for application context?

I want to get a Glide instance, which needs a context to create.

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


When I inject Glide into an activity, I get a compilation error. What am I doing wrong?

/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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-03-28
@overfortune

He writes to you that the cycle is in the graph.

Provides
fun provideApplicationContext(applicationContext: Context): Context

Is this what you want to achieve? You want to get the context from the graph into the method, and you want to give the context to the graph. What is this for?
This method is generally superfluous, you made
BindsInstance, the context after that is already in the graph.
And why do you have an app component that injects a fragment and an activity? Each of them must have its own component, Subcomponent app component. Otherwise it will just be a dump. And in any case, a situation will happen when you need something in the graph that is not and cannot be in the application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question