Why we need yet another dependency injection framework for Kotlin & Android
At the beginning of this year my colleague and I started a new Android application project. Being a greenfield project we had no technical constraints. Therefore during the planning phase we sat down and made a wish list of which technologies we want to use. Obviously 100% Kotlin was a no-brainer! Eventually we came to the point of dependency injection. We have lots of experience with Dagger 2 but felt that we should give one of the new Kotlin-specific frameworks a try, utilising the full potential of Kotlin. Also we decided to use Conductor for the UI so we had no benefits of using Dagger since we couldn’t use Dagger’s Android extensions which remove a lot of the boiler plate code. We looked at Koin and Kodein, both popular libraries in the Kotlin community. However these frameworks didn’t suit us and either had too many features that we don’t need or — according to our opinion — questionable design decisions which might introduce complex problems within the Android environment. For instance Koin works with a global singleton object. Modules are added to and removed from the object during runtime. We felt that this concept might introduce memory leaks, especially when injecting instances of Android framework classes and not properly releasing the modules (in the right order). Later Koin introduced scopes but the original problem remains. We thought that there’s already a perfect tool for handling the scope of objects, which has been with the JVM from the very beginning: the garbage collector. Why should I have to explicitly bind and release scopes or modules when the garbage collector can do this job?
That’s why we decided to write our own dependency injection framework for Kotlin with Android in mind. After all a dependency injection framework is just a glorified hash map, right? 😉
After a few weeks of development and fine tuning and months of internal testing we’re happy to announce Katana, our vision of a lightweight and minimalistic dependency injection framework 🎉
Slices of Katana
Katana is inspired by Dagger, Koin and Kodein. It inherits the concept of modules and components to provide a facility for safely injecting context-specific dependencies in a leak-free manner. Instead of code generation Katana harnesses the capabilities of the Kotlin programming language like inline functions for type-safe injection. Furthermore Katana does not rely on reflection guaranteeing an optimal performance on Android. It’s purely written in Kotlin with no third-party dependencies and adds only a very light footprint of about 60kb and 113 methods (after ProGuard) to your application.
We believe that Katana fills a niche in the realm of dependency injection frameworks suitable for Android, broadening the choice for developers. If you are looking for a lean framework which is as good as handwritten dependency injection maybe Katana might be right for you.
Please have a look at Katana, try it out, spread the word, report bugs and feature requests and let us know when you’re using Katana in your application. Many thanks! 🙂