在你的应用中更新 Capacitor 至 2.0
Capacitor 2 进行了一些工具链更新,包括在 iOS 上采用 Swift 5 以及在 Android 上采用 AndroidX。
更新 Capacitor 依赖
首先,更新 Capacitor Core 和 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
向后不兼容的插件更改
- Camera
saveToGallery的默认值现在在所有平台上均为false- 如果
allowEditing为true且编辑被取消,则返回原始图片
- Push Notifications
- 调用
register()时不再自动请求权限,请使用requestPermission() PushNotificationChannel已重命名为NotificationChannel
- 调用
- Local Notifications
- 调用
register()时不再自动请求权限,请使用requestPermission() schedule()现在返回LocalNotificationScheduleResult
- 调用
- Toast
- 统一各平台的持续时间:短 2000ms,长 3500ms
- Geolocation
- 在 Android 上使用 Fused Location Provider
requireAltitude已从GeolocationOptions中移除- 更改 iOS 上的原生定位精度值(更多信息)
- Filesystem
createIntermediateDirectories已从MkdirOptions中移除(请改用recursive)writeFile新增recursive选项,更改了 Android 和 Web 上的行为(更多信息)Application目录选项已被移除,因为它存在缺陷
- Device
batteryLevel和isCharging已从getInfo()中移除,请使用getBatteryInfo()
- Modals
inputPlaceholder现在设置占位符而非文本,请改用inputText
- App
AppRestoredResult现在是可选的,仅在成功时返回,否则返回错误
- Clipboard
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 使用 AndroidX 作为 Android support 库的依赖(这也是 Google 推荐的),因此原生项目也需要更新为使用 AndroidX。
在 Android Studio 中执行 Refactor -> Migrate to AndroidX。然后点击 Migrate 按钮,最后点击 Do Refactor。
如果你使用的 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'
// 注意:不要在此处放置应用依赖;它们属于
// 各个模块的 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
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 项目时,会显示 Plugin Update Recommended 消息。点击 update。它会提示你更新 Gradle 插件和 Gradle。点击 Update 按钮。
你也可以手动更新 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'
// 注意:不要在此处放置应用依赖;它们属于
// 各个模块的 build.gradle 文件
}
更改 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">
添加 caches 文件夹到 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'
}
}