56

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.

murt
  • 3,790
  • 4
  • 37
  • 48
user3521637
  • 1,622
  • 2
  • 18
  • 25
  • I am not sure if injecting Context of Application makes sense. You can extend Application class and create static instance of the Application. You can name it e.g. `BaseApplication`. After that, you can create `get()` method inside your extended Application class, which will return that instance and will be Application Context at the same time. Then, you can use the following construction to reach Application Context from any place in your project: `BaseApplication.get()`. You should use it carefully and only when it's necessary. – Piotr Wittchen Jun 07 '15 at 13:05

6 Answers6

34
@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();
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
16

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;
    }
}
keisar
  • 5,266
  • 5
  • 28
  • 28
8

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.

Adolfo Chavez
  • 91
  • 1
  • 4
2

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

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
user3521637
  • 1,622
  • 2
  • 18
  • 25
2
@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)
Ashok Kumar
  • 1,226
  • 1
  • 10
  • 14
1

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

}
hyena
  • 755
  • 1
  • 14
  • 24