相比之前的更新,升级从 Capacitor 4 到 Capacitor 5 的过程涉及最小的破坏性更改。这份指南提供了升级项目到 Capacitor 5 的逐步指南,以及官方插件的破坏性更改列表。
注意: Capacitor 5 需要 NodeJS 16 或更高版本,因为 Node 12 已经到达了末端生命期,Node 14 将于 2023 年 4 月 30 日到达末端生命期。建议使用 NodeJS 的最新 LTS 版本。
-
安装
latest项目中使用的Capacitor CLI的版本。npm i -D @capacitor/cli@latest -
运行以下命令让CLI处理迁移:
npx cap migrate如果您安装了 VS __CAPGO_KEEP_0__ 扩展,检查扩展的建议部分以找到迁移项目到 __CAPGO_KEEP_1__ 5 的选项。
-
升级 Code 4 iOS 项目到 Capacitor 5
升级 Capacitor 4 iOS 项目到 Capacitor 5
-
升级 Xcode: Capacitor 5 需要 Xcode 14.1+.
-
更新 .gitignore: 请对您的
.gitignore文件进行以下修改:- App/Podfile.lock + App/output -
更新 Assets 以使用单个应用图标:Xcode 14 支持单个应用图标1024x1024。清理 AppIcon.appiconset 中的所有不必要的尺寸。
升级 Capacitor 4 Android 项目到 Capacitor 5
-
升级 __CAPGO_KEEP_0__ 4 Android 项目到 __CAPGO_KEEP_1__ 5: Capacitor 5 requires Android Studio Flamingo | 2022.2.1 or newer due to the usage of Gradle 8, which requires Java JDK 17. Java 17 ships with Android Studio Flamingo, so no additional downloads are needed.
-
: __CAPGO_KEEP_0__ 5 需要 Android Studio Flamingo | 2022.2.1 或更高版本,因为它使用 Gradle 8,Gradle 8 需要 Java JDK 17。Java 17 与 Android Studio Flamingo 一起提供,因此无需额外下载。运行 AGP Upgrade 助手程序
Tools -> AGP Upgrade Assistant. -
更新 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' -
更新 Google 服务:
# build.gradle dependencies { - classpath 'com.google.gms:google-services:4.3.13' + classpath 'com.google.gms:google-services:4.3.15' } -
更新 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' } -
更新 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 -
禁用 Jetifier:
# gradle.properties android.useAndroidX=true - android.enableJetifier=true -
移动包到
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 -
更新 androidScheme: 在 Capacitor 6 中,
https将成为现有应用的默认设置,以更好地启用 __CAPGO_KEEP_0__ 应用使用系统 Autofill 功能。为了避免由于此更改而导致的数据丢失,请将方案设置为androidSchemefor 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 tohttp现在,即使是当前的默认值。{ server: { androidScheme: "http" } } -
更新 Kotlin 版本: 如果您的项目使用 Kotlin,更新
kotlin_version变量到'1.8.20'.
插件功能性变化
以下插件功能已修改或删除。更新您的 code 以适应:
- 动作表单
- 浏览器
- 相机
- 设备
- 地理位置
- Google Maps
- 本地通知
- 推送通知
- 状态栏
操作面板
- 更新
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.uuid到DeviceId.identifier. - 在 iOS 16+ 中,
DeviceInfo.name许可 地理位置.
地理位置
- 更新
playServicesLocationVersion到21.0.1.
Google Maps
- 更新以下变量:
googleMapsPlayServicesVersion到18.1.0.googleMapsUtilsVersion到3.4.0.googleMapsKtxVersion到3.4.0.googleMapsUtilsKtxVersion到3.4.0.kotlinxCoroutinesVersion到1.6.4.androidxCoreKTXVersion到1.10.0.kotlin_version到1.8.20.
本地通知
- 对于 Android 13,为了在目标版本 SDK 33 上排程本地通知,需要进行新的运行时权限检查。
checkPermissions()和requestPermissions()accordingly.
推送通知
- 对于 Android 13,需要在目标SDK 33 时,调用新的运行时权限检查,以接收推送通知。
checkPermissions()和requestPermissions()accordingly. - 更新
firebaseMessagingVersion变量23.1.2.
状态栏
- 在 iOS 上,已更改默认状态栏动画为
FADE.
By following these steps and updating your code accordingly, you should now have successfully updated your project from Capacitor 4 to Capacitor 5. Make sure to test your application thoroughly to ensure that all features and plugins are working as expected.
Keep going from 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 原生构建流程做准备。