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.
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.
startKoin {
modules(appModule)
}
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.
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.
val appModule = module {
single { GreetingService() }
}
What it does: Groups related dependencies together.
Where to use: In a centralized file or split by feature/module for better organization.
single {}
The single
keyword ensures that a single instance of a dependency is created and shared across the entire app.
single { GreetingService() }
Use case: For dependencies that can be reused, such as repositories or services.
Lifecycle: Singleton – created once and shared.
factory {}
The factory
keyword creates a new instance of the dependency each time it is requested.
factory { GreetingService() }
Use case: For dependencies like utility classes that should not be shared.
Lifecycle: Creates a new instance every time.
viewModel {}
The viewModel
keyword simplifies injecting ViewModels into your app. It is specifically designed for Android’s MVVM architecture.
viewModel { GreetingViewModel(get()) }
Use case: For ViewModels in your app.
Lifecycle: Managed by Android’s ViewModel lifecycle.
get()
The get
function retrieves an instance of a dependency from Koin’s container.
val service: GreetingService = get()
Use case: When you need to manually retrieve a dependency.
Integration: Works seamlessly with other Koin functions.
inject()
The inject
function is used for lazy injection of dependencies.
val service: GreetingService by inject()
Use case: When dependencies are not immediately required.
Advantage: Improves performance by delaying initialization.
parametersOf()
The parametersOf
function allows passing parameters to dependencies when they are retrieved.
val service: GreetingService = get { parametersOf("Compose") }
Use case: For dynamic dependency injection.
Flexibility: Enables customization during instantiation.
loadKoinModules()
and unloadKoinModules()
These functions dynamically load or unload Koin modules at runtime.
loadKoinModules(featureModule)
unloadKoinModules(featureModule)
Use case: For modular applications where dependencies need to be loaded/unloaded dynamically.
Control: Offers fine-grained management of dependencies.
koinViewModel()
This function is specifically designed for injecting ViewModels in Jetpack Compose.
@Composable
fun GreetingScreen(viewModel: GreetingViewModel = koinViewModel()) {
Text(viewModel.getGreetingMessage())
}
Use case: For Compose-based UI with ViewModels.
Ease of use: Simplifies ViewModel injection in Compose.
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:
Join our upcoming classes
https://www.androidengineers.in/courses