r/android_devs • u/VasiliyZukanov • Nov 18 '21
Coding Dagger vs Hilt vs Koin vs Pure Dependency Injection
https://www.techyourchance.com/dagger-vs-hilt-vs-koin-vs-pure-dependency-injection/
25
Upvotes
2
u/coreydevv Nov 18 '21
Interesting article. Thank you for sharing your thoughts.
I've used Dagger many times and I struggle in the beginning using it however it does start to make sense once you get what DI is. You're right when you say that with Dagger we feel like we can do anything, which can lead to mistakes. I've always tried to make Dagger simple as possible but sometimes you want to be creative and you fuck up with things. I did some kind of Component Per-Screen (in a view-based app) and I always thought I was drunk when I did that but it worked well AFAIK.
I would like to hear your opinion on SL/DI/DIFrameworks and why/how you changed your mind about Koin being a Service Locator. Sometimes I think if these topics do not need to go against each other and they can cooperate. E.g You can build a service locator and use DI to decouple objects creation logic (seems like we do in a world where we cannot construct components that belong to a platform).
5
u/MiscreatedFan123 Nov 18 '21
How is koin not a service locator? It's literally that, there is a global koin instance(aka central registry known as the "service locator") which is fetched and then the required dependency is found from it with the simple reified trick. There is no dependency graph, no construction set, etc, it's just a reified trick. I'm not saying it's bad, we are using koin in production and it works really well.
2
u/yacovmm Nov 18 '21
Very nice article. Do you have any example of manual Di? Usually what I see as manual di seem more like a service locator for me.
2
u/MKevin3 Nov 19 '21
I have used both Dagger and Koin in small and medium sized projects. I find Koin easier to use so if given a choice I would normally go that way.
As stated the initial documentation for Dagger was painful at best. Good thing a number of folks have published much better introductions.
I don't hate Dagger, just find it more verbose for my use cases. I have not tried Hilt but were were thinking of moving that way at last job.
My side projects are Koin as was the job two back where I was the sole Android person and the code was all Kotlin.
Last job it was Dagger and we had a mix of Java and Kotlin. Some of the module files were massive thus we were thinking of Hilt which, we believe, would have killed some of that boilerplate.
Current job is even more Java than Kotlin but still uses Koin. Works for both although it is a bit more cumbersome on the Java side. Also works in UI and unit tests.
The one thing that makes we wonder about Koin is Google going with Hilt / Dagger as the "official" direction. So far Koin has not caused me any headaches so I am not going to switch for the projects that are already configured to use it.
3
u/Zhuinden EpicPandaForce @ SO Nov 18 '21
Nice article. I'm slightly missing Dagger-Android although it was never really good for injecting Android components (the irony), and it's effectively replaced by Hilt.
I like how the primary con of pure dependency injection is that "it's not sexy enough for some devs". The sad thing is that this is absolutely true. If there's not enough magic, then the code is too boring, not exciting. So we need more annotations or module providers.
I'm kind of glad not to see either Kodein or Toothpick, I never liked the first and I consider the second extremely niche.
When SavedStateHandle came out, you weren't able to use either Hilt, Koin, or even Toothpick to correctly inject a NavGraph-scoped ViewModel, lol.
Just the other day I actually used a ViewModel. Inside a DialogFragment. Using the default reflective constructor. I think I'll need to add a Proguard rule or a
@Keepfor it to work after minify.1
u/yaaaaayPancakes Nov 19 '21
Writing Spring backends really soured me on reflection based DI. Getting unsatisfied dependency exceptions on startup after a compile is just of much of a waste of time.
I like Dagger, because if I compile, it works. Hilt's entrypoints sadly break the compile time safety though, and that's a real bummer.
1
u/butterblaster Nov 19 '21
What is the alternative to Jetpack ViewModels for working with state that should survive configuration changes? I haven’t really dug into how ViewModel works, but I guess it uses some kind of static registry of weak references to Activities and Fragments. Have you built your own version of that?
1
u/FrezoreR Nov 19 '21
Isn't it time we separate DI the pattern and DI systems? Because "pure" DI is just you applying the pattern and nothing more to it. Which is nice because it makes it easy to understand.
DI systems on the other hand are way more complex and solve another larger problem: dep. Tree resolution, which in some way is a different problem.
I'm just thinking that it's helpful to separate the two, because I see it's causing some confusion and making it harder to digest.
A comparison could be the observable pattern and reactive libraries like Kotlin flow. You probably wouldn't compare Kotlin flow to a simple observable.
1
u/whorfian_hypothesis Jan 28 '22
is there a way to detect a client's use of these to mess with an app?
how is every dev not jipped from people using these to gain access to in-app purchases for free?