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 to your Android app:
compile 'org.koin:koin-android:0.3.1'
First of all, you need to write a module. A module gathers your components definitions and allows 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. In Android components (Activity, Fragment ...) you can use by inject()
to inject your dependencies.
Check the latest changes: what's new
To start Koin and your modules, you just have to build it in your 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 build your koinContext with the newKoinContext
function huilder. This will able you to use the Koin Android extensions in your Android Application.
The newKoinContext
function build a KoinContext for your ApplicationContext
class and list of AndroidModule
(allmMdules()
is just a function returning a list of AndroidModule
).
First of all, write a module class (extends AndroidModule), overrides the context()
function by using the declareContext
function, to declare a context like below:
class MyModule : AndroidModule() {
override fun context() =
declareContext {
provide { ServiceA(get()) }
provide { ServiceB() }
provide { ServiceC(get(), get()) }
}
}
//for classes
class ServiceA(val serviceB: ServiceB)
class ServiceB()
class ServiceC(val serviceA: ServiceA, val serviceB: ServiceB)
To describe your module, you can use the following Koin DSL keywords:
provide { /* component definition */ }
declares a component for your Module - You provide a function to instanciate your componentbind {/* compatible type */}
bind a compatible type for provided definition (use it behind provide{} expression)get()
resolve a component dependencyscope {/* scope class */}
use the given scope for current module's definitions
NB: Koin is simple: All your components are singletons. You have to use scopes to handle your components lifecycle and release them when needed.
Once your app is configured, you have to ways of handling injection in your application:
- In Android components (Activity,Fragment...): use the
by inject()
lazy operator - in any Kotlin component: injection is made by constructor
Below a quick sample of injection with by inject()
in an Activity:
class MainActivity : AppCompatActivity() {
// inject my WeatherService
val weatherService by inject<WeatherService>()
// ...
}
The koin-sample-app application offers a complete application sample, with MVP Android style.
The weather app wiki page describes all about Koin features used.
A global wiki documentation page gather all features and references about 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.