Scalable Jetpack Compose Navigation
Article Summary
Lachlan McKee from Bumble Tech tackles a critical scalability problem in Jetpack Compose Navigation that Google's docs don't address. His solution? Delegate route creation to feature modules using Dagger Hilt factories.
When building a single-activity Compose app, the standard NavHost approach forces one location to know how to instantiate every screen—creating a potential god-class nightmare. McKee developed a factory pattern that keeps navigation logic decentralized and feature modules truly independent.
Key Takeaways
- Standard NavHost requires defining all routes in one place, creating maintenance bottlenecks
- Factory pattern with Dagger Hilt lets each feature module own its navigation logic
- Published open-source library reduces boilerplate with HiltComposeNavigationFactory annotation
- Enables feature modules to use Kotlin internal keyword for better encapsulation
A factory-based navigation pattern solves Compose's centralization problem by distributing route definitions across feature modules using Dagger Hilt's automatic discovery.
About This Article
Lachlan McKee found that Google's Jetpack Compose Navigation documentation doesn't explain how to handle applications with hundreds of Composable routes. Without this guidance, the NavHost can become a god-class that has to instantiate every screen.
McKee built the Hilt Compose Navigation Factory library. It uses Dagger Hilt's automatic module discovery and annotation processing to generate boilerplate code. The approach mirrors how Google's hiltViewModel() function works, letting developers define routes in a decentralized way.
Feature modules can now use Kotlin's internal keyword for better encapsulation. Developers no longer need to manually construct Dagger modules. This cuts down on boilerplate code and makes modular Android applications easier to maintain.