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

Cordova中file-transfer与Whitelist插件冲突致Android构建失败求助

解决cordova-plugin-file-transfer与新版Cordova Android的Whitelist冲突问题

问题根源

你使用的cordova-plugin-file-transfer@1.7.1版本过于陈旧,硬编码依赖了已被新版Cordova Android(10+)移除的org.apache.cordova.Whitelist类。即使手动替换类名为Allowlist也无法解决,因为插件内部的API调用逻辑也未适配新版Cordova的Allowlist机制。同时,新版Cordova Android已内置Allowlist功能,无需单独安装cordova-plugin-whitelist插件。

解决方案

方案1:使用适配Allowlist的file-transfer修复分支

安装社区维护的修复版本,该分支已完成Whitelist到Allowlist的适配:

# 先移除旧插件
cordova plugin remove cordova-plugin-file-transfer cordova-plugin-whitelist
# 安装修复分支
cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git#allowlist-fix

方案2:迁移到官方推荐的原生API(更长期稳定)

cordova-plugin-file-transfer已被官方废弃,建议使用Web标准的Fetch API或XMLHttpRequest配合cordova-plugin-file实现文件传输:

文件下载示例

window.requestFileSystem(window.TEMPORARY, 0, fs => {
    fs.root.getFile('downloaded-file.jpg', { create: true }, fileEntry => {
        const downloadUrl = 'https://your-server.com/file.jpg';
        
        fetch(downloadUrl)
            .then(res => res.blob())
            .then(blob => {
                fileEntry.createWriter(writer => {
                    writer.onwriteend = () => console.log('下载完成');
                    writer.write(blob);
                });
            })
            .catch(err => console.error('下载失败:', err));
    });
});

文件上传示例

const localFileUrl = 'cdvfile://localhost/temporary/upload.jpg';
const uploadUrl = 'https://your-server.com/upload';

window.resolveLocalFileSystemURL(localFileUrl, fileEntry => {
    fileEntry.file(file => {
        const formData = new FormData();
        formData.append('upload-file', file, file.name);
        
        fetch(uploadUrl, {
            method: 'POST',
            body: formData
        })
        .then(res => res.json())
        .then(result => console.log('上传成功:', result))
        .catch(err => console.error('上传失败:', err));
    });
});

额外操作建议

  1. 清理旧平台残留,确保环境干净:
cordova platform remove android
cordova platform add android@latest
  1. 在config.xml中配置Allowlist规则(替代原whitelist插件的配置):
<!-- 允许访问的域名 -->
<access origin="*" />
<!-- 允许导航到的域名 -->
<allow-navigation href="https://your-server.com/*" />
<!-- 允许启动的外部应用 -->
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 10:15:27