Los gehts

Wenn Sie mit Navigation 3 beginnen möchten, fügen Sie Ihrem Projekt die Bibliothek und alle unterstützenden Bibliotheken hinzu. Anhand der Tabelle unten können Sie entscheiden, welche Bibliotheken Sie hinzufügen möchten.

Artefakte

Name

Funktion

Artefakt

Navigation 3-Laufzeitbibliothek

Core Navigation 3 API Enthält NavEntry, EntryProvider und das zugehörige DSL.

androidx.navigation3:navigation3-runtime

Navigation 3-UI-Bibliothek

Bietet Klassen zum Anzeigen von Inhalten, einschließlich NavDisplay und Scene.

androidx.navigation3:navigation3-ui

ViewModel-Lebenszyklus für Navigation 3

Ermöglicht es, ViewModels auf Einträge im Backstack zu beschränken.

androidx.lifecycle:lifecycle-viewmodel-navigation3

Adaptive Material 3-Layouts für Navigation 3

Bietet adaptive Layouts (Szenenstrategien, Szenen und Metadatendefinitionen) für die Verwendung mit NavDisplay.

androidx.compose.material3.adaptive:adaptive-navigation3

Verfügbar in Snapshot-Builds

KotlinX-Serialisierung

Ermöglicht die sätzung von Navigationsschaltflächen.

Plug-in: org.jetbrains.kotlin.plugin.serialization

Bibliothek:

org.jetbrains.kotlinx:kotlinx-serialization-core

Projekt einrichten

Wenn Sie der Navigation 3-Bibliothek ein vorhandenes Projekt hinzufügen möchten, fügen Sie Folgendes zu libs.versions.toml hinzu:

[versions]
nav3Core = "1.0.0-alpha01"
lifecycleViewmodelNav3 = "1.0.0-alpha01"
kotlinSerialization = "2.1.21"
kotlinxSerializationCore = "1.8.1"
material3AdaptiveNav3 = "1.0.0-SNAPSHOT"

[libraries]
# Core Navigation 3 libraries
androidx-navigation3-runtime = { module = "androidx.navigation3:navigation3-runtime", version.ref = "nav3Core" }
androidx-navigation3-ui = { module = "androidx.navigation3:navigation3-ui", version.ref = "nav3Core" }

# Optional add-on libraries
androidx-lifecycle-viewmodel-navigation3 = { module = "androidx.lifecycle:lifecycle-viewmodel-navigation3", version.ref = "lifecycleViewmodelNav3" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinxSerializationCore" }

# Note: The Material3 adaptive layouts library for Navigation 3 is currently
# only available in snapshot builds. Follow the instructions at androidx.dev to
# add the snapshot builds repository to your project.
androidx-material3-adaptive-navigation3 = { group = "androidx.compose.material3.adaptive", name = "adaptive-navigation3", version.ref = "material3AdaptiveNav3" }

[plugins]
# Optional plugins
jetbrains-kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlinSerialization"}

Aktualisieren Sie außerdem Ihr Kompilierungs-SDK auf Version 36 oder höher:

[versions]
compileSdk = "36"

Fügen Sie der Build-Datei app/build.gradle.kts Ihrer App Folgendes hinzu:

plugins {
    ...
    // Optional, provides the @Serialize annotation for autogeneration of Serializers.
    alias(libs.plugins.jetbrains.kotlin.serialization)
}

dependencies {
    ...
    implementation(libs.androidx.navigation3.ui)
    implementation(libs.androidx.navigation3.runtime)
    implementation(libs.androidx.lifecycle.viewmodel.navigation3)
    implementation(libs.androidx.material3.adaptive.navigation3)
    implementation(libs.kotlinx.serialization.core)
}