从__CAPGO_KEEP_0__ 4更新到__CAPGO_KEEP_1__ 5:一步步指南
Capacitor

从Capacitor 4升级到Capacitor 5:一步步指南

了解如何以最小的破坏性更改更新您的项目,从Capacitor 4升级到Capacitor 5,包括更新官方插件和必需工具。

马丁·多纳迪尤

马丁·多纳迪尤

内容营销人员

从Capacitor 4升级到Capacitor 5:一步步指南

与之前的更新相比,Capacitor 4到Capacitor 5的过渡涉及最小的破坏性更改。这份指南提供了从Capacitor 4升级到Capacitor 5的步骤指南,以及官方插件的破坏性更改列表。

注意: Capacitor 5 requires NodeJS 16 or higher, as Node 12 has reached end-of-life and Node 14 will reach end-of-life on April 30th, 2023. It is recommended to use the latest LTS version of NodeJS.

  1. 安装 latest 在您的项目中使用的 Capacitor CLI 的版本:

    npm i -D @capacitor/cli@latest
  2. 运行以下命令让 CLI 处理迁移:

    npx cap migrate

    如果任何迁移步骤无法实现,终端输出中将提供额外信息。手动迁移步骤如下:

  3. 如果您已安装 VS Code 扩展,检查扩展的建议部分以找到将项目迁移到 Capacitor 5 的选项。

升级 Capacitor 4 iOS 项目到 Capacitor 5

  1. 升级 Xcode: Capacitor 5 需要 Xcode 14.1+。

  2. 更新 .gitignore:修改以下文件: .gitignore 更新 Assets 以使用单个应用图标:

    - App/Podfile.lock
    + App/output
  3. Xcode 14 支持 1024x1024 的单个应用图标。清理 AppIcon.appiconset 中的所有不必要的大小。: Xcode 14 supports a single app icon of 1024x1024. Clean up your AppIcon.appiconset by removing all unnecessary sizes.

升级 Capacitor 到 Capacitor

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

  2. 运行 AGP 升级助手: 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 将成为 androidScheme for existing apps to better enable Capacitor applications to use the system Autofill feature. To avoid data loss as a result of this change, set the scheme to http 应用程序使用系统 Autofill 功能的能力。为了避免由于此更改而导致的数据丢失,请将方案设置为

    {
      server: {
        androidScheme: "http"
      }
    }
  10. 现在,即使它是当前默认值。更新 Kotlin 版本 kotlin_version : 如果您的项目使用 Kotlin,则更新 '1.8.20'.

变量

The following plugin functionality has been modified or removed. Update your code accordingly:

  • 以下插件功能已更改或删除。更新您的 __CAPGO_KEEP_0__ 以及:
  • 浏览器
  • 相机
  • 设备
  • 地理位置
  • 谷歌地图
  • 本地通知
  • 推送通知
  • 状态栏

操作面板

  • 更新 androidxMaterialVersion __CAPGO_KEEP_0__ 1.8.0.

浏览器

  • 更新 androidxBrowserVersion __CAPGO_KEEP_0__ 1.5.0.

相机

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

设备

  • 更改 DeviceId.uuidDeviceId.identifier.
  • 在 iOS 16+ 中 DeviceInfo.name 除非您添加适当的 权限.

地理位置

  • 更新 playServicesLocationVersion21.0.1.

Google Maps

  • 更新以下变量:
    • 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() 按照此方式。

推送通知

  • 对于 Android 13,需要在目标 SDK 33 时执行新运行时权限检查才能接收推送通知。请 checkPermissions()requestPermissions() 按照此方式。
  • 更新 firebaseMessagingVersion 变量到 23.1.2.

状态栏

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

通过遵循这些步骤并更新您的 code,您应该现在已经成功地将您的项目从 Capacitor 4 升级到 Capacitor 5。请务必对您的应用程序进行彻底的测试,以确保所有功能和插件都正常工作。

为Capacitor应用提供实时更新

当有一个web层的bug活跃时,通过Capgo将修复推送到用户,而不是等待几天的应用商店审批。用户在后台接收更新,而原生变化仍然在正常审批路径中。

立即开始

最新博客

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