DI for Android using Hilt

The release of Hilt is one of the best things to happen to Android development.

--

I have been developing Android applications since 2012, and at that time the main dependency injection library was RoboGuice. Dagger 1 went 1.0 in May of 2013. Dagger 2’s initial release was in May of 2015. I have had the privilege to use all three libraries and see the change to dependency injection for Android over the years.

RoboGuice was an Android port of Google Guice. It supported more features than just dependency injection, such as layout injection. RoboGuice depended heavily on reflection and one would notice that impact on startup of the app, as well as only being able to find problems during runtime.

Dagger 1 added an annotation processor. This did two things:

  1. It provided build time verification
  2. It began the migration away from reflection
    Dagger 1 however didn’t completely get rid of reflection¹ so some errors were only found at runtime, however due to how the graph was built those errors tended to show when the graph was created.

Dagger 2 (Dagger for the rest of the article) completed the migration from reflection to only use generated code. This allowed developers to remove Dagger from proguard files since all the code is static…

--

--

Jordan Hansen
The Startup

I have been developing Android Applications professionally since 2012.