Understanding Koin Keywords

In our previous blogs, we explored integrating Koin with Jetpack Compose. Now, let’s dive deeper into Koin’s core functionality by understanding its keywords and their use cases. This knowledge will form the foundation for creating scalable, maintainable applications with Koin.

1. startKoin {}

The startKoin function initializes the Koin framework. It is typically called in your application’s onCreate method to set up dependency injection for the app.

Example:

startKoin {
    modules(appModule)
}

Key Points:

  • What it does: Registers modules and starts Koin’s service locator.

  • Where to use: In your Application class or wherever your app’s lifecycle begins.

2. module {}

The module function is where you define your dependencies. It acts as a container for bindings that describe how instances are created and injected.

Example:

val appModule = module {
    single { GreetingService() }
}

Key Points:

  • What it does: Groups related dependencies together.

  • Where to use: In a centralized file or split by feature/module for better organization.

3. single {}

The single keyword ensures that a single instance of a dependency is created and shared across the entire app.

Example:

single { GreetingService() }

Key Points:

  • Use case: For dependencies that can be reused, such as repositories or services.

  • Lifecycle: Singleton – created once and shared.

4. factory {}

The factory keyword creates a new instance of the dependency each time it is requested.

Example:

factory { GreetingService() }

Key Points:

  • Use case: For dependencies like utility classes that should not be shared.

  • Lifecycle: Creates a new instance every time.

5. viewModel {}

The viewModel keyword simplifies injecting ViewModels into your app. It is specifically designed for Android’s MVVM architecture.

Example:

viewModel { GreetingViewModel(get()) }

Key Points:

  • Use case: For ViewModels in your app.

  • Lifecycle: Managed by Android’s ViewModel lifecycle.

6. get()

The get function retrieves an instance of a dependency from Koin’s container.

Example:

val service: GreetingService = get()

Key Points:

  • Use case: When you need to manually retrieve a dependency.

  • Integration: Works seamlessly with other Koin functions.

7. inject()

The inject function is used for lazy injection of dependencies.

Example:

val service: GreetingService by inject()

Key Points:

  • Use case: When dependencies are not immediately required.

  • Advantage: Improves performance by delaying initialization.

8. parametersOf()

The parametersOf function allows passing parameters to dependencies when they are retrieved.

Example:

val service: GreetingService = get { parametersOf("Compose") }

Key Points:

  • Use case: For dynamic dependency injection.

  • Flexibility: Enables customization during instantiation.

9. loadKoinModules() and unloadKoinModules()

These functions dynamically load or unload Koin modules at runtime.

Example:

loadKoinModules(featureModule)
unloadKoinModules(featureModule)

Key Points:

  • Use case: For modular applications where dependencies need to be loaded/unloaded dynamically.

  • Control: Offers fine-grained management of dependencies.

10. koinViewModel()

This function is specifically designed for injecting ViewModels in Jetpack Compose.

Example:

@Composable
fun GreetingScreen(viewModel: GreetingViewModel = koinViewModel()) {
    Text(viewModel.getGreetingMessage())
}

Key Points:

  • Use case: For Compose-based UI with ViewModels.

  • Ease of use: Simplifies ViewModel injection in Compose.

Conclusion

Koin’s keywords provide a powerful and flexible way to manage dependencies in your Android applications. Understanding these keywords enables you to:

  • Organize your dependencies effectively.

  • Create scalable, maintainable apps.

  • Take full advantage of Koin’s Kotlin-first design.

Akshay Nandwana
Founder AndroidEngineers

You can connect with me on:


Book 1:1 Session here
Click Here

Join our upcoming classes
https://www.androidengineers.in/courses

Love from our past students

Excellent list of questions really helped me to coverup all the topics before interview.

Saiteja Janjirala

10th Oct, 2024

I had an exceptional experience with the 1:1 mentorship session. Akshay was incredibly friendly and provided invaluable guidance on focusing on long-term goals. They also gave great interview tips, including a thorough resume review. Additionally, the discussion on Data Structures and Algorithms (DSA) was insightful and practical. Highly recommended for anyone looking to advance their career!

Nayab khan

11th Sep, 2024

Cleared my major points for what I am missing in the resume and also suggested what I can work on for further growth in the career.

Ketan Chaurasiya

7th Aug, 2024

What impressed me most was his personalized approach and practical tips that I could immediately apply. Akshay’s guidance not only improved my technical skills but also boosted my confidence in navigating my career path. His insights and encouragement have been a game-changer for me. I highly recommend Akshay’s mentorship to anyone looking to advance their Android development career.

Hardik Kubavat

5th Aug, 2024

2025© Made with   by Android Engineers.