Getting started with Kotlin & Koin

Let’s start with a standalone Kotlin app

About

This tutorial lets you write a Kotlin application and use Koin inject and retrieve your components.

Get the code

Checkout the project directly on Github or download the zip file

Setup

First, check that the koin-core dependency is added like below:

// Add Jcenter to your repositories if needed
repositories {
    jcenter()    
}
dependencies {
    // Koin for Kotlin apps
    compile 'org.koin:koin-core:1.0.1'
    // Testing
    testCompile 'org.koin:koin-test:1.0.1'
}

The application

In our small app we need to have 2 components:

  • HelloMessageData - hold data
  • HelloService - use and display data from HelloMessageData
  • HelloApplication - retrieve and use HelloService

Data holder

Let’s create a HelloMessageData data class to hold our data:

/**
 * A class to hold our message data
 */
data class HelloMessageData(val message : String = "Hello Koin!")

Service

Let’s create a service to display our data from HelloMessageData. Let’s write HelloServiceImpl class and its interface HelloService:

/**
 * Hello Service - interface
 */
interface HelloService {
    fun hello(): String
}


/**
 * Hello Service Impl
 * Will use HelloMessageData data
 */
class HelloServiceImpl(private val helloMessageData: HelloMessageData) : HelloService {

    override fun hello() = "Hey, ${helloMessageData.message}"
}

The application class

To run our HelloService component, we need to create a runtime component. Let’s write a HelloApplication class and tag it with KoinComponent interface. This will later allows us to use the by inject() functions to retrieve our component:

/**
 * HelloApplication - Application Class
 * use HelloService
 */
class HelloApplication : KoinComponent {

    // Inject HelloService
    val helloService by inject<HelloService>()

    // display our data
    fun sayHello() = println(helloService.hello())
}

Declaring dependencies

Now, let’s assemble HelloMessageData with HelloService, with a Koin module:

val helloModule = module {

    single { HelloMessageData() }

    single { HelloServiceImpl(get()) as HelloService }
}

We declare each component as single, as singleton instances.

  • single { HelloMessageData() } : declare a singleton of HelloMessageData instance
  • single { HelloServiceImpl(get()) as HelloService } : Build HelloServiceImpl with injected instance of HelloMessageData, declared a singleton of HelloService.

That’s it!

Just start our app from a main function:

fun main(vararg args: String) {

    startKoin(listOf(helloModule))

    HelloApplication().sayHello()
}

What’s next?

Also other getting started project: