Android Architecture Design with Koin by Arnaud Giuliani

March 16, 2022

In the Android Architecture Design with Koin presentation by Arnaud Giuliani, he discusses the use of Koin, a dependency injection framework for Android architecture. Koin is a lightweight library that allows developers to configure dependencies directly in the Kotlin language using a DSL. The latest version of Koin is 3.1.x, and the 3.2.0 beta is available. Arnaud emphasizes the importance of keeping the Koin configuration simple for fewer bugs and demonstrates how to use the Koin DSL for dependency injection in Android development. He provides real-application samples and discusses the use of Koin in managing components of the architecture, including repositories, ViewModel, and Presenter, as well as the introduction of use cases or interactor components. Arnaud also discusses the concept of scopes in Koin and how to use it in the context of Android navigation and Jetpack Compose. Additionally, the presentation covers the use of Koin in Kotlin Multi-Platform projects and new features in Koin 3.2, such as the use of factory off and constructor classes, includes for modules, and the opening of Koin to annotations.

Android Architecture Design with Koin: A comprehensive overview

Introducing Koin

Arnaud Giuliani kicks off by introducing Koin as a Kotlin-first dependency injection framework tailored for Android. With Koin, developers can manage dependencies using a Domain-Specific Language (DSL) directly in Kotlin, simplifying configuration without relying on XML or complicated setups. He shares that the latest version at the time, 3.1.x, is stable, while version 3.2.0 is in beta for developers eager to experiment with newer features.

 

Simplifying dependency injection with Koin DSL

A major highlight of Koin is its easy-to-use DSL, which allows developers to write cleaner, more maintainable code. Koin uses constructor injection, where dependencies are resolved at runtime. Arnaud emphasizes how Koin’s simplicity reduces bugs, showing examples from a real-world application — a simple to-do app with components such as activities, fragments, view models, and repositories — all configured through Koin.

He demonstrates the effortless configuration of components like data sources and repositories in just a few lines of code, with the Koin container managing the retrieval and injection of these dependencies. Whether it’s managing UI components, services, or database access, Koin allows seamless integration, helping developers avoid boilerplate code and improving productivity.

 

 ViewModel and Presenter management with Koin

Koin’s integration with Android’s lifecycle is another highlight. Arnaud demonstrates how to manage ViewModel and Presenter components using Koin, emphasizing how Koin handles lifecycle-aware components. When a ViewModel is instantiated via Koin, it is automatically hooked into the Android lifecycle, ensuring that components like Presenters, which follow the MVP architecture, are managed efficiently without manual lifecycle handling.

By using keywords like viewModel in Koin’s DSL, developers can easily create and inject these components. Koin also simplifies the process of passing arguments from the UI layer to components, allowing smooth interaction between layers in the architecture.

 

Enhancing architecture with scopes and modules

Scopes play a crucial role in managing object lifecycles. Arnaud explains how to use Koin’s scope API to maintain instances across the Android lifecycle. For example, by using scopes, developers can ensure that instances within an activity or fragment are retained properly, avoiding memory leaks. He emphasizes the need to choose between activity, fragment, or ViewModel scopes, depending on the use case, to ensure a proper lifecycle configuration.

He further illustrates how modular design in Koin makes testing and maintenance easier. By organizing dependencies around modules and layers, components become easier to test in isolation, and architecture becomes more maintainable.

 

Navigating complex architecture with Koin

As applications grow in complexity, Koin helps manage dependencies across multiple layers. Arnaud introduces the concept of integrating use cases or interactors between ViewModels and repositories. This helps developers decouple business logic from the UI, making the architecture more modular. Koin’s flexibility allows developers to define singletons, factories, and inject dependencies across modules, promoting better organization.

Arnaud also covers Android’s scope management, explaining how Koin can help persist instances during navigation in apps or handle configurations in Jetpack Compose. Additionally, he discusses Koin’s support for Kotlin Multi-Platform (KMM) projects, where developers can write shared logic for Android and iOS while managing dependencies through Koin.

 

Koin’s latest features and future direction

With version 3.2.x in the works, Arnaud introduces several upcoming features, including constructor classes for better performance and annotations to register dependencies without the DSL. He explains that Koin is moving toward using the Google KSP project for fast compilation times, offering greater flexibility while maintaining the framework’s Kotlin-first approach.

He also highlights improvements in modularization and the performance of dependency management by flattening modules, ensuring that components are loaded only once. These improvements aim to streamline dependency management for larger applications and enterprise use.

 

The journey of Koin

Arnaud closes the presentation by sharing Koin’s origin story. What started as a simple DSL in 2016 grew into a globally adopted framework, used by companies in industries ranging from banking to video streaming. Koin’s rise underscores its utility as a lightweight, developer-friendly solution for dependency injection, designed specifically for Kotlin.

Looking ahead, the Koin team plans to focus on providing long-term support, ensuring that as new versions of Koin are released, older versions are still supported for enterprise use. Codila, a company that provides long-term support for Koin, is actively involved in managing this.

 

Conclusion

Arnaud Giuliani’s presentation underscores Koin’s ability to simplify Android architecture design. From handling dependency injection seamlessly using Kotlin’s DSL to offering lifecycle-aware components and modular architecture, Koin continues to be a powerful tool for Android developers. Its ability to manage complex architectures, coupled with upcoming improvements like faster compilation through KSP, makes it a forward-thinking framework well-suited for modern Android development.

For developers looking to streamline their dependency injection and architecture design processes, Koin is definitely worth exploring. Whether you’re working on a small project or a large-scale application, Koin provides the flexibility and simplicity needed to manage dependencies efficiently.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.