koin
Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
Top Related Projects
A fast dependency injector for Android and Java.
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 11 and above, brought to you by Google.
A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Uber's cross-platform mobile architecture framework.
Mavericks: Android on Autopilot
Quick Overview
Koin is a pragmatic lightweight dependency injection framework for Kotlin developers. It's designed to be simple, practical, and efficient, providing a straightforward way to manage dependencies in Kotlin projects, including Android applications.
Pros
- Easy to set up and use, with minimal boilerplate code
- Lightweight and fast, with no code generation or reflection
- Supports multiplatform projects (JVM, Android, iOS, JS)
- Excellent documentation and community support
Cons
- Limited compile-time safety compared to some other DI frameworks
- May not be suitable for very large-scale projects with complex dependency graphs
- Less feature-rich compared to more established DI frameworks like Dagger
Code Examples
- Defining a module:
val appModule = module {
single { DatabaseService() }
factory { UserRepository(get()) }
}
This code defines a Koin module with a singleton DatabaseService and a factory for UserRepository.
- Starting Koin in an Android application:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MyApplication)
modules(appModule)
}
}
}
This code initializes Koin in an Android application, providing the application context and modules.
- Injecting dependencies in a ViewModel:
class MyViewModel(
private val userRepository: UserRepository
) : ViewModel() {
// ViewModel implementation
}
// In your Activity or Fragment
val myViewModel: MyViewModel by viewModel()
This example shows how to inject dependencies into a ViewModel using Koin.
Getting Started
To start using Koin in your Kotlin project:
- Add the Koin dependency to your
build.gradle
file:
dependencies {
implementation "io.insert-koin:koin-core:3.4.0"
// For Android projects, also add:
// implementation "io.insert-koin:koin-android:3.4.0"
}
- Define your modules:
val appModule = module {
single { MyService() }
factory { MyRepository(get()) }
}
- Start Koin in your application:
fun main() {
startKoin {
modules(appModule)
}
}
- Inject dependencies where needed:
class MyClass(private val myService: MyService)
// Usage
val myClass: MyClass by inject()
Competitor Comparisons
A fast dependency injector for Android and Java.
Pros of Dagger
- More powerful and flexible for complex dependency graphs
- Compile-time code generation for better performance
- Extensive documentation and large community support
Cons of Dagger
- Steeper learning curve and more complex setup
- Requires annotation processing, increasing build times
- More verbose code, especially for simple use cases
Code Comparison
Koin:
val myModule = module {
single { MyService() }
factory { MyViewModel(get()) }
}
Dagger:
@Module
class MyModule {
@Provides
fun provideMyService(): MyService = MyService()
@Provides
fun provideMyViewModel(service: MyService): MyViewModel = MyViewModel(service)
}
Key Differences
- Koin uses a DSL for dependency declaration, while Dagger uses annotations
- Dagger generates code at compile-time, Koin resolves dependencies at runtime
- Koin is more lightweight and easier to set up for simple projects
- Dagger offers stronger compile-time checks and better performance for large-scale applications
Both libraries are popular choices for dependency injection in Android development, with Koin being favored for its simplicity and Dagger for its robustness in complex scenarios.
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 11 and above, brought to you by Google.
Pros of Guice
- More mature and battle-tested, with a larger community and extensive documentation
- Supports Java SE applications, not limited to Android development
- Offers more advanced features like AOP (Aspect-Oriented Programming) support
Cons of Guice
- Steeper learning curve due to more complex API and concepts
- Requires more boilerplate code compared to Koin's lightweight syntax
- Not specifically optimized for Android, which may lead to larger APK sizes
Code Comparison
Guice:
public class RealBillingService implements BillingService {
@Inject
public RealBillingService(CreditCardProcessor processor,
TransactionLog transactionLog) {
// ...
}
}
Koin:
val myModule = module {
single<BillingService> { RealBillingService(get(), get()) }
}
Summary
Guice is a more comprehensive dependency injection framework for Java, offering advanced features and broader application support. However, it comes with increased complexity and verbosity. Koin, on the other hand, provides a simpler, Kotlin-focused solution that's particularly well-suited for Android development, sacrificing some advanced features for ease of use and a more lightweight implementation.
A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Pros of Toothpick
- Faster compile times due to its annotation-based approach
- More flexible scoping options, allowing for fine-grained control over object lifetimes
- Supports method injection, which can be useful in certain scenarios
Cons of Toothpick
- Steeper learning curve compared to Koin's simplicity
- Requires more boilerplate code for setup and configuration
- Less active community and fewer resources available online
Code Comparison
Koin setup:
val myModule = module {
single { MyService() }
factory { MyViewModel(get()) }
}
Toothpick setup:
@Module
class MyModule {
@Provides @Singleton
fun provideMyService(): MyService = MyService()
@Provides
fun provideMyViewModel(service: MyService): MyViewModel = MyViewModel(service)
}
Both Koin and Toothpick are dependency injection frameworks for Android and Kotlin projects. Koin is known for its simplicity and ease of use, while Toothpick offers more advanced features at the cost of complexity. Koin uses a DSL-based approach, making it more readable and requiring less setup. Toothpick, on the other hand, uses annotations and provides more control over scoping and object lifetimes. The choice between the two depends on project requirements and developer preferences.
Uber's cross-platform mobile architecture framework.
Pros of RIBs
- Designed for large-scale mobile app architecture
- Promotes modularity and testability
- Provides a clear separation of business logic and UI
Cons of RIBs
- Steeper learning curve compared to Koin
- More complex setup and boilerplate code
- Primarily focused on mobile development, less versatile for other platforms
Code Comparison
RIBs (Router implementation):
class SomeRouter(
interactor: Interactor<SomeInteractor.SomePresenter, SomeRouting>,
component: SomeBuilder.Component
) : ViewRouter<SomeView, SomeInteractor, SomeBuilder.Component>(interactor, component)
Koin (Module definition):
val myModule = module {
single { MyService() }
factory { MyViewModel(get()) }
}
RIBs focuses on a hierarchical architecture with routers, interactors, and builders, while Koin provides a simpler dependency injection approach. RIBs is more suitable for complex, large-scale mobile applications, whereas Koin is more flexible and easier to adopt for various project types and sizes.
Mavericks: Android on Autopilot
Pros of Mavericks
- More comprehensive architecture framework for Android apps
- Better suited for large-scale applications with complex state management
- Includes built-in support for ViewModels and state reduction
Cons of Mavericks
- Steeper learning curve due to its more complex architecture
- May be overkill for smaller projects or simpler apps
- Less flexibility in terms of integration with other libraries
Code Comparison
Koin (Dependency Injection):
val myModule = module {
single { MyRepository() }
viewModel { MyViewModel(get()) }
}
Mavericks (State Management):
class MyState(val data: String) : MavericksState
class MyViewModel(initialState: MyState) : MavericksViewModel<MyState>(initialState) {
fun updateData(newData: String) = setState { copy(data = newData) }
}
Summary
Koin is a lightweight dependency injection framework, while Mavericks is a more comprehensive architecture framework. Koin is easier to adopt and more flexible, making it suitable for a wide range of projects. Mavericks offers robust state management and is better suited for complex, large-scale applications. The choice between the two depends on project requirements, team expertise, and application complexity.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
What is KOIN? â¨
Koin is a pragmatic lightweight dependency injection framework for Kotlin developers, developed by Kotzilla and open-source contributors.
Koin is a DSL, a light container and a pragmatic API
Setup & Current Version ð¦
Follow the dedicated setup page to setup Koin for your project. Additionally, you may need to check the last changes for Maven Central packages.
Here are the current available Koin project versions:
- Koin
- Koin for Compose
- Koin Annotations
- Koin - Official Long Term Support for Enterprises by Kotzilla
Latest News & Resources ð
- Official Website: insert-koin.io
- Twitter: @insertkoin_io
- Blog: Koin Developers
- Newsletter: Koin Newsletter
Koin Tutorials ð
You can find here tutorials to help you learn and get started with Koin framework:
- Kotlin
- Kotlin with Koin Annotations
- Android
- Android with Koin Annotations
- Android Jetpack Compose
- Kotlin Multiplatform
- Ktor
Community ð¬
- Come talk on slack #koin channel
- Post your question on Stackoverflow
- Found a bug or a problem? Open an issue on Github issues
Contributing ð
Want to help or share a proposal about Koin? problem on a specific feature?
- Open an issue to explain the issue you want to solve Open an issue
- Come talk on slack #koin-dev channel
- After discussion to validate your ideas, you can open a PR or even a draft PR if the contribution is a big one Current PRs
Additional readings about basic setup: https://github.com/InsertKoinIO/koin/blob/master/CONTRIBUTING.adoc
Contributors
Thank you all for your work! â¤ï¸
OpenCollective - Sponsorship â¤ï¸
Support this project by becoming a sponsor and be displayed on the offcial website. [Help us and Become a sponsor!]
Top Related Projects
A fast dependency injector for Android and Java.
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 11 and above, brought to you by Google.
A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Uber's cross-platform mobile architecture framework.
Mavericks: Android on Autopilot
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot