Android

All you need to know about koin-android & koin-android-architecture API

This section describes the koin-android & koin-android-architecture API

DSL

Access all the Koin DSL, plus:

  • androidApplication() - resolve Application type dependency
  • viewModel - declare a ViewModel component (koin-android-architecture only)

Start

Function startKoin() can be launched only from an Application class. It has the following properties:

  • application - your Application instance
  • modules - list of your Koin modules
  • properties: list of additional properties
  • logger - Koin Logger used (default is AndroidLogger())

The startKoin() function will also check the assets/koin.properties to load properties

Function loadKoinModules() can also be called the with function, to provide an instance Android Application:loadKoinModules() with this // if this is an Application instance

Injection

Your Koin components are injected by constructor, with the DSL.

Your Actvivity/Fragment/Service Android classes can use following Koin functions:

  • get() get desired dependency
  • by inject() lazy delagate to inject the desired dependency
  • by property() lazy inject property value
  • releaseContext() release the given context by its name

ViewModel

A ViewModel is a Kotlin class that extends Android android.arch.lifecycle.ViewModel. Koin can inject your ViewModel by constructor

In Activity or Fragment:

  • getViewModel() fetch given ViewModel dependency
  • by viewModel() lazy delegate to inject the ViewModel

In Fragment:

  • Fragment.getSharedViewModel() fetch given ViewModel dependency - shared with Activity
  • Fragment by sharedViewModel() lazy delegate to inject the ViewModel - shared with Activity