Android及混编Flutter三方库重复及引用解决
情景:Android集成了Flutter混编, android 原生部分使用了pdfview三方库,flutter中也使用了该三方库,所以产生冲突。报错如下:
Duplicate class com.github.barteksc.pdfviewer.util.Util found in modules jetified-android-pdf-viewer-3.2.0-beta.1-runtime (com.github.barteksc:android-pdf-viewer:3.2.0-beta.1) and jetified-android-pdf-viewer-3.2.0-beta.1-runtime (com.github.mhiew:android-pdf-viewer:3.2.0-beta.1)
系统查找这个类在两个地方发现了不同的版本,而因为项目需求,这两个部分都必须存在(要求Android与Flutter部分既能分开打包运行也能合并运行)。所以不能去除其中之一。
解决办法:
找到Android代码中的flutter_pdfview这个model,打开build.gradle
将implementation替换为provided(compileOnly),以provided的方式参与编译。这样在App就只能访问到Android项目里的pdfview包了,flutter_pdfview modle中的pdfview包仅供自己访问。
implementation 'com.github.mhiew:android-pdf-viewer:3.2.0-beta.1'
compileOnly 'com.github.mhiew:android-pdf-viewer:3.2.0-beta.1'
参考文档:https://blog.csdn.net/weixin_42602900/article/details/124005566