Dependency Injection with Kodein & MVVM Architecture – Android Kotlin Tutorial

Not a single software developer ever said that a more loosely coupled code gives him headaches. On the contrary, tight coupling can and will create a bunch of things that are hard to deal with.

But first, what is that “loose coupling”? Loose coupling means not instantiating dependencies of a class directly but getting them from a centralized place. Dependencies are simply classes which are needed by other classes. For example, a view model needs a repository, so the repository is a dependency of the view model.

Loose coupling also means that your classes operate with their dependencies through an interface and not directly with their concrete implementation. This allows you to swap implementations really easily. For example, a production implementation of a database will store data on a remote server, while a test implementation will store data only locally.

In this tutorial you are going to learn how to put what’s written above into practice by using a dependency injection framework called Kodein. It is specifically designed to work well with Kotlin and it’s also compatible with Java.

?Get the code from this tutorial?


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.


5 months ago

I have learned a lot from your tutorials and have slowly gotten more comfortable with MVVM and Kotlin on Android (coming from Xamarin world and finally favoring native dev instead).

Please can you make this sample include validation because that’s something I’m struggling with.
By validation I mean, the button should be disabled unless both input texts are filled up. Thanks. Or at least can you make a little tutorial like that as I am not sure how to handle validation with 2 or more edit texts


1 month ago

Did you give up on Xamarin? I’m asking myself whether I should program natively or using Xamarin.

Matej Rešetár

1 month ago

I’d suggest to check out Flutter.


1 month ago

I actually will not. Flutter has a big issue that I don’t see being fixed soon: Instance state not saved when app is killed by OS( I think for most reasonable apps, this is a big deal. My advice is to either go the React Native route as they plan to rewrite their entire architecture soon and they say it will be a seamless transition or just go the native route


5 months ago

I craete same app exactly with same code but i got this error in QuoteActivity on line code
val viewModel= ViewModelProviders.of(this,factory).get(

the error : Caused by: java.lang.ClassCastException: cannot be cast to org.kodein.di.KodeinAware

Development made simple

We're here to help you. If you have any questions, let us know.

Copyright 2017 - 2018 Reso Coder © All Rights Reserved