同步操作将从 Gitee 极速下载/koin 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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
Check the getting started section from our website, to discover Koin with the favorite platform.
Any question about Koin usage?
Found a bug or a problem on a specific feature? Open an issue on Github issues
koin_version = '1.0.2'
Check that you have the jcenter
repository.
// Add Jcenter to your repositories if needed
repositories {
jcenter()
}
Choose your Koin dependency:
// 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"
// 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 (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"
// Koin for Ktor Kotlin
implementation "org.koin:koin-ktor:$koin_version"
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() }
}
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))
}
}
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()
}
}
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.
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()
}
Use koin from a simple JUnit class:
// Just tag your class with KoinTest to unlock 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...
This project exists thanks to all the people who contribute. [Contribute].
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。