Flutter安卓构建失败:Duplicate class错误求助
本地Flutter环境配置
Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.10.3, on macOS 13.4 22F66 darwin-arm64, locale fr-FR) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2) [✓] Xcode - develop for iOS and macOS (Xcode 14.3.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2022.1) [✓] VS Code (version 1.78.2) [✓] Connected device (3 available) [✓] Network resources • No issues found!
启动安卓应用时的错误日志
Launching lib/main.dart on SM G973F in debug mode... Running Gradle task 'assembleDebug'... FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:checkDebugDuplicateClasses'. > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable > Duplicate class com.google.zxing.BarcodeFormat found in modules jetified-android-zxingLibrary-0.1.4-PRE-runtime (com.github.leyan95:android-zxingLibrary:0.1.4-PRE) and jetified-core-3.3.0 (com.google.zxing:core:3.3.0) Duplicate class com.google.zxing.Binarizer found in modules jetified-android-zxingLibrary-0.1.4-PRE-runtime (com.github.leyan95:android-zxingLibrary:0.1.4-PRE) and jetified-core-3.3.0 (com.google.zxing:core:3.3.0) Duplicate class com.google.zxing.qrcode.decoder.Version found in modules jetified-android-zxingLibrary-0.1.4-PRE-runtime (com.github.leyan95:android-zxingLibrary:0.1.4-PRE) and jetified-core-3.3.0 (com.google.zxing:core:3.3.0) Duplicate class com.google.zxing.qrcode.decoder.Version$ECB found in modules jetified-android-zxingLibrary-0.1.4-PRE-runtime (com.github.leyan95:android-zxingLibrary:0.1.4-PRE) and jetified-core-3.3.0 (com.google.zxing:core:3.3.0) Duplicate class com.google.zxing.qrcode.decoder.Version$ECBlocks found in modules jetified-android-zxingLibrary-0.1.4-PRE-runtime (com.github.leyan95:android-zxingLibrary:0.1.4-PRE) and jetified-core-3.3.0 (com.google.zxing:core:3.3.0) Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 2m 59s Exception: Gradle task assembleDebug failed with exit code 1
已尝试的解决操作
- 删除缓存
- 更新依赖并修复缓存
- 删除全局.gradle目录(执行命令:
cd ~; rm -rf .gradle)
有效解决方案
错误核心是重复的zxing核心类,com.github.leyan95:android-zxingLibrary和com.google.zxing:core两个依赖包包含完全相同的类文件,导致Gradle构建冲突。按以下步骤解决:
定位依赖来源
在项目根目录执行命令,查看完整依赖树,确认哪个Flutter包间接引入了冲突的依赖:flutter pub deps --style=compact找到包含
com.github.leyan95:android-zxingLibrary或com.google.zxing:core的上游依赖包。排除重复依赖模块
打开项目的android/app/build.gradle文件,在dependencies块中添加排除规则,移除其中一个依赖的重复核心模块:dependencies { // 保留其他原有依赖 implementation('com.github.leyan95:android-zxingLibrary:0.1.4-PRE') { exclude group: 'com.google.zxing', module: 'core' } }若冲突来自Flutter插件,可在
pubspec.yaml中通过版本覆盖统一zxing版本:dependency_overrides: # 根据实际依赖包名调整,确保只保留一个版本的zxing核心 zxing_core: 3.3.0清理缓存并重新构建
执行以下命令彻底清理缓存后重新构建项目:flutter clean cd android && ./gradlew clean flutter pub get flutter run
内容的提问来源于stack exchange,提问作者SOULEYMANE TOURE
相关产品推荐
相关产品推荐

