Cordova引入cordova-plugin-file-transfer插件编译失败及替代方案咨询
问题描述
在Cordova项目中集成cordova-plugin-file-transfer插件后,构建过程出现如下报错:
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * 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 Gradle official support channel. Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. Refer to Gradle official CLI documentation for warning mode configuration details. BUILD FAILED in 6s 45 actionable tasks: 5 executed, 40 up-to-date
移除该插件后项目所有功能均可正常运行,核心疑问为:该插件是否已废弃,可采用什么方案对其进行替换?
解答
- 插件状态说明:
cordova-plugin-file-transfer早被Apache Cordova官方正式废弃,已经停止所有维护更新。这个插件最后一个稳定版发布时,AndroidX、Gradle 7+、iOS 13+这些当前主流的移动开发生态组件都还未正式上线,原生代码中存在大量过时API引用,和新版构建环境的兼容性问题没有官方修复,碰到的Java编译报错就是插件原生代码与当前项目构建配置不匹配导致的典型问题。 - 官方推荐替换方案:不需要额外引入第三方原生插件,直接使用现代Web标准API即可完全覆盖旧插件的所有能力,长期维护性更强,不会随构建环境、系统版本升级出现兼容性问题:
- 基础文件上传、下载场景:使用标准
fetchAPI配合FormData、Blob实现网络传输,搭配cordova-plugin-file完成本地文件系统的读写操作即可。 - 需要监听传输进度、大文件断点续传的场景:使用原生
XMLHttpRequest实现,该API原生支持上传、下载进度事件,无需原生层额外封装。简单的带进度下载实现示例:const downloadTask = new XMLHttpRequest(); downloadTask.open('GET', targetFileUrl); downloadTask.responseType = 'blob'; // 监听下载进度 downloadTask.onprogress = (event) => { if (event.lengthComputable) { const percent = Math.round((event.loaded / event.total) * 100); // 自行实现进度更新逻辑 } }; downloadTask.onload = () => { if (downloadTask.status === 200) { const fileBlob = downloadTask.response; // 调用cordova-plugin-file的API将blob写入本地目标路径即可完成下载 } }; downloadTask.send(); - 上传场景逻辑一致,通过
XMLHttpRequest.upload.onprogress即可监听上传进度。
- 基础文件上传、下载场景:使用标准
- 临时应急方案(不推荐):如果短时间内无法完成旧代码迁移,可手动修改插件内的Android原生源码,将旧版Support库依赖替换为AndroidX对应引用,调整构建配置适配当前Gradle版本,但该方案没有后续维护保障,后续升级系统、构建工具版本时大概率会再次出现兼容问题。
内容的提问来源于stack exchange,提问作者Massimiliano Comunale
相关产品推荐
相关产品推荐

