Why would I use Android Hilt (Dagger2) when Koin is available
Asked Answered
X

3

33

I like to keep the number of third party libraries used in my Android Apps to an absolute minimum.

I had started using Dagger2, then switched to Koin.

Koin is such a great improvement on Dagger2.

Koin has builtin ViewModel support and doesnt need anything "extra" for Workers.

Koin allows you to inject anything anywhere with minimal effort, its superb.

On the Android Hilt announcement I completed a spike to evaluate it, as it would reduce my dependencies on 3rd party libraries.

On completion of my spike efforts I do not see why anyone would use Hilt.

For example:

For Koin to inject into a Worker I have the worker implement KoinComponent, for Hilt to inject into a worker I need to disable the default WorkerManager initialisation, and employ two annotations @WorkerInject & @Assisted.

Am I missing something?

Xeniaxeno answered 13/11, 2020 at 16:18 Comment(0)
T
29

I think you have partially answered your question by saying: On the Android Hilt announcement I completed a spike to evaluate it, as it would reduce my dependencies on 3rd party libraries.

But to help you more in your decision, will try to list out the few pointers for both Koin and Hilt below.

Koin:

  1. All written in Kotlin, super-easy to learn, developer-friendly, and very similar to manual dependency injection.
  2. It is a lightweight DSL dependency injection framework written with a service locator pattern.
  3. No annotations used, which means no code generation, this helps in build speed.
  4. Uses scope to manage android component lifecycles.
  5. Resolves dependencies at run-time, as they are loaded lazily, which can lead to RuntimeException later in the application.

Hilt:

  1. Built on top of the dagger, a standard framework for dependency injection, and officially recommended for android development.
  2. Uses annotation to generate code. Official annotations cheat sheet to help you.
  3. It knows about the android component lifecycle.
  4. If you have basic knowledge of Dagger then learning Hilt will be easy.
  5. Hilt resolves dependencies at compile-time, which means it helps with build-time correctness.

Choosing between Koin and Hilt depends on multiple factors for the given scenario, both have their acknowledgeable amount of advantages and disadvantages.

For example: As you said you have already written your project in Koin then if you want to use Hilt, then you might have to re-write all your dependencies graph. If you are using Dagger, then migrating to Hilt will be less difficult.

Tortilla answered 17/5, 2021 at 14:4 Comment(6)
Just my 2 cents: officially recommended means nothing. It doesn't add any value to something.Packton
@Packton I get what you are saying but disagree it doesn't, potentially at least, add value. Being "officially recommended" may mean that if you run into problems there is a bigger community to lean on as a resource, same goes for its development and bug fixes. Its possibly just an advantage of sorts. Also one missing advantage for Koin is compatible Kotlin Multiplatform Mobile modules, Dagger2 is restricted to the Java/JVM.Spiral
@Spiral Kotlin community is big enough. Especially if you work with Android and Kotlin Multiplatform, you should know it. As I understood, Hilt can't work with feature modules (Google recommend Dagger for it) while Koin can, and you can use it not only with Android but also with backend for example.Motorize
@Tortilla Can you elaborate more on #4 in Hilt's section? What does that mean exactly?Ennis
@ArthurKasparian as I got it, Hilt was developed on the basis of Dagger and, as result, these two frameworks are very similar to each other, but Hilt is more modern and simplier. So, if you are familiar with Dagger, it will be easier for you to deal with Hilt.Egad
one day I have to convert my app to module but hilt does not support to use in android module, this is a big disadvantagePhenocryst
P
16

Stability and available Documentation would be two more reasons to choose Hilt over Koin.

Since I first worked with Koin I became a big fan of it. Especially compared to Dagger. However in my recent (big) project, we decided in favor of Koin (vs. Hilt/Dagger). But we also ran into an (yet unresolved) bug using koins viewModel features in conjuction with latest navigation components from google.

Hilt is officially supported by Google, which means it should have less integration issues and higher stability compared to 'outsider' frameworks.

Also a lot of standard documentation and examples you will find are based on hilt, so if you are using koin you might need to spend some time adapting them for your setting. Fortunately, Koin is pretty straightforward.

As others already have written: Another important difference you should understand: a lot of daggers magic happens during compile time vs Koin working on runtime only. Both strategies come with their pros and cons.

Compile-time has better type safety. And the performance impact is moved to the build time instead of run time[1]. Runtime can lead to late discovery of errors (when Casting or Null Pointer Exception) but makes the framework also more flexibel (but slighty more dangerous).

(edit) A note on Testing: I just discovered a scenario in testing where there is a big difference in convenience: When you want to access your ViewModel in an UI Test (Instrumented Android Compose Test) there is direct Hilt support afaik. It is still possible to do it using koin, but it took me a while to figure out how for my scenario.

[1] But I think performance impact of DI is minimal in most settings and mostly overrated by developers trapped in pre-mature optimization.

Posner answered 8/4, 2022 at 9:2 Comment(0)
W
0

Addition to what other answers' pointed out, Koin also allows dependency injection for unit test not just instrumented test (androidTest) in which as far as I know is not possible with Hilt.

Wheedle answered 21/7, 2024 at 23:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.