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?
Way easier to use than Dagger and Hilt First of all, Koin is definitely much simpler to use and to learn than Dagger or Hilt. It can be a good choice for novice programmers that want to learn Dependency Injection.
Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.
Dagger and Hilt code can coexist in the same codebase. However, in most cases, it's best to use Hilt to manage all your use of Dagger on Android.
Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.
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).
[1] But I think performance impact of DI is minimal in most settings and mostly overrated by developers trapped in pre-mature optimization.
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:
Hilt:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With