Dagger 2 injecting Android Application Context

AndroidDependency InjectionAndroid ContextDagger 2

Android Problem Overview


I am using Dagger 2 and have it working however I now need access to the Android Application Context.

Its not clear to me how to inject and get access to the context. I have tried to do this as follows:

@Module
public class MainActivityModule {    
    private final Context context;
    
    MainActivityModule(Context context) {
        this.context = context;
    }

    @Provides @Singleton
    Context provideContext() {
        return context;
    }
}

However this results in the following exception:

> java.lang.RuntimeException: Unable to create application : java.lang.IllegalStateException: mainActivityModule must be set

If I inspect the Dagger generated code this exception is raised here:

public Graph build() {  
    if (mainActivityModule == null) {
        throw new IllegalStateException("mainActivityModule must be set");
    }
    return new DaggerGraph(this);
}

I am not sure if this is the correct way to get Context injected - any help will be greatly appreciated.

Android Solutions


Solution 1 - Android

@Module
public class MainActivityModule {    
    private final Context context;

    public MainActivityModule (Context context) {
        this.context = context;
    }

    @Provides //scope is not necessary for parameters stored within the module
    public Context context() {
        return context;
    }
}

@Component(modules={MainActivityModule.class})
@Singleton
public interface MainActivityComponent {
    Context context();

    void inject(MainActivity mainActivity);
}

And then

MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder()
    .mainActivityModule(new MainActivityModule(MainActivity.this))
    .build();

Solution 2 - Android

It took me a while to find a proper solution, so thought it might save some time for others, as far as I could gather this is the preferred solution with the current Dagger version (2.22.1).

In the following example I need the Application's Context to create a RoomDatabase (happens in StoreModule).

Please if you see any errors or mistakes let me know so I'll learn as well :)

Component:

// We only need to scope with @Singleton because in StoreModule we use @Singleton
// you should use the scope you actually need
// read more here https://google.github.io/dagger/api/latest/dagger/Component.html
@Singleton
@Component(modules = { AndroidInjectionModule.class, AppModule.class, StoreModule.class })
public interface AwareAppComponent extends AndroidInjector<App> {

    // This tells Dagger to create a factory which allows passing 
    // in the App (see usage in App implementation below)
    @Component.Factory
    interface Factory extends AndroidInjector.Factory<App> {
    }
}

AppModule:

@Module
public abstract class AppModule {
    // This tell Dagger to use App instance when required to inject Application
    // see more details here: https://google.github.io/dagger/api/2.22.1/dagger/Binds.html
    @Binds
    abstract Application application(App app);
}

StoreModule:

@Module
public class StoreModule {
    private static final String DB_NAME = "aware_db";
    
    // App will be injected here by Dagger
    // Dagger knows that App instance will fit here based on the @Binds in the AppModule    
    @Singleton
    @Provides
    public AppDatabase provideAppDatabase(Application awareApp) {
        return Room
                .databaseBuilder(awareApp.getApplicationContext(), AppDatabase.class, DB_NAME)
                .build();
    }
}

App:

public class App extends Application implements HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;

    @Override
    public void onCreate() {
        super.onCreate();

        // Using the generated factory we can pass the App to the create(...) method
        DaggerAwareAppComponent.factory().create(this).inject(this);
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return dispatchingAndroidInjector;
    }
}

Solution 3 - Android

I have read this article and it was very helpful.

https://medium.com/tompee/android-dependency-injection-using-dagger-2-530aa21961b4

Sample code.

Update: I removed from AppComponent.kt these lines because are not necessaries

fun context(): Context
fun applicationContext(): Application

AppComponent.kt

   @Singleton
    @Component(
        modules = [
            NetworkModule::class,
            AppModule::class
        ]
    )
    interface AppComponent {
        fun inject(viewModel: LoginUserViewModel)
    }

AppModule.kt

@Module
class AppModule(private val application: Application) {

    @Provides
    @Singleton
    fun providesApplication(): Application = application

    @Provides
    @Singleton
    fun providesApplicationContext(): Context = application

