代码重大变更
iOS部分
CAPBridgedProtocol协议变更
- CAPBridgedPlugin协议要求已从类级别移至实例级别
- pluginId重命名为- identifier以避免与- CAPPlugin.pluginId冲突,同时移除了- getMethod(_:)要求并将其转为内部扩展方法
- pluginMethods的类型定义也进行了明确(原为- Any类型,现指定为- CAPPluginMethod)
大多数用户不会遇到问题,因为目前普遍使用宏来生成对CAPBridgedPlugin的适配。只有那些手动  转换至CAPBridgedPlugin或未使用宏手动适配该协议的用户会受到影响。
Android部分
PluginCall.getObject() / PluginCall.getArray()方法
为了与iOS行为保持一致,Android上的PluginCall.getObject()和PluginCall.getArray()现在可能返回null值。建议插件开发者在处理这些方法返回值时进行null检查。
将插件中的Capacitor升级至5.0
使用@capacitor/plugin-migration-v4-to-v5工具
在插件目录下运行npx @capacitor/plugin-migration-v4-to-v5@latest命令,工具会自动完成所有文件修改。
手动更新文件
更新package.json
将@capacitor/cli、@capacitor/core、@capacitor/android和@capacitor/ios的版本更新为latest-5。
升级targetSDK/compileSDK至33
# build.gradle
android {
-    compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 32
+    compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 33
-    targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 32
+    targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 33
更新Android插件依赖版本
在build.gradle文件中更新以下依赖的最低版本:
ext {
    junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
-   androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxApppatibleVersion : '1.4.2'
+   androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.6.1'
-   androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.3'
+   androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5'
-   androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.4.0'
+   androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1'
升级Gradle插件至8.0.0
    dependencies {
-       classpath 'com.android.tools.build:gradle:7ự.1'
+       classpath 'com.android.tools.build:gradle:8.0.0'
    }
升级Gradle Wrapper至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
迁移包声明至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 project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 33
禁用Jetifier
# gradle.properties
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
- # Automatically convert third-party libraries to use AndroidX
- android.enableJetifier=true
升级至Java 17
# build.gradle
compileOptions {
-    sourceCompatibility JavaVersion.VERSION_11
+    sourceCompatibility JavaVersion.VERSION_17
-    targetCompatibility JavaVersion.VERSION_11
+    targetCompatibility JavaVersion.VERSION_17
}
更新kotlin版本
如果插件使用kotlin,请更新默认的kotlin_version
# build.gradle
buildscript {
-    ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '1.7.0'
+    ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '1.8.20'
    repositories {
同时将org.jetbrains.kotlin:kotlin-stdlib-jdk7或org.jetbrains.kotlin:kotlin-stdlib-jdk8依赖替换为org.jetbrains.kotlin:kotlin-stdlib。
# build.gradle
dependencies {
-    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"