Krzysztof Mamak
Krzysztof Mamak

Reputation: 63

How to provide context with Dagger 2?

I'm creating application which uses Dagger to inject some dependencies.

I created some module classes but one of them needs context. The problem is that i don't know how to provide context in a correct way. I tried a lot of solutions but everytime i get an exception:

@Component.Builder is missing setters for required modules or components: [com.mamak.geobaza.di.ContextModule]

What should I do to provide context in a correct way?

ApiModule.kt

package com.mamak.geobaza.di

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.mamak.geobaza.utils.AppConstans.BASE_URL
import dagger.Module
import dagger.Provides
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module(includes = [
    RxJavaModule::class,
    OkHttpModule::class
])
class ApiModule {
    @Provides
    @Singleton
    fun gson(): Gson {
        return GsonBuilder().create()
    }

    @Provides
    @Singleton
    fun gsonConverterFactory(gson: Gson): GsonConverterFactory {
        return GsonConverterFactory.create(gson)
    }

    @Provides
    @Singleton
    fun retrofit(
        okHttpClient: OkHttpClient,
        gsonConverterFactory: GsonConverterFactory,
        rxJava2CallAdapterFactory: RxJava2CallAdapterFactory
    ): Retrofit {
        return Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(BASE_URL)
            .addConverterFactory(gsonConverterFactory)
            .addCallAdapterFactory(rxJava2CallAdapterFactory)
            .build()
    }
}

InterfaceModule.kt

package com.mamak.geobaza.di

import android.content.Context
import com.mamak.geobaza.ui.`interface`.ProjectListItemInterfaceImpl
import dagger.Module
import dagger.Provides
import javax.inject.Singleton

@Module(includes = [
    ContextModule::class
])
class InterfaceModule {
    @Provides
    @Singleton
    fun projectListItemInterface(context: Context): ProjectListItemInterfaceImpl {
        return ProjectListItemInterfaceImpl(context)
    }
}

OkHttpModule.kt

package com.mamak.geobaza.di

import dagger.Module
import dagger.Provides
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

@Module
class OkHttpModule {
    @Provides
    @Singleton
    fun okHttpLoggingInterceptor(): HttpLoggingInterceptor {
        return HttpLoggingInterceptor()
            .setLevel(HttpLoggingInterceptor.Level.BODY)
    }

    @Provides
    @Singleton
    fun okHttpClient(httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build()
    }
}

ProjectApiModule.kt

package com.mamak.geobaza.di

import com.mamak.geobaza.network.api.ProjectApiService
import dagger.Module
import retrofit2.Retrofit

@Module(includes = [
    ApiModule::class
])
class ProjectApiModule {
    fun projectApiService(retrofit: Retrofit): ProjectApiService {
        return retrofit.create(ProjectApiService::class.java)
    }
}

RxJavaModule.kt

package com.mamak.geobaza.di

import dagger.Module
import dagger.Provides
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import javax.inject.Singleton

@Module
class RxJavaModule {
    @Provides
    @Singleton
    fun rxJavaCallAdapterFactory(): RxJava2CallAdapterFactory {
        return RxJava2CallAdapterFactory.create()
    }
}

ViewModelKey.kt

package com.mamak.geobaza.di

import androidx.lifecycle.ViewModel
import dagger.MapKey
import kotlin.reflect.KClass

@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)

ViewModelModule.kt

package com.mamak.geobaza.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.mamak.geobaza.factory.ViewModelFactory
import com.mamak.geobaza.ui.viewmodel.ProjectListViewModel
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap

@Module
internal abstract class ViewModelModule {
    @Binds
    internal abstract fun viewModelFactory(viewModelFactory: ViewModelFactory): ViewModelProvider.Factory

    @Binds
    @IntoMap
    @ViewModelKey(ProjectListViewModel::class)
    protected abstract fun projectListViewModel(projectListViewModel: ProjectListViewModel): ViewModel

}

ContextModule.kt

package com.mamak.geobaza.di

import android.content.Context
import dagger.Module
import dagger.Provides

@Module
class ContextModule constructor(val context: Context) {
    @Provides
    fun context(): Context {
        return context
    }
}

AppComponent.kt

package com.mamak.geobaza.di

import android.app.Application
import dagger.BindsInstance
import dagger.Component
import dagger.android.support.AndroidSupportInjectionModule
import javax.inject.Singleton

