If you've been using Koin's DSL for dependency injection in your Kotlin Multiplatform projects, you...
Why Migrate from Dagger2/Hilt to Koin?
We’ve just wrapped up Koin’s very first webinar, and it was a great session, thanks to all those who attended! Arnaud Guiliani, the project lead for Koin, took us through the ins and outs of migrating from Dagger2/Hilt to Koin, especially for Kotlin Multiplatform (KMP) projects.
In case you missed it, or just want to revisit some of the key points, here’s a quick recap of what was covered, including the practical steps for making the switch and why it might be the right move for your team.
Why Consider Migrating from Dagger2/Hilt to Koin?
Let’s start with the big question: Why migrate to Koin? If you’re working with Dagger2 or Hilt, you’ve probably noticed that they don’t play nicely with Kotlin Multiplatform (KMP). They aren’t compatible, which can be a complication for teams wanting to share code across platforms like Android and iOS. Beyond that, Dagger2/Hilt can get pretty complicated to set up, and long compilation times don’t help either.
That’s where Koin comes in. It’s designed specifically for Kotlin, including full support for KMP. Koin simplifies Dependency Injection (DI) by cutting down on complexity, so you can focus on writing code rather than managing endless configurations. It’s lightweight and easy to use, and you won’t have to wrestle with non-Kotlin frameworks to get DI working smoothly across platforms.
Migration Doesn’t Need to Happen Overnight
In fact, starting small is the way to go. Instead of diving headfirst into a full migration, take an incremental approach. Begin with isolated modules or features to minimize risk and impact. This way, you can gradually introduce Koin into your project without disrupting everything.
A key takeaway was to look closely at your app’s architecture—identify components that can easily be migrated and target those first. Think of it as laying the groundwork for a larger migration, while keeping the project stable and moving forward.
Koin API Overview
The good news is Koin’s API is straightforward, which makes it easy to transition from Dagger2/Hilt. The main components you’ll use are factory
, single
, and viewModel
, which map directly to Dagger/Hilt concepts:
@Singleton
in Dagger/Hilt becomessingle
in Koin.@HiltViewModel
turns intoviewModel
in Koin.@Provides
or@Binds
gets replaced with Koin’sfactory
.
This mapping means you won’t need to do any heavy lifting to convert your existing DI setup to Koin. It’s just about swapping out the annotations and continuing with business as usual.
Migration Strategies - Before Jumping In
Get a clear view of your app’s components, lifecycle, and how everything fits together. Migrating doesn’t have to be all or nothing - you can keep using Dagger2 or Hilt in parts of your app while gradually introducing Koin in other areas.
For example, you can start by moving a single ViewModel or repository to Koin and still use Dagger elsewhere. Over time, this lets you transition more components without rushing or risking app stability. It’s all about balancing progress with caution, ensuring things stay smooth as you go.
Koin for Kotlin Multiplatform
A big plus with Koin is its native support for Kotlin Multiplatform (KMP). With Dagger2/Hilt, you’re stuck building DI for each platform separately. But with Koin, you can share DI logic across Android, iOS, and other platforms, keeping your code DRY and consistent.
This makes Koin a perfect fit for teams working with KMP, letting you share code without reworking your DI setup for each platform.
Wrapping Up
Migrating from Dagger2/Hilt to Koin is a smart move for teams working with Kotlin or Kotlin Multiplatform. Koin’s lightweight, Kotlin-native API cuts down on complexity and makes the whole DI process easier to manage. The best part? You don’t have to do it all at once—an incremental approach lets you switch over gradually while keeping your app stable.
Wanting to dive deeper? We’ve got you covered with some resources below to help you get started.
Resources
🔸 Cheat Sheet : Migrate from Hilt to Koin
🔸 Debug & Track your App Migration