Categories
android blog koin kotlin

Using Koin for dependency management in Android apps

So I have been using Dagger2 in previous apps and I was quite ok with it. But I always felt that Dagger may be an overkill for small apps and even medium apps. So when I started working on a new Android app I decided to look for alternative ways to manage my code dependencies. I had a couple of ideas on what I need:

  • Good integration with the Android ecosystem
  • Easy to understand for new team members
  • Good performance

I heard about Koin quite some time ago so I thought this is the perfect opportunity to try it out. I did have one issue with Koin, give that it resolves dependencies during the run time I was worried about the performance a bit. So I tried to find some benchmarks and luckily I found what I was looking for in this blog post! Koin 2.x had a huge performance upgrade compared to Koin 1.x, almost a 10x improvement. For 400 dependencies it takes Koin 2.x around 10 ms to walk up the dependency graph and inject required dependencies which is more than enough for my current project.

Now comes the fun part, how easy is it to setup Koin on a new project? I have to be honest here, it took me no more than 20 minutes to get the basics of Koin and have a couple of dependencies injected in my activities. I was blown away by how simple Koin is. There are three basic concepts in Koin

  • Modules
  • Injection
  • Module list

Lets quickly explain each concept.

Modules

Similar to Dagger, Koin uses a module to group actual instances you will inject. So in my app, I have ServiceModule (dependencies for a background service), PresentationModule (android ViewModels) and DataModule (various repositories)

val myModule : Module = applicationContext {
    // ViewModel instance of MyViewModel
    // get() will resolve Repository instance
    viewModel { MyViewModel(get()) }
    // Single instance of Repository
    single<repository> { MyRepository() }
}

Injection

After instances are being provided in the modules there is a special inject() method that can inject those dependencies into Activities, ViewModels or Repositories. To give an example:

class MyActivity : AppCompatActivity(){
    // Inject MyPresenter
    val presenter : MyPresenter by inject()
    override fun onCreate() {
        super.onCreate()
        // or directly retrieve instance
        val presenter : MyPresenter = get()
    }
}

Module list

Before everything is tied up together, Koin needs to be started and all modules have to be connected so that Koin can start connecting them together. This is done with a special startKoin function that lives in the application class.

 startKoin {
            // use AndroidLogger as Koin Logger - default Level.INFO
            androidLogger()
            // use the Android context given there
            androidContext(this@MainApplication)
            // module list
            modules(serviceModule, presentationModule, dataModule)
        }

And that is basically it. In the next couple of articles, we will dig a bit deeper into some nuances that Koin has in store for us and how I solved some problems with injection in my current android project.

Stay tuned!

Leave a Reply

Your email address will not be published. Required fields are marked *