跳到主要内容
版本:v3

将应用中的 Capacitor 升级到 2.0

Capacitor 2 带来了一些工具更新,包括 iOS 采用 Swift 5 以及 Android 采用 AndroidX。

阅读 Capacitor 2.0 公告 ›

更新 Capacitor 依赖项

首先更新 Capacitor 核心库和 CLI:

npm install @capacitor/cli@2 @capacitor/core@2

然后更新您使用的每个 Capacitor 平台:

# iOS
npm install @capacitor/ios@2
npx cap sync ios

# Android
npm install @capacitor/android@2
npx cap sync android

# Electron
cd electron
npm install @capacitor/electron@2

插件不兼容变更

  • 相机插件
    • saveToGallery 在所有平台上的默认值现在为 false
    • 如果 allowEditingtrue 且取消编辑,将返回原始图片
  • 推送通知
    • 调用 register() 时将不再请求权限,请改用 requestPermission()
    • PushNotificationChannel 重命名为 NotificationChannel
  • 本地通知
    • 调用 register() 时将不再请求权限,请改用 requestPermission()
    • schedule() 现在返回 LocalNotificationScheduleResult
  • Toast
    • 统一各平台持续时间:短 2000ms,长 3500ms
  • 地理位置
    • Android 上使用 Fused Location Provider
    • GeolocationOptions 中移除了 requireAltitude
    • 更改了 iOS 原生的定位精度值(更多信息
  • 文件系统
    • MkdirOptions 中移除了 createIntermediateDirectories(改用 recursive
    • 为 writeFile 添加了 recursive 选项,这会改变 Android 和网页端的行为(更多信息
    • 移除了损坏的 Application 目录选项
  • 设备信息
    • getInfo() 中移除了 batteryLevelisCharging,改用 getBatteryInfo()
  • 模态框
    • inputPlaceholder 现在设置占位文本而非输入文本,请改用 inputText
  • 应用
    • AppRestoredResult 现在是可选的,仅在成功时返回,否则返回错误
  • 剪贴板
    • 移除了 ReadOptions

iOS

Capacitor 2 需要 Xcode 11 或更高版本。

将原生项目更新至 Swift 5

Capacitor 2 使用 Swift 5。建议您也将原生项目更新至 Swift 5。

  1. 在 Xcode 中点击 编辑 -> 转换 -> 至当前 Swift 语法
  2. 选择 App.app,点击 下一步
  3. 提示 无需源代码更改
  4. 最后点击 更新 按钮

Android

AndroidX

Capacitor 2 遵循 Google 建议,使用 AndroidX 作为 Android 支持库依赖项,因此原生项目也需要更新使用 AndroidX。

在 Android Studio 中执行 重构 -> 迁移到 AndroidX。点击 迁移 按钮,最后点击 执行重构

如果使用的 Cordova 或 Capacitor 插件尚未支持 AndroidX,可以使用 jetifier 工具进行适配。

npm install jetifier
npx jetifier

要在每次安装包后自动运行,可在 package.json 中添加 "postinstall": "jetifier"

创建公共变量

创建 android/variables.gradle 文件,内容如下:

ext {
minSdkVersion = 21
compileSdkVersion = 29
targetSdkVersion = 29
androidxAppCompatVersion = '1.1.0'
androidxCoreVersion = '1.2.0'
androidxMaterialVersion = '1.1.0-rc02'
androidxBrowserVersion = '1.2.0'
androidxLocalbroadcastmanagerVersion = '1.0.0'
firebaseMessagingVersion = '20.1.2'
playServicesLocationVersion = '17.0.0'
junitVersion = '4.12'
androidxJunitVersion = '1.1.1'
androidxEspressoCoreVersion = '3.2.0'
cordovaAndroidVersion = '7.0.0'
}

android/build.gradle 文件中添加 apply from: "variables.gradle"

         classpath 'com.android.tools.build:gradle:4.1.1'
classpath 'com.google.gms:google-services:4.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

+apply from: "variables.gradle"

allprojects {
repositories {
google()
jcenter()

使用公共变量

如果已创建 variables.gradle 文件,请更新项目以使用这些变量。

android/app/build.gradle 文件中进行以下更新:

 android {
- compileSdkVersion 28
+ compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "com.example.app"
- minSdkVersion 21
- targetSdkVersion 28
+ minSdkVersion rootProject.ext.minSdkVersion
+ targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
 dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
- implementation 'androidx.appcompat:appcompat:1.0.0'
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
implementation project(':capacitor-android')
- testImplementation 'junit:junit:4.12'
- androidTestImplementation 'androidx.test.ext:junit:1.1.1'
- androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
+ testImplementation "junit:junit:$junitVersion"
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation project(':capacitor-cordova-android-plugins')

注意将单引号改为双引号。变量插值需要双引号。

建议更新 Android Studio 插件

在 Android Studio 中打开 Android 项目时,会出现 建议更新插件 提示。点击 更新。它将提示更新 Gradle 插件和 Gradle。点击 更新 按钮。

您也可以手动更新 Gradle 插件和 Gradle。

手动更新 Gradle 插件:在 android/build.gradle 文件中,将 com.android.tool.build:gradle 版本更新至 3.6.1。

手动更新 Gradle:在 android/gradle/wrapper/gradle-wrapper.properties 中,将 gradle-4.10.1-all.zip 改为 gradle-5.6.4-all.zip

更新 Google Services 插件

android/build.gradle 文件中,将 com.google.gms:google-services 依赖版本更新至 4.3.3。

     dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
- classpath 'com.google.gms:google-services:4.2.0'
+ classpath 'com.google.gms:google-services:4.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}

修改 android:configChanges 避免应用重启

android/app/src/main/AndroidManifest.xml 中,在 activity 的 android:configChanges 属性中添加 |smallestScreenSize|screenLayout|uiMode

         <activity
- android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
+ android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:name="com.example.app"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBarLaunch"
android:launchMode="singleTask">

FileProvider 文件路径中添加缓存文件夹,避免编辑图库图片时出现权限错误

android/app/src/main/res/xml/file_paths.xml 中添加 <cache-path name="my_cache_images" path="." />

 <?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="." />
+ <cache-path name="my_cache_images" path="." />
</paths>

移除 launch_splash.xml

可以删除 android/app/src/main/res/drawable/launch_splash.xml 文件,因为它不再使用。

移除 maven 仓库

Capacitor 通过 npm 分发,因此 android/app/build.gradle 中的 maven 仓库条目不再需要且可能引发问题。 从 repositories 部分移除:

 repositories {
- maven {
- url "https://dl.bintray.com/ionic-team/capacitor"
- }
flatDir {
dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
}
}