If you’re new to DI, whether it’s Hilt or Koin - sooner or later, you’ll encounter a circular dependency (I hope you don’t, but let’s assume you will 😢).
This post is an explanation of a challenge I posted two days ago 
So what is a circular dependency? 🤔
According to Wikipedia:
A circular dependency (or cyclic dependency) is a relation between two or more modules / classes which either directly or indirectly depend on each other to function properly
🔄 Essentially, it’s an infinite loop, because when one class requires another class at creation time and vice versa, you have a never-ending cycle.
How do you know you’ve run into a circular dependency? You will probably get a StackOverflowException because of the never-ending invocation of the class constructor. 💥
How can you resolve or detect it? The best way is to not have one in the first place. And Clean Architecture helps a lot here. See the carousel below for an explanation.
Are there ways to detect circular dependencies if you already have one? Yes, but it’s rather painful (and I might cover that later) - so it’s best to avoid them altogether.


Member discussion: