Ionic项目构建报错:Google ZXing相关类重复冲突
问题描述
构建Ionic项目时出现重复类错误,具体错误信息如下:
Duplicate class com.google.zxing.qrcode.encoder.Encoder$1 found in modules jetified-barcodescanner-release-2.1.5-runtime (:barcodescanner-release-2.1.5:) and jetified-core-3.4.0 (com.google.zxing:core:3.4.0)
Duplicate class com.google.zxing.qrcode.encoder.MaskUtil found in modules jetified-barcodescanner-release-2.1.5-runtime (:barcodescanner-release-2.1.5:) and jetified-core-3.4.0 (com.google.zxing:core:3.4.0)
Duplicate class com.google.zxing.qrcode.encoder.MatrixUtil found in modules jetified-barcodescanner-release-2.1.5-runtime (:barcodescanner-release-2.1.5:) and jetified-core-3.4.0 (com.google.zxing:core:3.4.0)
Duplicate class com.google.zxing.qrcode.encoder.QRCode found in modules jetified-barcodescanner-release-2.1.5-runtime (:barcodescanner-release-2.1.5:) and jetified-core-3.4.0 (com.google.zxing:core:3.4.0)
中文翻译
在模块jetified-barcodescanner-release-2.1.5-runtime(:barcodescanner-release-2.1.5:)和jetified-core-3.4.0(com.google.zxing:core:3.4.0)中发现重复类com.google.zxing.qrcode.encoder.Encoder$1
在模块jetified-barcodescanner-release-2.1.5-runtime(:barcodescanner-release-2.1.5:)和jetified-core-3.4.0(com.google.zxing:core:3.4.0)中发现重复类com.google.zxing.qrcode.encoder.MaskUtil
在模块jetified-barcodescanner-release-2.1.5-runtime(:barcodescanner-release-2.1.5:)和jetified-core-3.4.0(com.google.zxing:core:3.4.0)中发现重复类com.google.zxing.qrcode.encoder.MatrixUtil
在模块jetified-barcodescanner-release-2.1.5-runtime(:barcodescanner-release-2.1.5:)和jetified-core-3.4.0(com.google.zxing:core:3.4.0)中发现重复类com.google.zxing.qrcode.encoder.QRCode
解决方案
这个问题是因为项目同时引入了barcodescanner插件和com.google.zxing:core依赖,两者包含相同的Zxing编码类,导致冲突,可通过以下方法解决:
- 排除重复依赖:在项目的
build.gradle文件中,给barcodescanner依赖添加排除规则,移除其自带的Zxing核心模块,保留单独引入的com.google.zxing:core:
dependencies { // 其他依赖... implementation('com.google.zxing:core:3.4.0') implementation(':barcodescanner-release-2.1.5:') { exclude group: 'com.google.zxing', module: 'core' } }
统一依赖版本:如果不需要单独引入
com.google.zxing:core,直接移除该依赖,使用barcodescanner插件自带的版本即可。更新插件版本:尝试更新
barcodescanner插件到最新版,新版本可能已修复依赖冲突问题,执行以下命令更新:
ionic cordova plugin update phonegap-plugin-barcodescanner npm update @ionic-native/barcode-scanner
内容的提问来源于stack exchange,提问作者Ahmadou Adiawiakoye

