KOIN is a simple (but powerful) dependency injection framework for Android. It uses Kotlin and its functional power to get things done! No proxy/CGLib, no code generation, no introspection. Just functional Kotlin and DSL magic ;)
KOIN is a very small library, that aims to be as simple as possible and let's you write dependency injection in a breath.
Just describe your stuff and inject it!
Check the latest changes in What's New and the Roadmap for next releases.
For users using a version prior to Koin 0.4.x, please refer the migrating to 0.4.0 page to understand the latest changes.
Check that you have the jcenter repository. Add the following gradle dependency to your Android app:
compile 'org.koin:koin-android:0.4.1'To start KOIN and your modules, you just have to implement it in your Android Application class like below:
class MainApplication : Application(), KoinContextAware {
// Your Koin Context here
override val koinContext = newKoinContext(this, allModules())
override fun onCreate() {
super.onCreate()
// Your Koin context is ready ! :)
}
}Implement KoinContextAware interface, and override your koinContext property with the newKoinContext() function builder.
The newKoinContext function builder requires a list of modules to run. A function can manage this for you, check out the allModules() function.
KOIN requires you to declare your components in modules.
A module class extends your AndroidModule class and implements the context() function by using the applicationContext function builder, to declare a context like below:
class WeatherModule : AndroidModule() {
override fun context() = applicationContext {
// WeatherActivity context
context(name = "WeatherActivity") {
// Provide a factory for presenter WeatherContract.Presenter
provide { WeatherPresenter(get()) }
}
// Weather data repository
provide { WeatherRepository(get()) }
// Local Weather DataSource
provide { LocalDataSource(AndroidReader(applicationContext) } bind WeatherDatasource::class
}
}
//for classes
class WeatherPresenter(val weatherRepository: WeatherRepository)
class WeatherRepository(val weatherDatasource: WeatherDatasource)
class LocalDataSource(val jsonReader: JsonReader) : WeatherDatasourceYour module then provides an applicationContext (description of your components), which will be made of provided components and subcontexts.
You can refer to the KOIN DSL for more details.
Once your app is configured, you have 2 ways of handling injection in your application:
- In Android components (Activity, Fragment etc.): use the
by inject()lazy operator - In any Kotlin component: injection is made by constructor
// In Android class, use the by inject() operator
class WeatherActivity() : AppCompatActivity() {
// inject my Presenter
val presenter by inject<WeatherPresenter>()
// you can use your injected dependencies anywhere
}// In pure Kotlin class, All is injected in constructor
class WeatherPresenter(val weatherRepository: WeatherRepository) {
// you can use your dependencies here
}KOIN is an internal DSL: all your modules evolves directly with your code (if you change a component, it will also impact your modules).
You can check your modules with KoinContext.dryRun() (launch all your modules and try to inject each component). Better is to place it in your tests folder and check it regulary - ensure everything is injected correctly.
in a JUnit test file:
@Test
fun dryRun(){
val koinContext = Koin().build(allModules()).dryRun()
// or if you need Application context in your injection
val koinContext = Koin().init(mock(Application::class.java)).build(allModules()).dryRun()
}The koin-sample-app application offers a complete application sample, with MVP Android style.
The weather app wiki page describes all the KOIN features used.
A global wiki documentation page gather all features and references about the KOIN Framework.
Check the kotlin slack community and join #koin channel
Don't hesitate to open an issue to discuss about your needs or if you don't a feature for example.