Hilt's built-in dependency scopes don't support scoping dependencies to a single navigation entry, forcing developers to use Singleton repositories with manual data-flow separation to avoid leaking data across screens in the back stack. This post explains how Mercari's Android team created a custom Hilt component called NavEntryComponent, bound to the navigation entry lifecycle via a ViewModel-backed owner, and built a bridge module so ViewModels can access NavEntryScoped dependencies through SavedStateHandle and a component store. The result is a library (open-sourced on GitHub) with an annotation processor that eliminates boilerplate, enabling clean shared-dependency scoping per screen without data leakage or manual cleanup.

10m read timeFrom engineering.mercari.com
Post cover image
Table of contents
Many ViewModels on the same screenWhy a Singleton Repository?Create the Custom Hilt ComponentAccess NavEntryComponent dependenciesWhat to change in your screen codeDo you need NavEntryScope?Wrapping up
241 Impressions