@Singleton
@Component(modules = [
    ApiModule::class,
    ViewModelModule::class,
    AndroidSupportInjectionModule::class,
    InterfaceModule::class,
    ContextModule::class
])
interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        @BindsInstance
        fun apiModule(apiModule: ApiModule): Builder

        @BindsInstance
        fun interfaceModule(interfaceModule: InterfaceModule): Builder

        @BindsInstance
        fun contextModule(contextModule: ContextModule): Builder

        fun build(): AppComponent
    }

    fun inject(appController: AppController)
}

AppController.kt

package com.mamak.geobaza.di

import android.app.Application

import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasAndroidInjector
import javax.inject.Inject

class AppController : Application(), HasAndroidInjector {
    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Any>

    override fun androidInjector(): AndroidInjector<Any> {
        return dispatchingAndroidInjector
    }

    override fun onCreate() {
        super.onCreate()
        DaggerAppComponent.Builder()
            .application(this)
            .apiModule(ApiModule())
            .interfaceModule(InterfaceModule())
            .contextModule(ContextModule(this))
            .build()
            .inject(this)
    }
}

Upvotes: 3

Views: 5369

Answers (4)

Pedro Varela
Pedro Varela

Reputation: 2425

All the code from previous answers is not longer necessary, simply use @ApplicationContext where context is needed:

@Provides
fun provideFirebaseAnalytics(@ApplicationContext context: Context) =
    FirebaseAnalytics.getInstance(context)

Upvotes: 0

Manu
Manu

Reputation: 422

By using the component factory method, you can avoid instantiating a module manually. Please check the example below.

@Component(
    modules = [ApplicationModule::class]
)
@Singleton
interface ApplicationComponent {

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance context: Context): ApplicationComponent
    }
}

And in your application class, initialize the object graph like the following

class MyApplication : Application() {
    lateinit var applicationComponent: ApplicationComponent

    override fun onCreate() {
        super.onCreate()

        applicationComponent = DaggerApplicationComponent.factory().create(this)
    }
}

Now the context will be available in your object graph to be injected anywhere.

Upvotes: 3

Zafer Celaloglu
Zafer Celaloglu

Reputation: 1418

add a new Module by adding below codes

@Module
class ApplicationModule(var app: App) {
    @Provides
    @Singleton
    fun provideApp(): App = app

    @Provides
    @Singleton
    fun provideContext(): Context = app.applicationContext
}

and then add this module into your component interface

@Component(modules = arrayOf(ApplicationModule::class))

lastly create your component in your Application(class App : android.app.Application()) class

val component by lazy {
    DaggerApplicationComponent.builder()
            .applicationModule(ApplicationModule(this))
            .build()
}

Upvotes: 0

iCantC
iCantC

Reputation: 3180

You are all good at using @Component.Builder but it can be further optimized.

Following are the changes:

Step 1 : Use @Binds in Context Module to provide Context

package com.mamak.geobaza.di

import android.content.Context
import dagger.Module
import dagger.Provides

@Module
abstract class ContextModule {  // to allow abstract method make module abstract

  //@Binds works on an abstract method

    @Singleton
    @Binds   // @Binds, binds the Application instance to Context
    abstract fun context(appInstance:Application): Context //just return the super-type you need 

}

Step 2 : Remove redundant code from AppComponent

package com.mamak.geobaza.di

import android.app.Application
import dagger.BindsInstance
import dagger.Component
import dagger.android.support.AndroidSupportInjectionModule
import javax.inject.Singleton

@Singleton
@Component(modules = [
    ApiModule::class,
    ViewModelModule::class,
    AndroidSupportInjectionModule::class,
    InterfaceModule::class,
    ContextModule::class
])
interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        @BindsInstance
        fun apiModule(apiModule: ApiModule): Builder

        @BindsInstance
        fun interfaceModule(interfaceModule: InterfaceModule): Builder

       // @BindsInstance   //this two  commented lines can be removed 
       // fun contextModule(contextModule: ContextModule): Builder

       // why? because dagger already knows how to provide Context Module 

        fun build(): AppComponent
    }

    fun inject(appController: AppController)
}

Step 3: Modify the Component Builder to take advantage of @Component.Builder and @Binds

package com.mamak.geobaza.di

import android.app.Application

import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasAndroidInjector
import javax.inject.Inject

class AppController : Application(), HasAndroidInjector {
    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Any>

    override fun androidInjector(): AndroidInjector<Any> {
        return dispatchingAndroidInjector
    }

    override fun onCreate() {
        super.onCreate()
        DaggerAppComponent.Builder()
            .application(this)
            .apiModule(ApiModule())
            .interfaceModule(InterfaceModule())
           // .contextModule(ContextModule(this)) //this line can be removed 
            .build()
            .inject(this)
    }
}

Upvotes: 3

Related Questions