Cordova 11更新内容

参考

  1. https://cordova.apache.org/news/2021/12/21/cordova-cli-11.0.0-release.html
  2. https://volt.build/docs/upgrading_to_cordova_11/

Cordova11 较 Cordova10 版本,主要是 Android 端做了修改。

1. 平台无关

1.1 Node版本支持

此版本放弃了对 Nodejs 10 的支持。Cordova 所需的最低支持版本是 12.x。

1.2 NPM依赖版本

更新了各npm依赖版本

1.3 文件传输

cordova-plugin-file-transfer插件下岗(注意,cordova-plugin-file还是有用的哦);

可以使用 JavaScript 的 XMLHttpRequest 代替,Cordova官方有迁移文档可以参考一下: https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html

这里也把相关示例代码粘出来:
下载:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    console.log('file system open: ' + fs.name);
    fs.root.getFile('bot.png', { create: true, exclusive: false }, function (fileEntry) {
        console.log('fileEntry is file? ' + fileEntry.isFile.toString());
        var oReq = new XMLHttpRequest();
        // Make sure you add the domain name to the Content-Security-Policy <meta> element.
        oReq.open("GET", "http://cordova.apache.org/static/img/cordova_bot.png", true);
        // Define how you want the XHR data to come back
        oReq.responseType = "blob";
        oReq.onload = function (oEvent) {
            var blob = oReq.response; // Note: not oReq.responseText
            if (blob) {
                // Create a URL based on the blob, and set an <img> tag's src to it.
                var url = window.URL.createObjectURL(blob);
                document.getElementById('bot-img').src = url;
                // Or read the data with a FileReader
                var reader = new FileReader();
                reader.addEventListener("loadend", function() {
                   // reader.result contains the contents of blob as text
                });
                reader.readAsText(blob);
            } else console.error('we didnt get an XHR response!');
        };
        oReq.send(null);
    }, function (err) { console.error('error getting file! ' + err); });
}, function (err) { console.error('error getting persistent fs! ' + err); });

上传:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    console.log('file system open: ' + fs.name);
    fs.root.getFile('bot.png', { create: true, exclusive: false }, function (fileEntry) {
        fileEntry.file(function (file) {
            var reader = new FileReader();
            reader.onloadend = function() {
                // Create a blob based on the FileReader "result", which we asked to be retrieved as an ArrayBuffer
                var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
                var oReq = new XMLHttpRequest();
                oReq.open("POST", "http://mysweeturl.com/upload_handler", true);
                oReq.onload = function (oEvent) {
                    // all done!
                };
                // Pass the blob in to XHR's send method
                oReq.send(blob);
            };
            // Read the file as an ArrayBuffer
            reader.readAsArrayBuffer(file);
        }, function (err) { console.error('error getting fileentry file!' + err); });
    }, function (err) { console.error('error getting file! ' + err); });
}, function (err) { console.error('error getting persistent fs! ' + err); });

2. Android

2.1 白名单插件下岗

之前在添加平台时会默认安装的 cordova-plugin-whitelist 插件已经下岗。

目前 cordova-android 内置 AllowList 插件,使用方法与whitelist插件类似,详见官方文档 https://cordova.apache.org/docs/en/11.x/guide/appdev/allowlist/index.html

2.2 默认AndroidX

(想使用Android Support Library,可以添加 cordova-plugin-androidx-adapter 插件后重新添加平台解决)

但还是强烈建议升级 androidx。

Android Support Library 用于向下兼容,比如常用的 support-v4 和 appcompat-v7,这里的v4对应系统为 Android1.6,v7对应系统为 Android2.1,都已经被时间淘汰了,support-v4、appcompat-v7库也不再支持那么久远的系统了,但是它们的名字却一直保留了下来。

AndroidX本质上其实就是对Android Support Library进行的一次升级。

从support library 迁移到 androidx 也很简单,Android Studio提供一键迁移功能,只需要 项目名 -> 右键 -> Refactor -> Migrate to AndroidX,在弹窗中确认即可。

cordova 不支持ios开发吗 cordova最新版本_ios

2.3 TargetSDK

TargetSDK 版本限制在 28~31,超出范围 Build 时将报错。

2.4 Gradle版本

默认Gradle版本为 7.1(构建的时候终于可以不用关电脑热点了…)

注意,如果 repositories 添加了 maven 仓库,并且为 HTTP 协议,需要添加 allowInsecureProtocol = true,否则会报错。

cordova 不支持ios开发吗 cordova最新版本_ios_02

2.5 支持JAVA11

其实 cordova-android@10.0.0 就已经支持 java11,不过之前我们都用的 ^9.1.0 版本。

2.6 跨域

可参考 cordova-android github issue-1354

在 Cordova10 版本,默认使用 cordova-android@9,使用未强制执行 COR 的普通旧文件系统 (file://),

而在 cordova-android@10 中,实现了一个称为 WebAssetLoader 的东西,它通过 https://localhost 协议代理请求,如果未进行处理,就会导致跨域问题。

如何解决?
有两种方案:

  1. 在 config.xml 中添加下面代码
<preference name="AndroidInsecureFileModeEnabled" value="true" />
  1. 配置代理服务器。

2.7 正式包打包

使用 cordova build android --release 打Android正式包,默认格式改为 .aab,如果要打apk格式,需要增加命令行参数:
cordova build android --release -- --packageType=apk