title: Dagger 2 injecting Android Application Context
body: <p>I am using Dagger 2 and have it working however I now need access to the <strong>Android Application Context</strong>.</p>
  <p>Its not clear to me how to inject and get access to the context. I have tried to do this as follows:</p>
  <pre><code>@Module
  public class MainActivityModule {    
      private final Context context;
      
      MainActivityModule(Context context) {
          this.context = context;
      }
  
      @Provides @Singleton
      Context provideContext() {
          return context;
      }
  }
  </code></pre>
  <p>However this results in the following exception:</p>
  <blockquote>
  <p>java.lang.RuntimeException: Unable to create application : java.lang.IllegalStateException: mainActivityModule must be set</p>
  </blockquote>
  <p>If I inspect the Dagger generated code this exception is raised here:</p>
  <pre><code>public Graph build() {  
      if (mainActivityModule == null) {
          throw new IllegalStateException(&quot;mainActivityModule must be set&quot;);
      }
      return new DaggerGraph(this);
  }
  </code></pre>
  <p>I am not sure if this is the correct way to get Context injected - any help will be greatly appreciated.</p>