如何修复Flutter添加local_auth:^2.1.2后出现的:app:checkDebugDuplicateClasses错误
解决Flutter添加local_auth后出现重复类错误的方案
问题原因
local_auth: ^2.1.2依赖的androidx.lifecycle相关包版本,与项目中其他依赖(如Flutter框架或其他插件)引入的版本冲突,导致编译时触发重复类检测失败。
具体解决方案
方案一:强制统一生命周期依赖版本
在项目根目录下的android/build.gradle文件中,添加依赖版本强制配置,将冲突的生命周期包统一为同一版本:
allprojects { repositories { // 保留原有仓库配置 google() mavenCentral() } // 添加以下版本强制规则 configurations.all { resolutionStrategy { force 'androidx.lifecycle:lifecycle-viewmodel:2.5.1' force 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' } } }
配置完成后执行flutter clean,再重新编译项目。
方案二:排查并排除冲突依赖
- 运行命令查看项目完整依赖树,定位引入旧版本生命周期包的来源:
flutter pub deps
- 在
pubspec.yaml中给冲突的依赖添加排除规则。如果确认是local_auth引入了旧版本的lifecycle-viewmodel-ktx,修改依赖配置如下:
dependencies: local_auth: ^2.1.2 exclude: group: 'androidx.lifecycle' module: 'lifecycle-viewmodel-ktx'
- 执行
flutter pub get更新依赖,再重新编译项目。
方案三:升级local_auth到最新版本
旧版本的local_auth可能存在依赖兼容问题,尝试升级到最新稳定版:
- 修改
pubspec.yaml中的local_auth版本为当前最新稳定号(可通过官方包仓库确认):
dependencies: local_auth: ^最新稳定版本号
- 执行
flutter pub get,再执行flutter clean后重建项目。
内容的提问来源于stack exchange,提问作者Serenity Emmanuel
相关产品推荐
相关产品推荐

