title: Injecting application context in library module with Dagger 2
body: <p>I'm building an app with some features: a ContentProvider a SyncAdapter, a Job service and related persistence logic. On top on these there are the Activities with the UI. I'm trying to put all said features in a separate library module, because in theory their logic is stand-alone and would be reusable by any application.</p>
  
  <p>Now comes Dagger2. The first node (main Component) of my library's dependency graph does need to provide Context, and this Context has to be injected from the Application, because the library scope has the same lifecycle of the application. To be self contained, obviously, my library should not directly use my Application class.</p>
  
  <p>These are the possibilities I thought of:</p>
  
  <ul>
  <li>Build the library's main Component in my Application and store it in a global static class/enum as suggested <a href="https://stackoverflow.com/questions/31417795/dagger-2-on-android-singleton-annotated-class-not-being-injected/31865368#31865368">here</a> but I'm concerned that using such a static reference could be an anti-pattern.</li>
  <li>Pack in the library an Application class which builds the library scoped Component, cast app context to this class in the library to use the component and then extend this Application class on the main app. This works, but if there's more than one library it's not viable anymore.</li>
  <li>Use the factory pattern: put provision methods in the library component that provide the factory which in turn is given the locally available context as a parameter. (As explained <a href="https://stackoverflow.com/questions/37516736/dagger-2-injecting-user-inputted-parameter-into-object">here</a>). This seems viable, although it adds extra complexity.</li>
  <li>Last but non the least, give up trying to modularize the components, since being dependent on the application context breaks the concept of modularity.</li>
  </ul>
  
  <p>What is the correct way to do this?</p>