Skip to content

Instantly share code, notes, and snippets.

@sagarnayak
Last active July 29, 2019 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagarnayak/a35268aeed11d612656901162fbdaddd to your computer and use it in GitHub Desktop.
Save sagarnayak/a35268aeed11d612656901162fbdaddd to your computer and use it in GitHub Desktop.
guide to use kodein in android

Using KODEIN in Android with kotlin

Adding dependencies

    implementation 'org.kodein.di:kodein-di-generic-jvm:6.3.3'
    implementation 'org.kodein.di:kodein-di-framework-android-x:6.3.3'

Declaring Builders

to create a dependency it is required to provide the builder method for that so that Kodein can create it. in this case lets say we want a Repository with a application context.

class Repository(var application: Application) {
}

Binding

the created builders are required to bind to kodein instance in application class.

binding can be done mainly in two ways -

  1. with singleton - here only single instance for that dependency will be created.
  2. with provides - here new instances will be created as we required the depdndency over the application.

there anr many other types of binding on kodein which you can have look at the official docs.

class ApplicationClass : Application(), KodeinAware {

    private val repositoryModule = Kodein.Module(
        "repositoryModule",
        allowSilentOverride = true
    ) {
        import(androidXModule(this@ApplicationClass))
        bind<Repository>() with singleton { Repository(instance()) }
    }

    override val kodein: Kodein = Kodein.lazy {
        import(repositoryModule)
        bind() from provider { SplashViewModelProvider(instance()) }
    }
}

this is done in application class so that the kodein instance is accessible all over the application. also do not forget to all this class to menifest as application class.

Getting the dependencies

there are again two ways to get the depdndencies where required.

  1. injection
  2. retrival
  • injection gives a easy and non kodein dependent way to inject the required depdedencies. but have less features than the retrival method.
  • retrival gives us more features but it has the kodein code dependency and you need to get the kodein object inorder to get the required depdedencies.

Injection

lets say this is the class that require instanciation -

class Presenter(private val db: Database, private val rnd: Random) {
}

so we have to create its object like this -

val presenter by kodein.newInstance { Presenter(instance(), instance()) }

Retrival

Here we need the kodein instance in the class from the application class to create the dependencies -

class Presenter(val kodein: Kodein) {
    private val db: Database by kodein.instance()
    private val rnd: Random by kodein.instance()
}

we can implement the kodeinaware interface to get the kodein instance lazily injected -

class Presenter(): KodeinAware {
        override val kodein by lazy { (application as ApplicationClass).kodein }
        //or you can do - override val kodein by org.kodein.di.android.kodein()
    private val db: Database by instance()
    private val rnd: Random by instance()
}

official Docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment