https://kotlinlang.org logo
Title
c

Cyberpunk Keanu

11/21/2019, 4:26 AM
Guys, which dependency injection framework is better? Dagger 2 or Koin? Now that our app is going to scale, I feel like we're going to have a need for dependency injection in it
d

darkmoon_uk

11/21/2019, 5:19 AM
Currently using Dagger in a large multi-module project. Have only made exploratory use of Koin.
Pro's of Dagger: • Compile-time checking of your DI provisions. • Advanced feature-set with regard to injection Scopes and Android specific features.
Con's of Dagger: • Adds significantly to build time. • Adds somewhat to Gradle setup/day-to-day project complexity. • Requires a degee of 'binary compatibility' when injecting dependencies that are being provided by other library modules - so if your organisation provides library components that your team don't directly control, this is a red flag.
On the last one, our team have been bitten where we can't upgrade Dagger past a certain version until other teams upgrade their Dagger, due to breaking changes.
Pro's of Koin: • Super simple to use • Nice Kotlinic syntax Con's of Koin: • Koin won't give you compile time checking leading to runtime errors if you don't set it up right. While conventional wisdom has it that failing at compile time is better than at runtime - and rightly so - I have come to feel there's an exception to be made here. • Maybe more but I haven't used it in anger yet.
I'm not sure the additional complexity of Dagger, over Koin, is worth it. Especially when you consider that most DI setup errors are not going to emerge in the corner-cases of your app and go uncaught until production.
👍 6
c

Cyberpunk Keanu

11/21/2019, 5:27 AM
Woah thanks for the insight. So in shorter words, unless you really really really need Dagger 2, use Koin instead
d

darkmoon_uk

11/21/2019, 5:37 AM
I'd go along with that statement, yes!
r

rkeazor

11/21/2019, 6:30 AM
Dagger is always better for production apps
👍 5
👎 7
Its literally the only one that scales. Its both Java and Kotlin compatible. it has enormous support ,esepically from Google. Koin is cool, don't get me wrong, but for production your safe bet is dagger. for smaller apps or personal apps, than perhaps koin
d

Derek Peirce

11/21/2019, 6:44 AM
I second using Dagger for production apps. I've lost count of how many times I was building what I thought would be a perfectly functional app, only to learn that there was a misconfigured dependency in one of the many features involved. At build time, it's easy enough to recognize and fix the issue. If I had to wait until I was running and testing the app to get a crash (I don't know what a Koin error looks like), or worse, had to wait for a battery of automation tests to report the error, or yet even worse, had to wait for production crashes, that would be intolerable.
r

rkeazor

11/21/2019, 6:50 AM
lol im lost are your saying your in support of Dagger?
d

Derek Peirce

11/21/2019, 7:09 AM
Yes. I prefer compile-time errors over runtime errors, especially ones that occur infrequently or in corner cases and are therefore harder to catch.
j

joeykaan

11/21/2019, 7:33 AM
In my experience Dagger is quite complex to get started with. Since it generates a lot of code which is not immediately visible it is not always easy to figure out how things work and how to fix things that might arise because of misusing it. I think Koin, or perhaps it was Kodein also has a way to test the entire dependency graph (be it at runtime, but you could do that for your local development builds), to verify that no dependencies are missing and I think it is quite a bit easier to setup. What is true is that Dagger has been used by a lot of production apps and you know you can trust it, Koin (I guess) still has to prove itself? What it boils down to is this for me: you have to decide which you find more important, 100% trust that it'll work and also scale but perhaps more problems with setting it up correctly. Then choose Dagger, or if you want to setup simple DI which might scale just fine, then use Koin/Kodein.
👍 2
d

darkmoon_uk

11/21/2019, 7:41 AM
Just make sure you have no cross-team dependencies where one team is producing a Dagger dependency and you're consuming it. This added a lot of pain and complexity to our dependency management when the 'ABI' broke between the generated classes and we were held back until we could convince the other team to upgrade.
d

dave08

11/21/2019, 1:08 PM
There's also #katana if all you need is something light-weight and simple
k

Kris Wong

11/21/2019, 2:05 PM
Also consider Google is officially endorsing, and contributing to, Dagger