将应用中的 Capacitor 升级至 2.0
Capacitor 2 包含多项工具链更新,包括 iOS 采用 Swift 5 以及 Android 支持 AndroidX。
更新 Capacitor 依赖项
首先更新 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
- 如果 allowEditing为true且用户取消编辑,将返回原始图片
 
- 推送通知
- 调用 register()时不再自动请求权限,需改用requestPermission()
- PushNotificationChannel重命名为- NotificationChannel
 
- 调用 
- 本地通知
- 调用 register()时不再自动请求权限,需改用requestPermission()
- schedule()现在返回- LocalNotificationScheduleResult对象
 
- 调用 
- Toast
- 统一各平台显示时长:短提示 2000ms,长提示 3500ms
 
- 地理位置
- Android 平台使用 Fused Location Provider
- 移除 GeolocationOptions中的requireAltitude参数
- 调整 iOS 原生定位精度值(详情)
 
- 文件系统
- 从 MkdirOptions移除createIntermediateDirectories(改用recursive)
- 为 writeFile 添加 recursive选项,这会改变 Android 和 web 平台行为(详情)
- 移除已损坏的 Application目录选项
 
- 从 
- 设备信息
- 从 getInfo()移除batteryLevel和isCharging,改用getBatteryInfo()
 
- 从 
- 模态框
- inputPlaceholder现在设置占位文本而非默认值,需改用- inputText
 
- 应用
- AppRestoredResult改为可选,仅在成功时返回,否则返回错误
 
- 剪贴板
- 移除 ReadOptions类型
 
- 移除 
iOS 平台
Capacitor 2 需要 Xcode 11 或更高版本。
将原生项目升级至 Swift 5
Capacitor 2 使用 Swift 5,建议将您的原生项目也升级至 Swift 5。
- 在 Xcode 中选择 Edit -> Convert -> To Current Swift Syntax
- 选择 App.app 后点击 Next 按钮
- 当提示 No source changes necessary 时
- 最后点击 Update 按钮完成转换
Android 平台
AndroidX 支持
Capacitor 2 按照 Google 建议使用 AndroidX 作为 Android 支持库,因此原生项目也需要迁移至 AndroidX。
在 Android Studio 中选择 Refactor -> Migrate to AndroidX,点击 Migrate 按钮后选择 Do Refactor 开始迁移。
如果使用了尚未支持 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 文件中添加 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()
使用公共变量
创建公共变量文件后,请更新项目配置使用这些变量。
在 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.1opathology: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 中打开项目时,可能会显示 Plugin Update Recommended 提示。点击 update 后按提示更新 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'
     }
 }