Android项目配置NavigationComponent SafeArgs遇错,求正确配置方案
问题背景
使用环境:
- Android Gradle Plugin 版本:
7.4.1 - Gradle 版本:
7.5 - JDK 版本:
11
在settings.gradle中添加buildscript块引入SafeArgs插件后,出现以下错误:
org.gradle.api.GradleException: Error resolving plugin [id: 'com.android.application', version: '7.4.1', apply: false] Caused by: org.gradle.plugin.management.internal.InvalidPluginRequestException: The request for this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so compatibility cannot be checked.
错误原因
Gradle 7+版本推荐使用Plugins DSL管理插件,在settings.gradle中混用buildscript块会导致Android Gradle Plugin被重复加载,触发版本冲突检测失败。
正确配置步骤
1. 修正settings.gradle
移除文件中的buildscript块,保留插件仓库和依赖仓库配置即可:
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "MyApp" include ':app'
2. 修正项目级build.gradle(Project:MyApp)
通过Plugins DSL声明SafeArgs插件的版本(与Navigation组件版本保持一致):
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.4.1' apply false id 'com.android.library' version '7.4.1' apply false // 统一SafeArgs版本与Navigation组件版本 id 'androidx.navigation.safeargs' version '2.6.0' apply false }
3. 修正模块级build.gradle(Module:App)
- 正确应用SafeArgs插件(移除
apply false) - 移除错误的
navigation-safe-args-gradle-plugin依赖(这是插件而非运行时依赖) - 统一所有Navigation组件的版本为
2.6.0 - 移除手动添加的
sourceSets配置(插件会自动生成并识别参数类目录)
修改后的文件内容:
plugins { id 'com.android.application' // 应用SafeArgs插件 id 'androidx.navigation.safeargs' } android { namespace 'com.gamenight.myapp' compileSdk 33 buildFeatures { dataBinding true viewBinding true } defaultConfig { applicationId "com.gamenight.nite" minSdk 24 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } } dependencies { // Lifecycle implementation 'androidx.lifecycle:lifecycle-viewmodel:2.6.1' implementation 'androidx.lifecycle:lifecycle-runtime:2.6.1' // ViewModel SavedState implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.6.1' implementation "androidx.appcompat:appcompat:1.6.1" implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1' // Room implementation 'androidx.room:room-runtime:2.5.2' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1' annotationProcessor 'androidx.room:room-compiler:2.5.2' implementation 'com.google.android.material:material:1.9.0' // Fragment Navigation(统一版本为2.6.0) implementation "androidx.navigation:navigation-fragment:2.6.0" implementation "androidx.navigation:navigation-ui:2.6.0" // Feature module Support(统一版本) implementation "androidx.navigation:navigation-dynamic-features-fragment:2.6.0" // Testing Navigation(统一版本) androidTestImplementation "androidx.navigation:navigation-testing:2.6.0" // Jetpack Compose Integration(统一版本) implementation "androidx.navigation:navigation-compose:2.6.0" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
4. 同步项目并验证
执行Gradle同步操作,之后在导航图中配置参数后,插件会自动生成对应的类型安全参数类,可直接在代码中调用。
内容的提问来源于stack exchange,提问作者Muhammed Yalçın
相关产品推荐
相关产品推荐

