
Enables function-like navigation for screens within applications, using a concept of navigation keys as contracts for screen transitions, with support for fragments, activities, and composables.
Note
The CHANGELOG lists the changes in each Enro release.
Enro is a navigation library for Kotlin Multiplatform. In Enro, a screen is declared like a function.
A NavigationKey is the signature of a screen. The NavigationKey declares the
screen's inputs and, optionally, the type of the screen's result. Code that opens
a screen depends on the NavigationKey, not on the screen's implementation.
@Serializable
data class ShowProfile(val userId: String) : NavigationKey
@Serializable
data class SelectDate(
val minDate: LocalDate? = null,
val maxDate: LocalDate? = null,
) : NavigationKey.WithResult<LocalDate>The same two contracts, written as function signatures:
fun showProfile(userId: String): Unit
fun selectDate(minDate: LocalDate? = null, maxDate: LocalDate? = null): LocalDateEnro targets Android, iOS, Desktop and Web through Compose Multiplatform.
On Android, a Fragment or an Activity can be a destination. The enro-compat
module provides the Enro 2 API for apps that are migrating to Enro 3.
Enro is published to Maven Central. Add the
mavenCentral() repository and these dependencies:
dependencies {
implementation("dev.enro:enro:3.0.0")
ksp("dev.enro:enro-processor:3.0.0")
testImplementation("dev.enro:enro-test:3.0.0")
}Declare a NavigationKey for each screen, bind a destination to each key with
@NavigationDestination, and open a key with navigation.open(key).
@Serializable
data object Home : NavigationKey
@Serializable
data class Profile(val userId: String) : NavigationKey
@Composable
@NavigationDestination(Home::class)
fun HomeScreen() {
val navigation = navigationHandle<Home>()
Button(onClick = { navigation.open(Profile("user-123")) }) {
Text("View profile")
}
}
@Composable
@NavigationDestination(Profile::class)
fun ProfileScreen() {
val navigation = navigationHandle<Profile>()
Column {
Text("Profile for ${navigation.key.userId}")
Button(onClick = { navigation.close() }) { Text("Back") }
}
}Declare one NavigationComponent and install the NavigationComponent when the
application starts:
@NavigationComponent
object MyComponent : NavigationComponentConfiguration(
module = createNavigationModule { }
)
// Android
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
MyComponent.installNavigationController(this)
}
}Create a navigation container in a Composable and render the container with
NavigationDisplay:
val container = rememberNavigationContainer(
backstack = backstackOf(Home.asInstance()),
)
NavigationDisplay(state = container)The documentation at enro.dev covers each of these steps.
recipes/ contains runnable examples. Each recipe shows one concept. enro.dev/docs/recipes.html lists the recipes. Run the recipes app with ./gradlew :recipes:app:desktop:run.tests/application contains three larger samples. Run the test application with ./gradlew :tests:application:app:desktop:run, then choose "Samples"../gradlew dokkaGenerate writes the API reference to build/dokka/html/index.html. Each artifact on Maven Central also has a -javadoc.jar.Enro 3 is a rewrite of Enro 2 for Kotlin Multiplatform and Compose. The migration guide at enro.dev/docs/migrating-from-v2.html lists every API change. The main changes:
NavigationKey.SupportsPush / SupportsPresent → NavigationKey, or NavigationKey.WithResult<T> for a screen with a result@Parcelize → @Serializable (kotlinx.serialization)push() / present() → open(key); destination metadata declares dialog and overlay behaviourcloseWithResult(r) → complete(r)
NavigationApplication is removed; call installNavigationController from Application.onCreate
enro-compat module provides the Enro 2 API during migration"The novices' eyes followed the wriggling path up from the well as it swept a great meandering arc around the hillside. Its stones were green with moss and beset with weeds. Where the path disappeared through the gate they noticed that it joined a second track of bare earth, where the grass appeared to have been trampled so often that it ceased to grow. The dusty track ran straight from the gate to the well, marred only by a fresh set of sandal-prints that went down, and then up, and ended at the feet of the young monk who had fetched their water." — The Garden Path
Note
The CHANGELOG lists the changes in each Enro release.
Enro is a navigation library for Kotlin Multiplatform. In Enro, a screen is declared like a function.
A NavigationKey is the signature of a screen. The NavigationKey declares the
screen's inputs and, optionally, the type of the screen's result. Code that opens
a screen depends on the NavigationKey, not on the screen's implementation.
@Serializable
data class ShowProfile(val userId: String) : NavigationKey
@Serializable
data class SelectDate(
val minDate: LocalDate? = null,
val maxDate: LocalDate? = null,
) : NavigationKey.WithResult<LocalDate>The same two contracts, written as function signatures:
fun showProfile(userId: String): Unit
fun selectDate(minDate: LocalDate? = null, maxDate: LocalDate? = null): LocalDateEnro targets Android, iOS, Desktop and Web through Compose Multiplatform.
On Android, a Fragment or an Activity can be a destination. The enro-compat
module provides the Enro 2 API for apps that are migrating to Enro 3.
Enro is published to Maven Central. Add the
mavenCentral() repository and these dependencies:
dependencies {
implementation("dev.enro:enro:3.0.0")
ksp("dev.enro:enro-processor:3.0.0")
testImplementation("dev.enro:enro-test:3.0.0")
}Declare a NavigationKey for each screen, bind a destination to each key with
@NavigationDestination, and open a key with navigation.open(key).
@Serializable
data object Home : NavigationKey
@Serializable
data class Profile(val userId: String) : NavigationKey
@Composable
@NavigationDestination(Home::class)
fun HomeScreen() {
val navigation = navigationHandle<Home>()
Button(onClick = { navigation.open(Profile("user-123")) }) {
Text("View profile")
}
}
@Composable
@NavigationDestination(Profile::class)
fun ProfileScreen() {
val navigation = navigationHandle<Profile>()
Column {
Text("Profile for ${navigation.key.userId}")
Button(onClick = { navigation.close() }) { Text("Back") }
}
}Declare one NavigationComponent and install the NavigationComponent when the
application starts:
@NavigationComponent
object MyComponent : NavigationComponentConfiguration(
module = createNavigationModule { }
)
// Android
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
MyComponent.installNavigationController(this)
}
}Create a navigation container in a Composable and render the container with
NavigationDisplay:
val container = rememberNavigationContainer(
backstack = backstackOf(Home.asInstance()),
)
NavigationDisplay(state = container)The documentation at enro.dev covers each of these steps.
recipes/ contains runnable examples. Each recipe shows one concept. enro.dev/docs/recipes.html lists the recipes. Run the recipes app with ./gradlew :recipes:app:desktop:run.tests/application contains three larger samples. Run the test application with ./gradlew :tests:application:app:desktop:run, then choose "Samples"../gradlew dokkaGenerate writes the API reference to build/dokka/html/index.html. Each artifact on Maven Central also has a -javadoc.jar.Enro 3 is a rewrite of Enro 2 for Kotlin Multiplatform and Compose. The migration guide at enro.dev/docs/migrating-from-v2.html lists every API change. The main changes:
NavigationKey.SupportsPush / SupportsPresent → NavigationKey, or NavigationKey.WithResult<T> for a screen with a result@Parcelize → @Serializable (kotlinx.serialization)push() / present() → open(key); destination metadata declares dialog and overlay behaviourcloseWithResult(r) → complete(r)
NavigationApplication is removed; call installNavigationController from Application.onCreate
enro-compat module provides the Enro 2 API during migration"The novices' eyes followed the wriggling path up from the well as it swept a great meandering arc around the hillside. Its stones were green with moss and beset with weeds. Where the path disappeared through the gate they noticed that it joined a second track of bare earth, where the grass appeared to have been trampled so often that it ceased to grow. The dusty track ran straight from the gate to the well, marred only by a fresh set of sandal-prints that went down, and then up, and ended at the feet of the young monk who had fetched their water." — The Garden Path