Skip to main content
Version: 3.2

Android

This tutorial lets you write an Android/Kotlin application and use Koin inject and retrieve your components.

Get the code

Gradle Setup

Add the Koin Android dependency like below:

// Add Maven Central to your repositories if needed
repositories {
mavenCentral()
}
dependencies {
// Koin for Android
implementation "io.insert-koin:koin-android:$koin_version"

// Koin Test
testImplementation "io.insert-koin:koin-test:$koin_version"
testImplementation "io.insert-koin:koin-test-junit4:$koin_version"
}

Our components

Let's create a HelloRepository to provide some data:

interface HelloRepository {
fun giveHello(): String
}

class HelloRepositoryImpl() : HelloRepository {
override fun giveHello() = "Hello Koin"
}

Let's create a presenter class, for consuming this data:

class MySimplePresenter(val repo: HelloRepository) {

fun sayHello() = "${repo.giveHello()} from $this"
}

Writing the Koin module

Use the module function to declare a module. Let's declare our first component.

Classical DSL way:

val appModule = module {

// single instance of HelloRepository
single<HelloRepository> { HelloRepositoryImpl() }

// Simple Presenter Factory
factory { MySimplePresenter(get()) }
}

Constructor DSL way:

val appModule = module {

// single instance of HelloRepository
singleOf(::HelloRepositoryImpl) { bind<HelloRepository>() }

// Simple Presenter Factory
factoryOf(::MyPresenter)
}

We declare our MySimplePresenter class as factory to have a new instance created each time our Activity need one.

Start Koin

Now that we have a module, let's start it with Koin. Open your application class, or make one (don't forget to declare it in your manifest.xml). Just call the startKoin() function:

class MyApplication : Application(){
override fun onCreate() {
super.onCreate()
// Start Koin
startKoin{
androidLogger()
androidContext(this@MyApplication)
modules(appModule)
}
}
}

Injecting dependencies

The MySimplePresenter component will be created with HelloRepository instance. To get it into our Activity, let's inject it with the by inject() delegate injector:

class MySimpleActivity : AppCompatActivity() {

// Lazy injected MySimplePresenter
val firstPresenter: MySimplePresenter by inject()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

//...
}
}
info

The by inject() function allows us to retrieve Koin instances, in Android components runtime (Activity, fragment, Service...)

info

The get() function is here to retrieve directly an instance (non lazy)

Verifying your graph

We can ensure that our Koin configuration is good before launching our app, by verifying our Koin configuration with a simple JUnit Test.

You need to declare a MockProviderRule to declare how you mock a class (here for example, we use Mockito).

The checkModules function allow to verify the given Koin modules:

class CheckModulesTest : KoinTest {

// Declare Mock with Mockito
@get:Rule
val mockProvider = MockProviderRule.create { clazz ->
Mockito.mock(clazz.java)
}

// verify the Koin configuration
@Test
fun checkAllModules() = checkModules {
modules(appModule)
}
}
``