What is kotlin Koin? How to use Kotlin Koin in android?

Hey, what’s up, coders. In this post, I will introduce you to an awesome framework that you can use in your android project. We will learn about Kotlin Koin and we will see how you can use kotlin Koin in your android project. Here I want to make clear one thing that this tutorial will cover only the basics of Kotlin Koin. Once you have a basic understanding of Koin, going deep will be easier for you. So let’s begin.

What is Kotlin Koin?

Koin is a dependency injection framework written completely in kotlin. It is a lightweight framework and the best part is that learning Kotlin Koin is a piece of cake.

And here’s what the official site says –

Koin – a smart Kotlin injection library to keep you focused on your app, not on your tools“.

Alright guys, now let’s see how to include Kotlin Koin in your android project to manage the dependencies. We will create a simple app that will show a student name in a TextView.

First you need to add the Kotlin koin dependency for android in your project. Here’s the dependency –

implementation "org.koin:koin-android:1.0.2"

Now I have a data class Student and I want to inject this class into my MainActivity.

Student.kt

package com.example.kotlinkoinexample

data class Student(
        val name : String
)

Now you will have to declare module. So first let’s understand what a module is. So in one line, module is used by koin to provide all the dependencies.

So here’s the module.

module.kt

package com.example.kotlinkoinexample

import org.koin.dsl.module.module

val applicationModule= module {
    single { Student("Liam") }
}

single{} is used to provide singleton instances.

Now you have to launch the dependency injection process. To do this you just have to write a single line –

startKoin(this, listOf(applicationModule))

See this line in the code below –

MyApplication.kt

package com.example.kotlinkoinexample

import android.app.Application
import org.koin.android.ext.android.startKoin

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

        //Start the dependency injection process
        startKoin(this, listOf(applicationModule))
    }
}

Here, you need to pass two parameters – First one is the context and the second one is the list of modules. In this case, we have only one module applicationModule that we created earlier.

Now you can inject the dependency. See the MainActivity code below.

MainActivity.kt

package com.example.kotlinkoinexample

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import org.koin.android.ext.android.inject

class MainActivity : AppCompatActivity() {

    //Inject the Student class dependency
    private val student: Student by inject()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val name=findViewById<TextView>(R.id.name)

        //Show student name to the Textview
        name.text = student.name;
    }
}

Notice the line in the code where I have injected the Student class.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.698"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.219" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="152dp"
        android:text="Student Name :"
        android:textSize="24sp"
        app:layout_constraintEnd_toStartOf="@+id/name"
        app:layout_constraintHorizontal_bias="0.541"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Output :

Now that you have a basic understanding of Kotlin Koin, you can explore more.

And this ends the post.

Goodbye.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: