跳到主要内容
版本:v5

将应用中的Capacitor升级到2.0

Capacitor 2进行了多项工具链更新,包括iOS平台采用Swift 5以及Android平台采用AndroidX支持库。

阅读Capacitor 2.0发布公告 ›

更新Capacitor依赖项

首先更新核心库和CLI工具:

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

然后更新您使用的各个平台包:

# 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提示
    • 统一各平台显示时长:短提示2000毫秒,长提示3500毫秒
  • 地理位置
    • Android平台使用Fused Location Provider
    • 移除了GeolocationOptions中的requireAltitude参数
    • 调整了iOS原生定位精度值(详情
  • 文件系统
    • 移除了MkdirOptions中的createIntermediateDirectories(改用recursive参数)
    • writeFile添加recursive选项,改变了Android和Web平台行为(详情
    • 移除了损坏的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支持库,因此需要将原生项目也迁移至AndroidX。

在Android Studio中执行 重构 -> 迁移至AndroidX,点击 迁移 按钮后选择 执行重构

如果使用了尚未支持AndroidX的Cordova或Capacitor插件,可以使用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文件中添加引用:

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

// 注意:不要在此处放置应用依赖项,它们应该
// 放在各模块的build.gradle文件中
}
}

+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
 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"

注意将单引号改为双引号,这是变量插值所必需的

推荐更新Android Studio插件

当在Android Studio中打开项目时,会出现插件更新推荐提示。点击更新,然后按提示更新Gradle插件和Gradle版本。

也可以手动更新:

  • android/build.gradle中更新com.android.tool.build:gradle版本至3.6.1
  • android/gradle/wrapper/gradle-wrapper.properties中将gradle-4.10.1-all.zip改为gradle-5.6.4-all.zip

更新Google Services插件

android/build.gradle中更新依赖版本:

     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'

修改配置避免应用重启

android/app/src/main/AndroidManifest.xml中扩展配置变更选项:

         <activity
- android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
+ android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"

添加缓存路径配置

android/app/src/main/res/xml/file_paths.xml中添加缓存路径:

 <?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>

移除启动闪屏配置

可删除android/app/src/main/res/drawable/launch_splash.xml文件,该文件已不再使用。

移除Maven仓库配置

Capacitor现在通过npm分发,可以移除android/app/build.gradle中的Maven仓库配置:

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