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.
Join our upcoming 6 Weeks Android Mentorship Program
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.
Join our upcoming 6 Weeks Android Mentorship Program