HomeAndroidKoin Dependency Injection for Android

Koin Dependency Injection for Android

Dependency Injection (DI) is a design pattern which has been around for while, but recently it has become more commonly used in the development of Android applications, due mainly to the implementation of some rather nifty DI frameworks. You must be aware of Marinator – Dependency Injection Framework for Android. However, in this journal entry we will be having a look at Koin Dependency Injection which uses Kotlin for Android.

DI allows developers to write code that has low coupling and which can therefore be easily tested. The more complex and longer lived your Android software the more important it becomes to be able to test it effectively.

Koin Dependency Injection

KOIN is a simple (but powerful) dependency injection framework for Android. It uses Kotlin and its functional power to get things done! No proxy/CGLib, no code generation, no introspection. Just functional Kotlin and DSL magic 😉

KOIN is a very small library, that aims to be as simple as possible and let’s you write dependency injection in a breath.

Just describe your stuff and inject it!

Getting Started

To get started with using Koin on your Kotlin based Android application make sure that you have jcenter repository in your gradle.build file. Once confirmed, add the following gradle dependency to your Android app:

// Koin for Android
compile 'org.koin:koin-android:0.5.2'
// If you need Koin for your tests
testCompile 'org.koin:koin-test:0.5.2'

Setup Your Application

To start KOIN and your modules, all you have to do is use the startAndroidContext function in your Android Application like below:

class MainApplication:Application(){

    override fun onCreate() {
        // Start Koin
        startAndroidContext(this, weatherAppModules())

The startAndroidContext function requires an Application instance, and a a list of modules to run.

To get a more grasp about KOIN, Modules, check out this wiki page.


Most Popular