Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

What’s new in Hilt and Dagger 2.31

 

 
Jaewoong Eum
A software engineer: Android, IoT. Interested in Psychology, Magic Tricks, and writing poems. https://github.com/skydoves
Published: January 26, 2021
Tweet
Share
 

Unsplash@dermanuskript

 

ilt is a dependency injection tool that has been introduced by Google in the last year. Hilt makes our projects cleaner and reduces setup time for constructing our DI environment than Dagger-Android. It has been steadily getting feedback and developing for several months. Now Hilt is more stable than the earliest was. In this post, we will take a look at what is the main difference between the earliest version of Hilt and the newest one.

 

Previous post

  • Exploring Hilt and what’s main differences from Dagger-Android

 

HiltAndroidApp

There is no difference in using HiltAndroidApp annotation for building the base DI environment. The role of the HiltAndroidApp is kicking off the code generation of the Hilt components of our project in the compile-time and generates the base classes that need to construct our DI structure. So we don’t need to create a bunch of boilerplates like ActivityModule, ViewModelModule, ViewModelFactory, etc.

 

PokedexApp

 

Components

The most noticeable difference between Dagger-Android and Hilt is that we don’t need to create the base components. Instead, Hilt provides predefined components that are generated for our projects. Each component has a corresponding scope that is integrated into the lifecycles of the Android application, and their providing rules are depending on the lifecycles. In Hilt 2.31, the previous ApplicationComponent has been removed. We should migrate to SingletonComponent instead. This is intended to be a pure rename/functional no-op. Also the ViewModelComponent and ViewModelScoped are newly introduced.

 

Image for post

components

 

ViewModelComponent

The ViewModelComponent is newly introduced in Hilt 2.31. Now ViewModels are injected from a ViewModelComponent with accompanying the ViewModelScope. It means all Hilt ViewModels are provided y the ViewModelComponents which follows the same lifecycle as a Jetpack’s ViewModel . (Survives screen rotations)

 

A @ViewModelScoped type will make it so that a single instance of the scoped type is provided across all dependencies injected into the Hilt View Model. Other instances of a ViewModel that requests the scoped instance will receive a different instance. Scoping to the ViewModelComponent allows for flexible and granular scope since View Models surive configuration changes and their lifecycle is controlled by the activity or fragment.

 

So If we want to provide some business instances related to network, database which has their own loading status, we have to provide it via the ViewModelComponent.

 

RepositoryModule

 

HiltViewModel

Previously, we used ViewModelInject with Assisted annotation for getting a SavedStateHandle in ViewModels. But in Hilt 2.31, HiltViewModel is newly introduced and we use it and inject instead of the ViewModelInjectand Assisted.

 

MainViewModel

 

Assisted Inject

Dagger 2.31 now supports assisted injection.

 

Assisted injection is a dependency injection (DI) pattern that is used to construct an object where some parameters may be provided by the DI framework and others must be passed in at at creation time (a.k.a “assisted”) by the user.

 

We have been used Square’s AssistedInjection and Google’s AutoFactory before. But now we don’t need to add more third-party dependencies and we can reduce a few extra boilerplates to integrate with Dagger. We can implement it using the AssistedInject and Assisted annotations. Furthermore, we don’t need to install AssistedInject_AssistedInjectModule. Here is an example of injecting specific parameters into ViewModel manually using the assisted injection.

 

DetailViewModel

 

And now we can inject the Factory and ViewModels in DetailActivity .

 

DetailActivity

 

Hilt + Navigation + Compose + ViewModel

The other news is that the Google team is preparing to release the compose & navigation supports using Hilt. This is good news for the compose and navigation users.

 

 

HiltWorker

Hilt-worker extension also will be changed soon. If you are using the worker extension, hold on a bit before releasing the next version. It is scheduled to be released on January 27th or after. Workers can be injected using the HiltWorker and AssistedInject annotations.

 

 

Conclusion

The goal of Hilt is to make it easier to set up DI environments and enable developers to focus on their business codes and Dagger binding definitions. Also, Hilt provides specific predefined components and corresponding scopes for developers, so it prevents misusages and reduces much of the initial setup time. Simplicity that comes from Hilt using monolithic components and its results in reducing time for constructing DI environments is the most powerful side of Hilt. It’s still alpha, but it seems good to use in the real project. And I’m more looking forward to the next releases and future.

You can check out the full usage source code of Hilt in the below link.

 

Image for post

https://github.com/skydoves/pokedex

 

skydoves/Pokedex

Pokedex is a small demo application based on modern Android application tech-stacks and MVVM architecture.This project…

github.com

 

 

Tags: Android, Hilt, Dagger, Dagger Hilt, Dagger Android

 

View original article at: 


 

Originally published: January 16, 2021

Android News
Our Engineering Roadmap
Our Engineering Roadmap

By Mark Ng

We just completed our engineering road map for our Android apps at Australia Post. Each year we get together and try to decide on what we are going to do from an engineering perspective for the next 12 months. Each team gets to decide on what should be done now, what they want to complete by the end of the year and whats on the horizon for next year.

By ProAndroidDev -
Android News
Android Activity Lifecycle considered harmful
Android Activity Lifecycle considered harmful

By Eric Silverberg

The Android activity lifecycle is one of the first architectural concepts learned by a new Android developer. Ask any Android dev and they’ll likely all have seen this handy diagram from Google: 

By ProAndroidDev -
Android News
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App

By Alejandro Weichandt

It has been a year since we started working on the Android version of the mobile app at Sync. During that year, we faced more than once that moment when we had to choose which path to follow on an Architectural decision. This story is about Navigation.

By ProAndroidDev -
Android News
Custom KotlinX Serializers
Custom KotlinX Serializers

By Jobin Lawrance

Let’s say we have a third-party class that we are using as a type in one of our data class that we want to be serialized, then we have to write a custom serializable for @Serializable to work.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One