Dagger: injection into android library modules made easy

Gaurav Vashisth
1 min readSep 16, 2017

--

Garima’s recent post on Defeating the Dahaka made me realize that a lot of new things have been incorporated lately in Dagger. There are now ‘tons’ of new mysterious annotations to get baffled with, but if you understand a few of them, they are gonna make life easy for all you droids.

One of the problems I was facing was to have dagger injections in a library module. The application component had to had abstract`inject(LibraryActivity activity)` methods and also the activities and fragments had to somehow get hold of application component to inject. This was causing a two way coupling between the two. With the release of Android support in Dagger 2.10 onwards, this problem has been solved.

Here is a simple example of how it can work.

Some resources to learn more about new features of Dagger 2

  1. Google sample todo-mvp-dagger
  2. Dagger Android Injector by Mert
  3. Defeat the Dahaka series by Garima

Never stop exploring

Stockholm city Library

--

--

Gaurav Vashisth

Mobile dev. ex Bontouch Stockholm, Lifesum Stockholm, Fueled New York.