Skip to content

Getting Started with Koin Annotations 1.4 in Compose Multiplatform

Getting Started with Koin Annotations 1.4 in Compose Multiplatform

If you've been using Koin's DSL for dependency injection in your Kotlin Multiplatform projects, you might be interested in exploring a more annotation-driven approach. In this guide, Arnaud walks you through migrating a simple Compose Multiplatform application from Koin DSL to the new Koin Annotations 1.4.

Project Overview

We'll be working with the KMP App Template from JetBrains, which provides a great starting point for our exploration. The data displayed by the app is from The Metropolitan Museum of Art Collection API This app already uses Koin for dependency injection. This template implements this simple art gallery application that displays a list of paintings and their details. While the functionality is straightforward, it serves as an excellent example to demonstrate Koin's annotation-based dependency injection. And bonus : it's beautiful to look at. 

Setting Up Dependencies

Before diving into the migration, let's ensure we have all the necessary dependencies:

Screenshot 2024-10-25 at 09.03.09

Don't forget to set up KSP (Kotlin Symbol Processing) in your project, as it's essential for annotation processing.

Project Structure

Our sample application follows a clean architecture approach with the following key components:

  • Data Layer:
    • MuseumAPI: Handles remote data fetching
    • MuseumRepository: Manages data operations
    • MuseumStorage: Handles local data persistence
  • UI Layer:
    • List Screen: Displays paintings grid
    • Detail Screen: Shows painting details
    • ViewModels: Manages UI state and business logic

Migrating to Koin Annotations

Step 1: View Model Migration

Let's start by migrating our view models from DSL to annotations. Replace the existing DSL module definition:

Screenshot 2024-10-25 at 09.03.45

Step 2: Data Layer Migration

Next, let's tackle the data layer components:

Screenshot 2024-10-25 at 09.04.32

Step 3: Complex Initializations

For more complex scenarios where you need custom initialization logic, you can use separate functions with the @Single annotation:

Screenshot 2024-10-17 at 15.08.46

Platform-Specific Components

One of the powerful features of Koin Annotations is its support for platform-specific implementations. Here's how to set it up:

Screenshot 2024-10-25 at 09.05.21

Benefits of Using Koin Annotations

🔸 Cleaner Code: Annotations provide a more declarative and concise way to define dependencies
🔸 Better IDE Support: Enhanced code navigation and refactoring capabilities
🔸 Compile-Time Validation: Earlier detection of dependency injection issues
🔸 Reduced Boilerplate: KSP generates necessary extension functions automatically
🔸 Platform-Specific Support: Seamless handling of platform-specific dependencies

Wrapping Up

Migrating from Koin DSL to Annotations might require some initial setup, but the benefits in terms of code clarity and maintainability are well worth it. The annotation-based approach provides a more familiar paradigm for developers coming from other dependency injection frameworks while maintaining Koin's lightweight and Kotlin-first philosophy.

Remember to check out the official Koin documentation for more detailed information and advanced usage scenarios. Tell us what you think!