Image for post
Image for post

ViewModelComponent is a component in the Hilt’s component hierarchy that follows the lifecycle of a ViewModel and allows scoping types to it.

Before the ViewModelComponent was added to Hilt, ViewModel classes were created and injected by the ActivityRetainedComponent. Therefore, dependencies of ViewModels could only use unscoped types or types scoped to either the SingletonComponent or ActivityRetainedComponent whose instance is shared by all ViewModels.

The above might not seem like a problem if each screen of your app is an Activity, as scoping a type to ActivityRetainedComponent would mean each ViewModel class would receive a different instance of that type. …


Image for post
Image for post

If you’re a library author, you might want to make your Java-based or callback-based libraries easier to consume from Kotlin using coroutines and Flow. Alternatively, if you’re an API consumer, you may be willing to adapt a 3rd party API surface to coroutines to make them more Kotlin friendly.

This article covers how to simplify APIs using coroutines and Flow as well as how to build your own adapter using suspendCancellableCoroutine and callbackFlow APIs. For the most curious ones, those APIs will be dissected and you’ll see how they work under the hood.

If you prefer to watch a video about this topic. …


Image for post
Image for post

Hilt got released in June 2020 as a way to standardize dependency injection (DI) in Android. For new projects, Hilt provides compile time correctness, runtime performance and scalability (read more about that here)! However, what are the benefits for an application that already uses Dagger? Should you be migrating your current app to Hilt? The following are some reasons whether your team should invest migrating from Dagger to Hilt.

✅ AndroidX extensions

If you already have Dagger working with ViewModels or WorkManager, you saw that wiring up your ViewModelFactory and WorkerFactory requires quite a lot of boilerplate code and Dagger knowledge. The most common implementation uses multibindings which is one of the most complex features in Dagger that developers often struggle to understand. Hilt makes working with AndroidX a lot easier by removing that boilerplate code. What’s even better is that you don’t even need to inject the Factory in the Android framework class, you call it as if Hilt wasn’t there. With @ViewModelInject, Hilt generates the right ViewModelProvider.Factory


Image for post
Image for post
Illustration by Virginia Poltrack

Scoping an object A to another object B means that throughout the lifecycle of B, it’ll always have the same instance of A. When it comes to dependency injection (DI), an object A scoped to a container means that the container will always provide the same instance of A until the container is destroyed.

In Hilt, you can scope types to containers or components using annotations. For example, let’s say your app has a UserManager type that handles logins and logouts. You could scope this type to the ApplicationComponent (which is a container managed by the application’s lifecycle) using the @Singleton annotation. The scoped types in the application component flow down the component hierarchy: in the example, the same instance of UserManager will be provided to the rest of the Hilt components in the hierarchy. …


Image for post
Image for post
Illustration by Virginia Poltrack

Hilt defines a standard way to do dependency injection (DI) in your application by providing containers for every Android class in your project and managing their lifecycles automatically for you. Hilt is built on top of the popular DI library Dagger so benefits from the compile time correctness, runtime performance, scalability, and Android Studio support that Dagger provides.

This cheat sheet allows you to quickly see what the different Hilt and Dagger annotations do and how to use them. For more information about DI and Hilt, check out these guides or read the documentation. If you prefer a step-by-step learning approach, check out the Using Hilt in an Android app codelab.

Download cheat sheet in PDF


Image for post
Image for post
Illustration by Virginia Poltrack

Inserting a component in the middle of the Hilt component hierarchy is not possible. However, you can create a custom component with a Hilt component as a parent and access its scoped types using entry points. In the article, we’ll see what all this implies and how to add a custom component in the Dagger 2 codelab code.

The Dagger 2 codelab goes through adding Dagger to a multiple-Activity application. As the user can log in and out of the app, user information is handled in a Dagger component with a lifespan longer than activity components.


Image for post
Image for post
Illustration by Virginia Poltrack

Jetpack’s recommended library for DI

Dependency injection (DI) is a technique widely used in programming and well suited to Android development, where dependencies are provided to a class instead of creating them itself. By following DI principles, you lay the groundwork for good app architecture, greater code reusability, and ease of testing. Have you ever tried manual dependency injection in your app? Even with many of the existing dependency injection libraries today, it requires a lot of boilerplate code as your project becomes larger, since you have to construct every class and its dependencies by hand, and create containers to reuse and manage dependencies.

By following DI principles, you lay the groundwork for good app architecture, greater code reusability, and ease of testing. …


Image for post
Image for post

Last update: July 23rd, 2020

Have you ever got lost in a project trying to figure out from where a Dagger or Hilt dependency is being provided? Does it come from an @Inject constructor? Or maybe from an @Binds or @Provides method? Does it have a qualifier? It’s not an easy task…

🤔 What if you could know that and more with just one click? 🎯 Ask no more!

The latest version of Android Studio 4.1 (currently available in Beta) comes with new gutter icons that allows you to easily navigate between Dagger-related code: dependency producers and consumers, components, subcomponents, and modules! …


Image for post
Image for post
Illustration by Virginia Poltrack

Cancellation and Exceptions in Coroutines (Part 4)

In part 2 of the Cancellation and Exceptions in Coroutines series, we learnt the importance of cancelling work when it’s no longer needed. On Android, you can use the CoroutineScopes provided by Jetpack: viewModelScope or lifecycleScope that cancel any running work when their scope completes — that is when the Activity/Fragment/Lifecycle completes. If you’re creating your own CoroutineScope, make sure you tie it to a Job and call cancel when needed.

However, there are cases when you want an operation to complete even if the user navigated away from a screen. As such, you don’t want the work to be cancelled (e.g. …


Have you ever wondered what Dagger does under the hood 🤔?

Every annotation has a point 🔍! Check out these cheat sheets to understand how Dagger interprets them and the code it generates for you. Explanations about the generated code and Dagger annotations can be found in the cheat sheets as comments.

For more information about dependency injection, why you might use Dagger in your Android app, and how to use it, check out the official Android documentation: https://d.android.com/dependency-injection

If you prefer a step-by-step learning process, check out the Using Dagger in your Android app codelab.

Also, if you’re interested in the future of Dagger in Android, watch this Android Developer Summit ’19 video:

Dagger basics

Download file

Image for post
Image for post
Dagger basic annotations cheat sheet

Dagger Subcomponents

Download file

About

Manuel Vivo

Android DevRel @ Google

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store