Skip to main content

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

note

This article has been translated by Gemini.

In this Notes article, I roughly jotted down notes in English from the content of the English session video/podcast. This article is intended to be read as a reference while watching the video. I hope this article helps you even a little when you watch the actual video. (Please feel free to contact me if there are any errors or typos! m(__)m)

This article is based on 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!

Impressions / Summary / Key Takeaways
#

  • They created a common guide.
    • It covers everything from basics to complex topics, so please check it out.
  • They want to improve compatibility with Kotlin further.
    • Plans to include releases related to Companion Objects in future versions?
  • Development of dagger-android has stopped for now.
    • Because they think it can be improved further.
  • Aiming to lower setup and learning costs.
    • Mechanisms like adding @EntryPoint to Framework EntryPoints like Activities to create a graph from that class, automatically handling the Component setup in onCreate.
    • Mechanisms to make DI easier for other lifecycle-related AAC classes.
    • Providing common predefined Components without needing to define them yourself.

I got the message: DI is great, Dagger is great, we’ll keep making it better so please use it!

That’s all!