跳过主要内容
Capacitor

Updating from Capacitor 4 to Capacitor 5: A Step-by-Step Guide

Learn how to update your project from Capacitor 4 to Capacitor 5 with minimal breaking changes, including updating official plugins and required tools.

文章贡献者

马丁·多纳迪厄

作者

瓦莱里娅

审阅者

乔丹

编辑

Updating from Capacitor 4 to Capacitor 5: A Step-by-Step Guide

与之前的更新相比,Capacitor 4 到 Capacitor 5 的过渡涉及最小的破坏性更改。这份指南提供了逐步的更新指南,帮助您将项目升级到 Capacitor 5,另外还列出了官方插件的破坏性更改。

注意:Capacitor 5 需要 NodeJS 16 或更高版本,因为 Node 12 已经到达了末端生命期,Node 14 将于 2023 年 4 月 30 日到达末端生命期。建议使用 NodeJS 的最新 LTS 版本。

  1. 安装项目中的 __CAPGO_KEEP_0__ __CAPGO_KEEP_1__ 的最新版本: latest version of the Capacitor CLI in your project:

    npm i -D @capacitor/cli@latest
  2. Run the following command to let the CLI handle the migration:

    npx cap migrate

    如果您安装了 VS __CAPGO_KEEP_0__ 扩展,请检查扩展的建议部分,找到迁移项目到 __CAPGO_KEEP_1__ 5 的选项。

  3. Code 4 iOS 项目升级到 Capacitor 5:

Upgrading Capacitor 4 iOS Project to Capacitor 5

  1. __CAPGO_KEEP_0__ 5 需要 Xcode 14.1+。: Capacitor 5 requires Xcode 14.1+.

  2. __CAPGO_KEEP_0__ 5 需要 Xcode 14.1+。: 请对以下文件进行以下修改 .gitignore : 文件:

    - App/Podfile.lock
    + App/output
  3. 更新 Assets 以使用单个应用图标更新:Xcode 14 支持单个应用图标,尺寸为 1024x1024。清理 AppIcon.appiconset 中的所有不必要的尺寸。

