Dagger vs Koin — Battle Dependencies Injection Framework in Android Development
Dependencies injection is technique to supplies object from external static object, with DI your class will be more modular because you only need depend on your interface and simplify your testing. Now a lot of framework that help you to implement DI in android development but this time we will discuss about Dagger and Koin. Dagger are most popular DI framework in android development that maintained by Google, but since android development starting migrate from java to kotlin then appear Koin as new competitor.
We start compare both framework about implementation in android development, so there is minimum dependencies at gradle if we want to use Dagger in android project.
Then how about Koin? with Koin we only need to add one dependencies at gradle that is Koin android core but if you need bring Android scope feature you can add Koin android scope dependencies and if you use ViewModel from Android Arch you need to add Koin android view model dependencies.
Ok we have add DI framework at our project so what next? First we need to create module contains collection of object provider. With Dagger we can use @Module annotation to describe that class is work as module then use @Provides annotation to describe that method will supply object that will consume by another class.
That code describe we create room module contains provider of AuthDao object and ContentDao object, ok look simple but i found issue when we want to provide ViewModel. ViewModel need to create from ViewModelFactory to work with Dagger than map every ViewModel with key
At this point we have try to provide Room object and ViewModel object, so what about activity or fragment? If we want provide activity or fragment class we can use @ContributesAndroidInjector annotation to method that will be object provider
Then what format of module that will be act as collection of provider with Koin? with Koin we must pass collection of provider to Koin module function and grab object provider inside Koin single function
So how about ViewModel? we need to create ViewModelFactory provider first like Dagger way? the answer is no, cause as we say before Koin have ViewModel provider feature.
Ok at this point we have create object provider so how we access it. With Dagger we must use @Inject annotation to indicate that object will supply from method provider inside dagger module. After that you need to add AndroidInjection.inject(this) to use dagger from Activity or Fragment. Then if your Activity contains fragment, don’t forget to extending it with HasSupportFragmentInjector interface
You will see with Dagger we must need more special annotation, interface etc to implement DI but at this step Koin doesn’t need other implementation. Your class will be no have different with or without use DI. Especially for ViewModel object you can access using Koin viewModel function
Ok we have know how to provide and how to access it, but don’t forget we must register that module at our application. With Dagger we must group module to Component then that component will registered at Android Application Class
Then when we try to register Koin module to our Application it will be look more simple
Finally we have try to implement DI with Dagger and Koin, we see what we do to achieve DI with Koin look more simple and more kotlin way but we know Dagger is maintained by Google and more mature. So let’s discuss if you have other perspective about DI with Dagger or Koin.
You can find the final implementation using insertkoin here