DEV Community

Cover image for Clean Architecture Made Simple: A Koin DI Walkthrough for Android
supriya shah
supriya shah

Posted on

Clean Architecture Made Simple: A Koin DI Walkthrough for Android

Originally published on Medium:
https://medium.com/@supsabhi/clean-architecture-made-simple-a-koin-di-walkthrough-for-android-c1f6fd95a718
Dependency injection (DI) is a staple in modern Android development. Whether you’re a seasoned pro or just getting your feet wet, you’ve probably encountered DI’s benefits: loose coupling, modularity, reduced redundancy, and improved reusability. But the real magic of DI often reveals itself as your app grows in size and complexity-suddenly, managing dependencies by hand becomes a headache you didn’t see coming.

At first, DI frameworks can seem intimidating. Many developers, when first introduced to DI, think, “Why do I need this extra layer? Isn’t it just making things more complicated?” And, to be fair, some DI frameworks do live up to that reputation-they can be complex and require a significant investment of time to learn and implement.

But not all DI frameworks are created equal. If you’re looking for a straightforward, Kotlin-first solution, let me introduce you to Koin.

Koin is a lightweight dependency injection framework designed specifically for Kotlin and Kotlin Multiplatform projects. Whether you’re building an Android app, a multiplatform project, or even a backend with Ktor, Koin has you covered. Developed by Kotzilla and a vibrant open-source community, Koin is known for its simplicity, minimal setup, and intuitive syntax.

If you want to streamline your Android architecture with as little boilerplate as possible, Koin is an excellent choice. Let’s walk through how you can implement Koin in your next Android project.

Step 1: Add Koin to Your Project
First, add the Koin dependency to your app-level build.gradle file

implementation "io.insert-koin:koin-android:3.5.3

Enter fullscreen mode Exit fullscreen mode

(Be sure to check for the latest version before adding.)

Step 2: Create Your Classes
Let’s set up a simple example with a repository and a ViewModel:

class LocationRepository {
    fun getLocation(): String = "Dubai"
}

class LocationViewModel(private val repository: LocationRepository) {
    fun getLocation() = repository.getLocation()
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Define Koin Modules
A Koin module tells the framework how to provide your dependencies. Here’s how you can define one for a network client, repository, and ViewModel:

val appModule = module {
    single { NetworkClient() }
    single { LocationRepository(get()) }
    viewModel { LocationViewModel(get()) }
}
Enter fullscreen mode Exit fullscreen mode

single creates a singleton instance.
viewModel is a Koin extension for providing ViewModels.
Step 4: Initialize Koin in Your Application Class
Next, set up Koin in your Application class:

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidContext(this@MyApp)
            modules(appModule)
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

And don’t forget to declare your custom Application class in your AndroidManifest.xml:

<application
    android:name=".MyApp"
    ... >
</application>
Enter fullscreen mode Exit fullscreen mode

Step 5: Inject Dependencies Where You Need Them
Now, you can inject your dependencies directly into your Android components. For example, in an Activity:

class MainActivity : AppCompatActivity() {
private val viewModel: LocationViewModel by inject()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println(viewModel.getLocation())
    }
}
Enter fullscreen mode Exit fullscreen mode

Wrapping Up
Koin brings a modern, pragmatic approach to dependency injection in Android. Its concise syntax, absence of code generation, and seamless Kotlin integration make it a favorite for developers who value simplicity and maintainability. By following these steps, you can quickly set up Koin and enjoy cleaner, more modular code in your Android projects.

Ready to take your architecture to the next level? Give Koin a try in your next Android app-you might just wonder how you ever coded without it.

That’s all for now. Happy coding, and see you in the next article!

Top comments (0)