Dagger2 for Modular Architecture

Content Table

Dagger2 Introduction

Dagger 2 is a compile-time evolution approach to dependency injection.

Taking the approach started in Dagger 1.x to its ultimate conclusion, Dagger 2.x eliminates all reflection, and improves code clarity by removing the traditional ObjectGraph/Injector in favor of user-specified @Component interfaces.

simple dagger2 ApplicationComponent
ApplicationComponent and feature SubComponents
Feature SubComponent in detail

Issue in a Modular Architecture

In a Modular Architecture or Instant app the BaseModule can’t access classes in Feature modules
Using plain component rather than SubComponent for feature modules
okHttp can’t be accessed by BrowserComponent
okHttp exposed explicitly so BrowserComponent can access it
ApplicationComponent split in multiple SubComponents

Example Repository



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store