heihei blog

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

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

f:id:shaunkawano:20191026000524p:plain

※Notes記事では、英語のセッション動画やポッドキャストの内容を(雑に)英語でメモに書き残すことを行っています。本記事は、あくまで動画を見ながら、参考程度に読んでいただくことを想定しています。Notes記事には雑メモ程度のものだったり、書き起こしのようなものもあります。これから実際の動画を見る際には、本記事の内容が少しでもお役に立てば幸いです。(内容において不備、誤字脱字等ありましたら気軽にご連絡いただけると嬉しいです!m(__)m)

本記事は、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

Dagger

Recommendation

  • 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

dagger-android?

  • 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

Bindings

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

Injection

  • 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

Components

  • Predefined Components
    • SingletonComponent
    • ActivityComponent
    • ServiceComponent
    • FragmentComponent

Jetpack DI Initiative

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

Takeaways

  • 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を用意しておくとか

DIはいいぞ、Daggerもいいぞ、どんどん良くしていくからぜひ使ってくれよな!感が伝わりました

以上です!