Kotlin Multiplatform: Navigation battle

Introduction

In the fast-changing world of mobile app development, Kotlin Multiplatform (KMP) has become a powerful tool for building cross-platform applications. It allows developers to write shared code for both Android and iOS, saving time and effort. However, effective navigation is crucial for any app, and this is where Compose Navigation and Voyager come in.

Sharing code which means: 

  • You can share the business logic for both platforms.
  • You can share the UI using CMP (Compose Multiplatform).

 

We all know that navigation plays a crucial role and challenges while developing any mobile application so, we will discuss here the most common libraries to handle navigation which are:

  • Compose Navigation , part of Jetpack Compose is Google’s solution for managing navigation in Compose apps. It’s popular among Android developers for its modern and easy-to-use approach.
  • Voyager , on the other hand, is a new navigation library built specifically for KMP. It aims to make navigation simple across multiple platforms.

In this article, we will compare Compose Navigation and Voyager, looking at their features, ease of use, performance, and how well they fit into KMP projects. By the end, you’ll know which navigation library is best for your needs.

 


Overview of Navigation Libraries

Compose Navigation

Compose Navigation is a library within Jetpack Compose that provides a way to navigate between different screens in a Compose application. It leverages the principles of declarative UI and offers a flexible and powerful navigation solution tailored for Compose.

Typical Use Cases in Android Development:

  • Single Activity Applications: Simplifying the navigation within single-activity architectures
  • Deep Linking: Implementing deep linking to specific app parts from notifications, web pages, or other apps.

 

Voyager

Voyager focuses on making navigation in KMP projects straightforward and platform-agnostic. It provides a unified API that works across Android, iOS, and other platforms supported by KMP, ensuring developers can write navigation logic once and use it everywhere.

Typical use cases in KMP:

  • Cross-Platform Applications: Ideal for projects targeting multiple platforms with shared navigation logic.
  • Unified Development Approach: Boost the development process by using a single navigation solution for all platforms.

 


 

Community and Ecosystem

 

Compose Navigation

 

Voyager

 

Community It is widely adopted and supported by Google Its community is smaller but growing
Ecosystem As part of Jetpack Compose, Compose Navigation integrates well with other Jetpack libraries and tools is part of the broader KMP ecosystem, which is still maturing. 
Support Strong community support through forums, GitHub, and Stack Overflow Good documentation and examples are available, but community support may not be as extensive as Compose Navigation

 


 

API and Features

 

Compose Navigation

 

Voyager

 

API

Compose Navigation provides a powerful and flexible API for managing navigation. 

It includes features such as NavController, NavGraph, and NavHost, which help in defining and controlling navigation routes.

  • NavController is a component which handles navigation and back stack management.
  • NavGraph collects all navigation-related data in one place. 
  • NavHost links the NavController with a NavGraph that specifies the composable destinations that you should be able to navigate between.
Voyager offers a simple and unified API for navigation across multiple platforms. It abstracts platform-specific details, providing a consistent navigation experience.
Code sample

Type-Safe Arguments: Ensures that data passed between screens is type-safe.

Easy and simple navigation

animation

Supports custom transition animations between screens.

Easy to add animation between the screen’s transactions.


Pros and Cons

 

Compose Navigation

 

Voyager

 

Advantages
  • Seamless Integration with Jetpack Compose: Designed to work perfectly within the Jetpack Compose ecosystem, providing a cohesive development experience.
  • Type-Safe Arguments: Reduces runtime errors by ensuring that data passed between screens is type-safe.
  • Custom Animations : Supports custom transition animations between screens, enhancing the user experience.
  • Very well-structured and easy documentation (Also tutorials and talks you can access from here https://voyager.adriel.cafe/community-projects )
  • It has an alternative for viewmodel called ( screenmodel can be integrated with Koin, Kodein, Hilt, Coroutines, RxJava, LiveData) can be used for all platforms.
  • Lightweight and Efficient: Optimized for performance, minimizing overhead in multiplatform projects.
Disadvantages
  • The navigation library is currently Experimental.
  • Deep links (handling or following them) are not supported.
  • The BackHandler function and predictive back gestures are not supported on any platform besides Android.
  • There’s no guarantee for that because it’s a 3rd party.
  • As a newer library, Voyager has a smaller community and ecosystem compared to Compose Navigation.
  • Currently, Voyager does not provide a built-in solution to handle Deeplink and URIs.
  • Might lack some advanced features and integrations available in Compose Navigation, particularly for Android-specific use cases.

 


Conclusion

Navigating the complexities of cross-platform mobile app development requires robust tools that can simplify the process and provide a seamless experience. In the context of Kotlin Multiplatform (KMP), both Compose Navigation and Voyager present compelling solutions, each with its distinct advantages and suitable use cases.

Compose Navigation stands out as a powerful tool for Android developers working within the Jetpack Compose ecosystem. Its seamless integration, declarative syntax, and rich feature set make it an excellent choice for modern Android applications. However, its primary focus on Android limits its applicability in multiplatform projects. For projects that need advanced navigation features, type-safe arguments, and deep linking capabilities on Android, Compose Navigation is the optimal choice.

Voyager, on the other hand, is tailored specifically for KMP, offering a unified navigation solution that works across multiple platforms. Its simplicity, cross-platform compatibility, and efficient state management make it ideal for projects targeting both Android and iOS. Although it has a smaller community and fewer advanced features compared to Compose Navigation, its ability to reduce code duplication and provide consistent navigation logic across platforms makes it a strong candidate for KMP projects.