开始使用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需开始使用 Navigation 3,请将该库以及所有支持库添加到您的项目中。请参考下表确定要添加哪些库。
工件
名称 |
用途 |
工件 |
Navigation 3 运行时库 |
Core Navigation 3 API。包括 NavEntry, EntryProvider 和关联的 DSL。 |
androidx.navigation3:navigation3-runtime |
Navigation 3 界面库 |
提供用于显示内容的类,包括 NavDisplay 和 Scene 。 |
androidx.navigation3:navigation3-ui |
Navigation 3 的 ViewModel 生命周期 |
允许将 ViewModel 的范围限定为返回堆栈中的条目。 |
androidx.lifecycle:lifecycle-viewmodel-navigation3 |
适用于 Navigation 3 的 Material 3 自适应布局 |
提供自适应布局(SceneStrategies、Scene 和元数据定义),以便与 NavDisplay 搭配使用。 |
androidx.compose.material3.adaptive:adaptive-navigation3 快照 build 中提供 |
KotlinX 序列化 |
允许对导航键进行序列化。 |
插件:org.jetbrains.kotlin.plugin.serialization 库: org.jetbrains.kotlinx:kotlinx-serialization-core |
项目设置
如需将 Navigation 3 库添加到现有项目,请将以下代码添加到 libs.versions.toml
:
[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"}
此外,请将编译 SDK 更新到 36 或更高版本:
[versions]
compileSdk = "36"
将以下内容添加到您的应用 build 文件 app/build.gradle.kts
:
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)
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Get started\n\nTo get started with Navigation 3, add the library to your project along with any\nsupporting libraries. Use the table below to decide which libraries to add.\n\nArtifacts\n---------\n\n| **Name** | **What it does** | **Artifact** |\n|-------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|\n| Navigation 3 runtime library | Core Navigation 3 API. Includes `NavEntry, EntryProvider` and the associated DSL. | androidx.navigation3:navigation3-runtime |\n| Navigation 3 UI library | Provides classes to display content, including `NavDisplay` and `Scene`. | androidx.navigation3:navigation3-ui |\n| ViewModel Lifecycle for Navigation 3 | Allows ViewModels to be scoped to entries in the back stack. | androidx.lifecycle:lifecycle-viewmodel-navigation3 |\n| Material 3 adaptive layouts for Navigation 3 | Provides adaptive layouts (SceneStrategies, Scenes and metadata definitions) for use with NavDisplay. | androidx.compose.material3.adaptive:adaptive-navigation3 Available in [snapshot builds](https://androidx.dev/) |\n| [KotlinX Serialization](https://kotlinlang.org/docs/serialization.html) | Allows navigation keys to be serialized. | Plugin: org.jetbrains.kotlin.plugin.serialization Library: org.jetbrains.kotlinx:kotlinx-serialization-core |\n\nProject setup\n-------------\n\nTo add the Navigation 3 library to your existing project, add the following to\nyour `libs.versions.toml`: \n\n [versions]\n nav3Core = \"1.0.0-alpha01\"\n lifecycleViewmodelNav3 = \"1.0.0-alpha01\"\n kotlinSerialization = \"2.1.21\"\n kotlinxSerializationCore = \"1.8.1\"\n material3AdaptiveNav3 = \"1.0.0-SNAPSHOT\"\n\n [libraries]\n # Core Navigation 3 libraries\n androidx-navigation3-runtime = { module = \"androidx.navigation3:navigation3-runtime\", version.ref = \"nav3Core\" }\n androidx-navigation3-ui = { module = \"androidx.navigation3:navigation3-ui\", version.ref = \"nav3Core\" }\n\n # Optional add-on libraries\n androidx-lifecycle-viewmodel-navigation3 = { module = \"androidx.lifecycle:lifecycle-viewmodel-navigation3\", version.ref = \"lifecycleViewmodelNav3\" }\n kotlinx-serialization-core = { module = \"org.jetbrains.kotlinx:kotlinx-serialization-core\", version.ref = \"kotlinxSerializationCore\" }\n\n # Note: The Material3 adaptive layouts library for Navigation 3 is currently\n # only available in snapshot builds. Follow the instructions at androidx.dev to\n # add the snapshot builds repository to your project.\n androidx-material3-adaptive-navigation3 = { group = \"androidx.compose.material3.adaptive\", name = \"adaptive-navigation3\", version.ref = \"material3AdaptiveNav3\" }\n\n [plugins]\n # Optional plugins\n jetbrains-kotlin-serialization = { id = \"org.jetbrains.kotlin.plugin.serialization\", version.ref = \"kotlinSerialization\"}\n\nAlso, update your compile SDK to 36 or above: \n\n [versions]\n compileSdk = \"36\"\n\nAdd the following to your **app** build file `app/build.gradle.kts`: \n\n plugins {\n ...\n // Optional, provides the @Serialize annotation for autogeneration of Serializers.\n alias(libs.plugins.jetbrains.kotlin.serialization)\n }\n\n dependencies {\n ...\n implementation(libs.androidx.navigation3.ui)\n implementation(libs.androidx.navigation3.runtime)\n implementation(libs.androidx.lifecycle.viewmodel.navigation3)\n implementation(libs.androidx.material3.adaptive.navigation3)\n implementation(libs.kotlinx.serialization.core)\n }"]]