    @Singleton
    @Provides
    fun providesNetworkConnectivityHelper(): NetworkConnectivityHelper{
        return NetworkConnectivityHelper(application.applicationContext)
    }
}

NetworkConnectivityHelper.kt

And only added @Inject constructor to pass the Context

class NetworkConnectivityHelper @Inject constructor(context: Context) {

    private val connectivityManager =
        context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager

    @Suppress("DEPRECATION")
    fun isNetworkAvailable(): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val nc = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)

            nc != null
                    && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
        }

        val networkInfo = connectivityManager.activeNetworkInfo
        return networkInfo != null && networkInfo.isConnected
    }
}

App class.kt

class App : Application() {

    lateinit var appComponent: AppComponent

    override fun onCreate() {
        super.onCreate()
        this.appComponent = this.initDagger()
    }

    private fun initDagger() = DaggerAppComponent.builder()
        .appModule(AppModule(this))
        .build()
}

Finally in my Activity I injected my helper

 @Inject lateinit var networkConnectivity: NetworkConnectivityHelper

And YEI! it works for me.

Solution 4 - Android

Was not correctly building the Application component, needed to pass in the Application. This Dagger 2 example perfectly shows how to do this: https://github.com/google/dagger/tree/master/examples/android-simple/src/main/java/com/example/dagger/simple

Update:
Working link: https://github.com/yongjhih/dagger2-sample/tree/master/examples/android-simple/src/main/java/com/example/dagger/simple

Solution 5 - Android

probably we could inject the context as shown below:

the application component

@Component(
    modules = [
        (ApplicationModule::class),
        (AndroidSupportInjectionModule::class),
        (UiBindingModule::class)
    ]
)
interface ApplicationComponent : AndroidInjector<AndroidApplication> {

    override fun inject(application: AndroidApplication)

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: AndroidApplication): Builder

        @BindsInstance
        fun context(context: Context): Builder

        fun build(): ApplicationComponent
    }
}

the custom application extending dagger application

class AndroidApplication : DaggerApplication() {

    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerApplicationComponent.builder().application(this).context(this).build()
    }
}

Example ApplicationModule

@Module
abstract class ApplicationModule {

    /**
     * Binds a background thread executor, which executes threads from a thread pool
     * @param jobExecutor
     * @return
     */
    @Binds
    internal abstract fun provideThreadExecutor(jobExecutor: JobExecutor): ThreadExecutor

    /**
     * Binds main ui looper thread
     * @param uiThread
     * @return
     */
    @Binds
    internal abstract fun providePostExecutionThread(uiThread: UIThread): PostExecutionThread

}

Example UI BindingModule

@Module
abstract class UiBindingModule {

    @ContributesAndroidInjector(modules = [(MainActivityModule::class)])
    internal abstract fun mainActivity(): MainActivity

    @ContributesAndroidInjector(modules = [(MapFragmentModule::class)])
    internal abstract fun mapFragment(): MapFragment

}

Solution 6 - Android

@Singleton
@Component(modules = [YourModule::class, ThatOtherModule::class])
interface ApplicationComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance fun applicationContext(applicationContext: Context): Builder
        fun build(): ApplicationComponent
    }
}

class YourApplication : Application() {

    val component: ApplicationComponent by lazy {
        DaggerApplicationComponent.builder()
            .applicationContext(applicationContext)
            .build()
    }
}
  • Use the @BindInstance to declare a abstract function that provides the context dependency. i.e @BindsInstance fun applicationContext(applicationContext: Context): Builder
  • set the context using .applicationContext(applicationContext)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3521637View Question on Stackoverflow
Solution 1 - AndroidEpicPandaForceView Answer on Stackoverflow
Solution 2 - AndroidkeisarView Answer on Stackoverflow
Solution 3 - AndroidAdolfo ChavezView Answer on Stackoverflow
Solution 4 - Androiduser3521637View Answer on Stackoverflow
Solution 5 - AndroidhyenaView Answer on Stackoverflow
Solution 6 - AndroidAshok KumarView Answer on Stackoverflow