Gradle/Android Studio依赖版本异常:LifecycleOwner版本不符求助
解决SavedStateRegistryOwner依赖版本冲突问题
1. 强制锁定lifecycle-common版本
在项目根目录的build.gradle(或build.gradle.kts)中添加版本强制规则,确保所有依赖路径下的lifecycle-common都使用2.6.1版本:
Groovy 语法
configurations.all { resolutionStrategy { force 'androidx.lifecycle:lifecycle-common:2.6.1' } }
Kotlin DSL 语法
configurations.all { resolutionStrategy.force("androidx.lifecycle:lifecycle-common:2.6.1") }
2. 排查完整依赖传递链
dependencyInsight仅展示指定配置的依赖情况,可能其他配置(如release、test)存在版本分歧。执行以下命令查看全配置依赖树:
./gradlew <module>:dependencies
若发现某个第三方库引入了2.0.0版本的lifecycle-common,直接在该依赖声明中排除传递依赖:
Groovy
implementation('com.example:third-party-lib:x.x.x') { exclude group: 'androidx.lifecycle', module: 'lifecycle-common' }
Kotlin DSL
implementation("com.example:third-party-lib:x.x.x") { exclude(group = "androidx.lifecycle", module = "lifecycle-common") }
3. 清理Gradle缓存并重新同步
缓存异常可能导致版本生效不一致,执行清理命令后重新同步项目:
./gradlew cleanBuildCache ./gradlew clean
在Android Studio中点击File -> Sync Project with Gradle Files完成同步。
4. 升级savedstate版本
确认androidx.savedstate:savedstate:1.2.1与lifecycle-common:2.6.1的兼容性,若存在适配问题,直接升级savedstate到最新稳定版(如1.2.2),它会默认依赖更高版本的lifecycle库:
implementation 'androidx.savedstate:savedstate:1.2.2'
5. 多模块项目统一版本管理
如果是多模块项目,在根目录的build.gradle中通过dependencyManagement统一管控版本,避免子模块各自引入不同版本:
Groovy(根build.gradle)
allprojects { dependencyManagement { dependencies { dependency 'androidx.lifecycle:lifecycle-common:2.6.1' } } }
Kotlin DSL(根build.gradle.kts)
allprojects { dependencyManagement { dependencies { add("androidx.lifecycle:lifecycle-common", "2.6.1") } } }
内容的提问来源于stack exchange,提问作者SageJustus
相关产品推荐
相关产品推荐

