Bumble Lachlan McKee Jun 3, 2021

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

Critical Insight

A factory-based navigation pattern solves Compose's centralization problem by distributing route definitions across feature modules using Dagger Hilt's automatic discovery.

The article includes working code examples and diagrams showing exactly how the factory pattern compares to Google's approach—plus a custom annotation processor that mirrors hiltViewModel().

About This Article

Problem

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.

Solution

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.

Impact

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.