KOIN is a dependency injection framework that uses Kotlin and its functional power to get things done! No proxy/CGLib, No code generation, No introspection. Just functional Kotlin and DSL magic ;)
Check that you have jcenter
repository. Add the following gradle dependency for your Android app:
compile 'org.koin:koin-android:0.2.0'
First of all, you need to write a module. A module gather your components definitions and allow it to be loaded by Koin and injected in your application. Keep in mind, that injection by constructor is the default strategy targeted by Koin.
Write a class that extends AndroidModule, and override the context()
function and use to declareContext
function to define a context like below:
class MyModule : AndroidModule() {
override fun context() =
declareContext {
// declare dependency here ...
provide { createOkHttpClient() }
}
}
}
fun createOkHttpClient() : OkHttpClient { //create OkHttpClient ...}
Your context is provided by the context()
function, and described via the declareContext
function. This unlock the Koin DSL:
provide { /* component definition */ }
declare a component for your Modulebind {/* compatible type */}
bind a compatible type for provided definitionget()
inject a component dependencyscope {/* scope class */}
defineor reuse a scope current module's definitions
AndroidModule also gives you the possibility to retrieve Android specific resources directly in your module context (ApplicationContext, Resources & Assets). e.g: Get an Android string in your module:
resources.getString(R.string.server_url)
To start your module, you must build it:
val myContext = Koin().init(applicationInstance).build(MyModule())
This will return a KoinContext object. Koin proposes the KoinContextAware interface, to help define and bring your Koin context all over your app. Configure it like below:
class MainApplication : Application(), KoinContextAware {
/**
* Koin context
*/
lateinit var context: KoinContext
/**
* KoinContextAware - Retrieve Koin Context
*/
override fun getKoin(): KoinContext = context
override fun onCreate() {
super.onCreate()
// insert Koin !
context = Koin().init(this).build(MyModule())
// ...
}
}
By using KoinContextAware
interface, you will be able to use the Koin Android extensions in your Android Application.
Don't forget to use the init()
function to init Android context injection, else you won't be able to load your modules & extensions.
Once you have your Koin context, you can get inject your components from anywhere (Application, Activity, Fragment) by using by inject<>()
function:
class MainActivity : AppCompatActivity() {
// inject my WeatherService
val weatherService by inject<WeatherService>()
...
}
That's it !
You can find a complete sample app here: github sources
This sample demo basics concepts:
- A Module -- Module example to create okhttpClient, retrofit and web service component
- An Application -- Setup for loading module with Android application
- An Activity -- Inject
WeatherService
into MainActivity
Check the wiki for complete documentation.