You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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构建冲突。按以下步骤解决:

  1. 定位依赖来源
    在项目根目录执行命令,查看完整依赖树,确认哪个Flutter包间接引入了冲突的依赖:

    flutter pub deps --style=compact
    

    找到包含com.github.leyan95:android-zxingLibrary或com.google.zxing:core的上游依赖包。

  2. 排除重复依赖模块
    打开项目的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
    
  3. 清理缓存并重新构建
    执行以下命令彻底清理缓存后重新构建项目:

    flutter clean
    cd android && ./gradlew clean
    flutter pub get
    flutter run
    

内容的提问来源于stack exchange,提问作者SOULEYMANE TOURE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:12:39