将 Capacitor 4 Android 项目升级到 Capacitor 5

  1. 升级 Android Studio: Capacitor 5 需要 Android Studio Flamingo | 2022.2.1 或更高版本,因为它使用 Gradle 8,Gradle 8 需要 Java JDK 17。Java 17 与 Android Studio Flamingo 一起提供,因此无需额外下载。

  2. 运行 AGP Upgrade 助手: Android Studio 可以帮助一些与 Gradle 和将包移动到构建文件相关的更新。要开始,请运行 Tools -> AGP Upgrade Assistant.

  3. 更新 Android 项目变量: 在您的 variables.gradle 文件中,更新您的值以以下新最低值:

    minSdkVersion = 22
    compileSdkVersion = 33
    targetSdkVersion = 33
    androidxActivityVersion = '1.7.0'
    androidxAppCompatVersion = '1.6.1'
    androidxCoordinatorLayoutVersion = '1.2.0'
    androidxCoreVersion = '1.10.0'
    androidxFragmentVersion = '1.5.6'
    coreSplashScreenVersion = '1.0.0'
    androidxWebkitVersion = '1.6.1'
    junitVersion = '4.13.2'
    androidxJunitVersion = '1.1.5'
    androidxEspressoCoreVersion = '3.5.1'
    cordovaAndroidVersion = '10.1.1'
  4. 更新 Google 服务:

    # build.gradle
    dependencies {
    -       classpath 'com.google.gms:google-services:4.3.13'
    +       classpath 'com.google.gms:google-services:4.3.15'
    }
  5. 更新 Gradle 插件到 8.0.0:

    # build.gradle
    dependencies {
    -       classpath 'com.android.tools.build:gradle:7.2.1'
    +       classpath 'com.android.tools.build:gradle:8.0.0'
    }
  6. 更新 Gradle 包装器到 8.0.2:

    # gradle-wrapper.properties
    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    - distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
    + distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-all.zip
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
  7. 禁用 Jetifier:

    # gradle.properties
    android.useAndroidX=true
    - android.enableJetifier=true
  8. 将包移动到 build.gradle:

    # AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    - <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    -     package="[YOUR_PACKAGE_ID]">
    + <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    # build.gradle
    android {
    +     namespace "[YOUR_PACKAGE_ID]"
          compileSdkVersion rootProject.ext.compileSdkVersion
  9. 更新 androidScheme: 在 Capacitor 6 中 https 将成为 __CAPGO_KEEP_0__ 的默认设置 androidScheme 为现有应用程序提供更好的支持,使 Capacitor 应用程序能够使用系统 Autofill 功能。为了避免由于此更改而导致的数据丢失,请将方案设置为 http 现在,即使它是当前默认值

    {
      server: {
        androidScheme: "http"
      }
    }
  10. 更新 Kotlin 版本: 如果您的项目使用 Kotlin,则更新 kotlin_version 变量到 '1.8.20'.

插件功能变更

以下插件功能已修改或删除。请更新您的code:

  • 操作面板
  • 浏览器
  • 相机
  • 设备
  • 地理位置
  • 谷歌地图
  • 本地通知
  • 推送通知
  • 状态栏

操作面板

  • 更新 androidxMaterialVersion 变量为 1.8.0.

浏览器

  • 更新 androidxBrowserVersion 变量为 1.5.0.

相机

  • 对于 Android 13,添加读取媒体图像权限(<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>)在 AndroidManifest.xml.
  • 更新 androidxMaterialVersion 变量为 1.8.0.
  • 更新 androidxExifInterfaceVersion 变量到 1.3.6.

设备

  • 更改 DeviceId.uuidDeviceId.identifier.
  • 在 iOS 16+ 中 DeviceInfo.name 将返回一个通用设备名称,除非您添加适当的 特权.

地理位置

  • 更新 playServicesLocationVersion21.0.1.

谷歌地图

  • 更新以下变量:
    • googleMapsPlayServicesVersion18.1.0.
    • googleMapsUtilsVersion3.4.0.
    • googleMapsKtxVersion3.4.0.
    • googleMapsUtilsKtxVersion3.4.0.
    • kotlinxCoroutinesVersion1.6.4.
    • androidxCoreKTXVersion1.10.0.
    • kotlin_version1.8.20.

本地通知

  • 为了 Android 13,需要在目标 SDK 33 时调用新的运行时权限检查来排程本地通知。 checkPermissions() 并且 requestPermissions() 因此

推送通知

  • For Android 13, a new runtime permission check is required to receive push notifications when targeting SDK 33. Call checkPermissions() 并且 requestPermissions() 因此
  • 更新 firebaseMessagingVersion 变量 23.1.2.

状态栏

  • 在 iOS 上,状态栏的默认动画已经更改为 FADE.

按照这些步骤并更新您的 code,您应该现在已经成功地从 Capacitor 4 更新到 Capacitor 5。确保测试您的应用程序,以确保所有功能和插件都按预期工作。

继续阅读 Updating from Capacitor 4 to Capacitor 5: A Step-by-Step Guide

如果您正在使用 Updating from Capacitor 4 to Capacitor 5: A Step-by-Step Guide 来规划原生插件工作,连接它 Capgo 插件目录 为产品工作流程在 Capgo 插件目录中 Capacitor 插件由 Capgo 为实现细节在 Capacitor 插件由 Capgo 中 添加或更新插件 为实现细节在添加或更新插件中 Ionic 企业插件替代品 为产品工作流程在 Ionic 企业插件替代品中,并且 Capgo 原生构建 为产品工作流程在 Capgo 原生构建中。

实时更新 Capacitor 应用

当 web 层 bug 活跃时,通过 Capgo 发布修复,而不是等待几天的 app 商店审批。用户在后台接收更新,而原生变化保持在正常审批路径中。

来自马丁的人性化支持

立即开始

最新博客文章

Capgo 为您提供了创建真正专业的移动应用所需的最佳见解。