heihei blog

Blog icon by Unsplash https://unsplash.com/@virussinside

Notes - An Opinionated Guide to Dependency Injection on Android (Android Dev Summit '19)



本記事は、An Opinionated Guide to Dependency Injection on Android (Android Dev Summit '19)の記事です。

Dependency Injection

Car needs engine to work = Engine is a dependency of car = Car depends on engine

  • With dependency injection, those dependencies are provided to a class instead of the class creating them itself.
  • You should always apply dependency injection principle to your app
    • sets fundamentals of testable and scalable app
    • Reusability of code
    • Good balance of loosely-coupled dependencies
    • Ease of refactoring



  • Does not use reflection
  • Recommended tool
  • Dagger is used on Gmail, Google Photos, Youtube

Steep learning curve..

  • Started providing a common guidance, which is a set of documentation from the basics to the complex topics
  • Codelab for Dagger


  • Google is stopping its development
  • Adding no more feature
  • Trying to reduce the amount of code you have to write

Better Dagger code with Kotlin

Dagger + Kotlin = ♡

  • No more JvmStatic
  • Qualifier Annotations
  • Kotlin wildcards

Future versions

  • Some more improvements are coming
    • e.g. for companion object

Simple DI Approach in Android

  • still under construction
  • how it will look like
    • Focus on binding declarations


The time you spend working on DI should really go to what you keep working on to scale your app.


  • Not just using @Inject, you should do some more extra work, for example to set up app component
  • Add @EntryPoint for Android component class
    • Tells that you want this Android component to be a entry point into the graph
  • Support and provide easy hookups for lifecycle related components like ViewModel


  • Predefined Components
    • SingletonComponent
    • ActivityComponent
    • ServiceComponent
    • FragmentComponent

Jetpack DI Initiative

  • Integrated with Dagger
  • Jetpack Extensions
    • Fragments
    • ViewModel
    • WorkManager


  • Use DI
  • Use Dagger
  • Check out the guidance
  • We're improving DI in Android, so stay tuned!


  • 共通ガイドを作った
    • 基礎的なところから複雑なトピックまで扱ってるからみてほしい
  • Kotlinとの相性をもっと良くしていきたい
    • 今後のリリースでCompanion Objectに関連したリリースなども行う予定?
  • dagger-androidの開発今はやめてる
    • もっとよくできると思っているから
  • セットアップコスト、学習コストを下げていきたい
    • ActivityなどのAndroidのEntryPointとなるFrameworkに対して@EntryPointを付与することで、そのクラスを起因にグラフを作る、つまり今までのonCreate内でのComponentクラスのセットアップまわりの処理などを自動でやってくれるような仕組みとか
    • 他のLifecycleに関するAACのクラスなどをより簡単にDIできるようにする仕組みとか
    • 独自に定義しなくてもある程度一般的なComponentを用意しておくとか