Android Quick Reference

All you need for your daily work with Koin and Android

Gradle Setup

// Add Jcenter to your repositories if needed
repositories {
    jcenter()    
}
dependencies {
    // Koin for Android
    compile 'org.koin:koin-android:0.8.2'
    // Koin for Android Architecture Components
    compile 'org.koin:koin-android-architecture:0.8.2'
}

Declaring your module

Below the Koin DSL keywords:

  • declare component with provide, bean, factory and a lmabda function to declare your component instance
  • viewModel- to declare a ViewModel component (Android Architecture only)
  • context - to declare a logical context for a subset of components (Koin Contexts)
  • resolve component with get()
  • resolve Koin properties with getProperty()

Check the complete Koin DSL reference documentation.

All your ViewModel can be injected by constructor

Start Koin

Use startKoin() function to start Koin, from your Application class

class MyApplication : Application(){
    override fun onCreate() {
        super.onCreate()
        // Start Koin
        startKoin(this, listOf(myModule))
    }
}

Load Properties

By default, Koin look at the assets/koin.properties file to load Properties.

Additional properties

You can also provide additional properties at start:

class MyApplication : Application(){
    override fun onCreate() {
        super.onCreate()
        // Start Koin
        startKoin(this, listOf(myModule), properties = <YOUR PROPERTIES>)
    }
}

Android properties binding

You can bind Android properties to Koin properties:

class MyApplication : Application(){
    override fun onCreate() {
        super.onCreate()
        // ...

        bindString(R.string.myString, "MyString")
    }
}

An later use this proeprty with Koin:

  • in Koin dsl: getProperty("MyString")
  • in Android components : by property("MyString")

Inject your components

All components declared with Koin are injected by constructor, thanks to the DSL. For the given module declaration:

val module = applicationContext{
    bean { MyService(get())}
}

It allows to work with your injected constructor components

class MyService(val myComponent : MyComponent) {
    
    // you can use myComponent

}

Android Components

Android components are part of the Android fraemwork, and thus can’t can’t be created by 3thrd part library. You must conform to the Android lifecycle components. If we can’t create directly Activity, Fragments … we can make dependency injection into any Android components (Activity, Fragment, Service) with delegate style declaration:

class MyActivity : AppCompatActivity(){

    // use myService anywhere
    val myService by inject<MyService>()

}

This will force to design our components and system to be more independant from Android runtime.

Koin Contexts

Koin contexts allows to gather components defintion into a logical sub-container. Use a context when you need components that aren’t just singleton or factory, able to survive for a during lifetime. The context has to be dropped manually later.

Given a context defintion:

class MyPresenter()

val module = applicationContext{
    context("PresenterContext"){
        bean { MyPresenter() }
    }
}

You can create any instance from context definitions:

class MyActivity : AppCompatActivity(){

    // use myService anywhere
    val presenter by inject<MyPresenter>()

}

And you can drop instances when you don’t need it anymore:

fun onStop(){
    super.onStop()
    releaseContext("PresenterContext")
}

Quick notes for declaring your components

MVP and MVVM architecture approach are the most common approach. Below some

Model View Presenter

In MVP style you can use:

  • factory declaration for presenter, or use a context for more extended lifecycle component
  • avoid to use factory if several components need it, it will give you different instance each time
  • bean or provide for all singletons components

Model View ViewModel

Declare any ViewModel component with viewModel keyword DSL and Android Architecture components

use it directly in onCreate, with getViewModel():

class MyActivity : AppCompatActivity(){
    override fun onCreate() {
        super.onCreate()
        
        val myModel = getViewModel<MyModel>()
    }
}

or with delegate style declaration, in property with by viewModel():

class MyActivity : AppCompatActivity(){

    val myModel by viewModel<MyModel>()

}

Sharing ViewModel

To share a ViewModel between Activity/Fragments, just the by viewModel() notation.

class MyActivity : AppCompatActivity(){

    val myModel by viewModel<MyModel>()

}
class MyFragment : Fragment(){

    val myModel by viewModel<MyModel>()

}

If you want to create a new instance of ViewModel in your activity write it: by viewModel(fromActivity = false)

class MyFragment : Fragment(){

    // Create new instance
    val myModel by viewModel<MyModel>(fromActivity = false)

}

And now?

Check the Koin Android Developer Guide for more detailed recipes and strategies.