What is KOIN?
A pragmatic lightweight dependency injection framework for Kotlin developers.
Written in pure Kotlin, using functional resolution only: no proxy, no code generation, no reflection.
Koin is a DSL, a light container and a pragmatic API
https://insert-koin.io
Official Website 👉Check the getting started section from our website, to discover Koin with the favorite platform.
Contact & Latest News 🌐
- Follow us on Twitter for latest news: @insertkoin_io
- Koin developers on Medium: koin developers hub
Getting Help 🚒
Any question about Koin usage?
- Come talk on slack #koin channel
- Post your question on Stackoverflow - #koin tag
Reporting issues 🛠
Found a bug or a problem on a specific feature? Open an issue on Github issues
Setup
Actual Version
koin_version = '1.0.0'
Gradle
Jcenter
Check that you have the jcenter
repository.
// Add Jcenter to your repositories if needed
repositories {
jcenter()
}
Dependencies
Choose your Koin dependency:
Core features
// Koin for Kotlin
implementation "org.koin:koin-core:$koin_version"
// Koin extended & experimental features
implementation "org.koin:koin-core-ext:$koin_version"
// Koin for Unit tests
testImplementation "org.koin:koin-test:$koin_version"
// Koin for Java developers
implementation "org.koin:koin-java:$koin_version"
Android
// Koin for Android
implementation "org.koin:koin-android:$koin_version"
// Koin Android Scope feature
implementation "org.koin:koin-android-scope:$koin_version"
// Koin Android ViewModel feature
implementation "org.koin:koin-android-viewmodel:$koin_version"
AndroidX
// AndroidX (based on koin-android)
// Koin AndroidX Scope feature
implementation "org.koin:koin-androidx-scope:$koin_version"
// Koin AndroidX ViewModel feature
implementation "org.koin:koin-androidx-viewmodel:$koin_version"
SparkJava
// Koin for Spark Kotlin
implementation "org.koin:koin-spark:$koin_version"
Ktor
// Koin for Ktor Kotlin
implementation "org.koin:koin-ktor:$koin_version"
Quickstart
Declare a Koin module
Write with the Koin DSL what you need to assemble:
// Given some classes
class Controller(val service : BusinessService)
class BusinessService()
// just declare it
val myModule = module {
single { Controller(get()) }
single { BusinessService() }
}
Start Koin
Use the startKoin() function to start Koin in your application.
In a Kotlin app:
fun main(vararg args : String) {
// start Koin!
startKoin(listOf(myModule))
}
In an Android app:
class MyApplication : Application() {
override fun onCreate(){
super.onCreate()
// start Koin!
startKoin(this, listOf(myModule))
}
}
Inject in Android
Easy to inject into your Android classes:
// Just inject in a simple Activity
class MyActivity() : AppCompatActivity() {
// lazy inject BusinessService into property
val service : BusinessService by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// or directly get any instance
val service : BusinessService = get()
}
}
Inject by constructor
You're now ready! The Koin DSL help you make injection by constructors. Now just use it:
// Controller & BusinessService are declared in a module
class Controller(val service : BusinessService){
fun hello() {
// service is ready to use
service.sayHello()
}
}
Koin can be easily embedded with your favorite Java/Kotlin SDK, and already provide some dedicated support module.
Ready for ViewModel
Want to use Android Architecture ViewModel? No problem, it's already available and easy to use:
// Injected by constructor
class MyViewModel(val repo : MyRepository) : ViewModel(){
}
// declare ViewModel using the viewModel keyword
val myModule : Module = module {
viewModel { MyViewModel(get()) }
single { MyRepository() }
}
// Just get it
class MyActivity() : AppCompatActivity() {
// lazy inject MyViewModel
val vm : MyViewModel by viewModel()
}
Easy testing
Use koin from a simple JUnit class:
// Just tag your class with KoinTest to unlick your testing power
class SimpleTest : KoinTest {
// lazy inject BusinessService into property
val service : BusinessService by inject()
@Test
fun myTest() {
// You can start your Koin configuration
startKoin(myModules)
// or directly get any instance
val service : BusinessService = get()
// Don't forget to close it at the end
closeKoin()
}
}
And more: check your configuration with a simple unit test, easily create mocks...
Articles
Articles & resouces about Koin
- A brief look at Koin on Android
- Bye bye Dagger
- Testing with Koin
- Painless Android testing with Room & Koin
- Unlock your Android ViewModel power with Koin
- Using dependency injection with Koin
- Koin + Spark = ❤️
- Push SparkJava to the next level (Kotlin Weekly issue 73, DZone.com )
- When Koin met Ktor ... (Kotlin Weekly issue 72)
- Android Dependency Injection – Why we moved from Dagger 2 to Koin?
- Moving from Dagger to Koin - Simplify your Android development - (Kotlin Weekly issue 66 & Android Weekly issue 282)
- Kotlin Weekly #64
- Insert Koin for dependency injection
- Better dependency injection for Android
Talks & podcasts
Koin developers hub
- Koin 1.0.0 Unleashed
- Opening Koin 1.0.0 Beta
- On the road to Koin 1.0
- Koin 0.9.2 — Maintenance fixes, new branding, roadmap for 1.0.0 & some other nice announces
- Koin 0.9.1 - Bug fixes & Improvments
- Koin 0.9.0 - Getting close to stable
- Unlock your Android ViewModel power with Koin
- koin 0.8.2 Improvements bugfixes and crash fix
- Koin release 0.8.0
Contributors
This project exists thanks to all the people who contribute. [Contribute].
Backers
Thank you to all our backers! 🙏 [Become a backer]
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]