导入Navigation组件Safe Args插件失败:无法找到指定版本依赖
嘿,我看你遇到了导入Navigation Safe Args插件时的依赖找不到问题,咱们来一步步排查和解决:
核心问题分析
你当前的配置存在两个关键问题:
- 依赖命名空间不匹配:你用了
androidx.navigation的safe-args插件,但核心Navigation依赖却是旧的android.arch.navigation命名空间,两者属于不同的架构组件版本体系,不能混用。 - 版本过于老旧:
1.0.0-alpha01是Navigation组件非常早期的Alpha版本,Google Maven仓库已经不再托管这类老旧的预览版本,所以同步时会找不到对应的pom和jar文件。
修改后的正确配置
1. 项目级build.gradle
更新safe-args插件的版本,并确保和后续的Navigation依赖版本一致:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' // 更换为稳定版的androidx命名空间插件,版本和下面的Navigation依赖保持一致 classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.7.0" } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
2. 模块级build.gradle
统一使用androidx命名空间的Navigation依赖,版本和插件保持一致:
apply plugin: 'com.android.application' apply plugin: 'androidx.navigation.safeargs' android { compileSdkVersion 27 defaultConfig { applicationId "com.mangoslab.navigation" minSdkVersion 21 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' def nav_version = "2.7.0" // 替换为androidx命名空间的Navigation依赖 implementation "androidx.navigation:navigation-fragment:${nav_version}" implementation "androidx.navigation:navigation-ui:${nav_version}" // 测试依赖也同步更新 androidTestImplementation "androidx.navigation:navigation-testing:${nav_version}" }
额外说明
- 如果你实在需要使用旧版本的Navigation组件(不推荐,因为存在很多已知bug),那需要将插件也改为旧的
android.arch命名空间:classpath "android.arch.navigation:safe-args-gradle-plugin:1.0.0-alpha01",但这个版本大概率已经无法从仓库下载,所以强烈建议升级到稳定版。 - 后续如果需要更新Navigation版本,记得同步更新safe-args插件的版本,保持两者版本一致,避免出现兼容性问题。
内容的提问来源于stack exchange,提问作者ibocon
相关产品推荐
相关产品推荐

