aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Mobile
  • Programming

Migrating From Dagger To Hilt — Is It Worth It?

  • aster.cloud
  • November 30, 2020
  • 3 minute read

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.

 


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

✅ 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 for you that @AndroidEntryPoint activities and fragments can use directly.

class PlayViewModel @ViewModelInject constructor(
  val db: MusicDatabase,
) : ViewModel() { ... }@AndroidEntryPoint
class PlayActivity : AppCompatActivity() {  val viewModel: PlayViewModel by viewModels()  override fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(bundle)
    viewModel.play()
  }
}

 

✅ Testing APIs

DI is supposed to make testing easier but ironically, having Dagger working in tests requires a lot of work. The fact that you have to maintain both the prod and test Dagger graph at the same time makes it notably worse than Hilt’s approach.

Hilt tests can explicitly modify the DI graph using the <a class="cm ie" href="https://developer.android.com/training/dependency-injection/hilt-testing#replace-binding" target="_blank" rel="noopener nofollow noreferrer">@UninstallModules</a> functionality. Apart from that, you get other perks like <a class="cm ie" href="https://developer.android.com/training/dependency-injection/hilt-testing#binding-new" target="_blank" rel="noopener nofollow noreferrer">@BindValue</a> that allows you to easily bind fields of your tests into the DI graph.

@UninstallModules(AnalyticsModule::class)
@HiltAndroidTest
class ExampleTest {  @get:Rule
  var hiltRule = HiltAndroidRule(this)  @BindValue @JvmField
  val analyticsRepository = FakeAnalyticsRepository()  @Test 
  fun myTest() { ... }
}

 

Read More  How To Monitor Kubernetes K3s Using Telegraf And InfluxDB Cloud

✅ Consistency

There are multiple ways to have the same functionality working in Dagger. The historical lack of guidance for Android apps (that we tackled last year) has caused multiple debates in the community and ultimately created inconsistencies in the way developers use and set up Dagger in their Android apps.

You might argue that your current Dagger setup is really good and you perfectly know how everything works and how everything is getting injected. Therefore, migrating to Hilt is not worth it! That might be true in your case, but is it the same for the rest of the team (and potentially future colleagues)? Will you know how everything works when switching to a new project? Understanding the setup and usage of Dagger in an app can be challenging and time consuming.

That time can be dramatically reduced by using Hilt into your app as the same setup is used by all Hilt applications. A new developer joining your team won’t be surprised about your Hilt setup because it’ll be pretty much the same as what they’re used to.

 

✅ Custom Components

Apart from the defined standard components, Hilt also gives you a way to create custom components and add them to the hierarchy which you can read more about here.

Even though custom components reduce consistency, you still get a lot of benefits! The module auto-discoverability (i.e. the <a class="cm ie" href="https://developer.android.com/training/dependency-injection/hilt-android#hilt-modules" target="_blank" rel="noopener nofollow noreferrer">@InstallIn</a> annotation functionality) feature as well as the test replacement features also work with custom components.

Read More  Demystifying Jetpack Glance For App Widgets

However, the difference between custom components and the Hilt built-in components is that you lose the ability to automatically inject those components into Android framework classes (i.e. what @AndroidEntryPoint does).

 

✅ Dagger and Hilt interop

Hilt and Dagger can co-exist together! You can benefit from Hilt in certain parts of your app while keeping the other most niche parts using Dagger if you allow Hilt to take over your SingletonComponent. This also means that the migration to Hilt can be done gradually.

 

❌ Component dependencies

Hilt being opinionated means it makes decisions for you. Hilt uses subcomponents for the component relationships, ready why here. If you’re a strong believer that your app is better off using component dependencies, Hilt is not the right tool for your app.

Migrating from Dagger to Hilt is worth it in most projects. The benefits Hilt brings to your application outnumbers the efforts of having to update. But you are not on your own! We provided lots of resources to help you out in this journey:

  • Comprehensive migration documentation
  • Migrating from Dagger to Hilt codelab
  • Migrating the Google I/O app to Hilt blog post and commit
  • Hilt and Assisted Injection working together gist

 

Source: Medium by Manuel Vivo


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • Android
  • AndroidX
  • Dagger
  • Hilt
  • Medium
You May Also Like
View Post
  • Architecture
  • Data
  • Engineering
  • People
  • Programming
  • Software Engineering
  • Technology
  • Work & Jobs

Predictions: Top 25 Careers Likely In High Demand In The Future

  • June 6, 2023
View Post
  • Programming
  • Software Engineering
  • Technology

Build a Python App to Alert You When Asteroids Are Close to Earth

  • May 22, 2023
View Post
  • Programming

Illuminating Interactions: Visual State In Jetpack Compose

  • May 20, 2023
View Post
  • Computing
  • Data
  • Programming
  • Software
  • Software Engineering

The Top 10 Data Interchange Or Data Exchange Format Used Today

  • May 11, 2023
View Post
  • Gears
  • Mobile
  • Technology

Apple Watch Pride Edition Celebrates The LGBTQ+ Community

  • May 10, 2023
View Post
  • Architecture
  • Programming
  • Public Cloud

From Receipts To Riches: Save Money W/ Google Cloud & Supermarket Bills – Part 1

  • May 8, 2023
View Post
  • Programming
  • Public Cloud

3 New Ways To Authorize Users To Your Private Workloads On Cloud Run

  • May 4, 2023
View Post
  • Programming
  • Public Cloud

Buffer HTTP Requests With Cloud Tasks

  • May 4, 2023

Stay Connected!
LATEST
  • 1
    Just make it scale: An Aurora DSQL story
    • May 29, 2025
  • 2
    Reliance on US tech providers is making IT leaders skittish
    • May 28, 2025
  • Examine the 4 types of edge computing, with examples
    • May 28, 2025
  • AI and private cloud: 2 lessons from Dell Tech World 2025
    • May 28, 2025
  • 5
    TD Synnex named as UK distributor for Cohesity
    • May 28, 2025
  • Weigh these 6 enterprise advantages of storage as a service
    • May 28, 2025
  • 7
    Broadcom’s ‘harsh’ VMware contracts are costing customers up to 1,500% more
    • May 28, 2025
  • 8
    Pulsant targets partner diversity with new IaaS solution
    • May 23, 2025
  • 9
    Growing AI workloads are causing hybrid cloud headaches
    • May 23, 2025
  • Gemma 3n 10
    Announcing Gemma 3n preview: powerful, efficient, mobile-first AI
    • May 22, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • Understand how Windows Server 2025 PAYG licensing works
    • May 20, 2025
  • By the numbers: How upskilling fills the IT skills gap
    • May 21, 2025
  • 3
    Cloud adoption isn’t all it’s cut out to be as enterprises report growing dissatisfaction
    • May 15, 2025
  • 4
    Hybrid cloud is complicated – Red Hat’s new AI assistant wants to solve that
    • May 20, 2025
  • 5
    Google is getting serious on cloud sovereignty
    • May 22, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.