Updating Capacitor to 2.0 in your app
Capacitor 2 makes some tooling updates including the adoption of Swift 5 in iOS and AndroidX for Android.
Read the Capacitor 2.0 announcement ›
Update Capacitor dependencies
First, update Capacitor Core and the CLI:
npm install @capacitor/cli@2 @capacitor/core@2
Next, update each Capacitor platform that you use:
# 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
Backward Incompatible Plugin Changes
- Camera
saveToGallery
default value is nowfalse
on all platforms- if
allowEditing
istrue
and the edit is canceled, the original image is returned
- Push Notifications
- permissions will no longer be requested when
register()
is called, userequestPermission()
PushNotificationChannel
renamed toNotificationChannel
- permissions will no longer be requested when
- Local Notifications
- permissions will no longer be requested when
register()
is called, userequestPermission()
schedule()
now returnsLocalNotificationScheduleResult
- permissions will no longer be requested when
- Toast
- unify duration across platforms: short 2000ms, long 3500ms
- Geolocation
- use Fused Location Provider on Android
requireAltitude
removed fromGeolocationOptions
- change native location accuracy values on iOS (more info)
- Filesystem
createIntermediateDirectories
was removed fromMkdirOptions
(userecursive
instead)recursive
option added to writeFile, which changes behavior on Android and web (more info)Application
directory option removed because it was broken
- Device
batteryLevel
andisCharging
removed fromgetInfo()
, usegetBatteryInfo()
- Modals
inputPlaceholder
sets a placeholder instead of text, useinputText
instead
- App
AppRestoredResult
is optional now, returned only if succeeded, otherwise it returns an error
- Clipboard
ReadOptions
was removed
iOS
Capacitor 2 requires Xcode 11+.
Update native project to Swift 5
Capacitor 2 uses Swift 5. It's recommended to update your native project to also use Swift 5.
- From Xcode click Edit -> Convert -> To Current Swift Syntax.
- App.app will appear selected, click Next button.
- Then a message will say No source changes necessary.
- Finally, click the Update button.
Android
AndroidX
Capacitor 2 uses AndroidX for Android support library dependencies as recommended by Google, so the native project needs to be updated to use AndroidX as well.
From Android Studio do Refactor -> Migrate to AndroidX. Then click on Migrate button and finally click on Do Refactor.
If using Cordova or Capacitor plugins that don't use AndroidX yet, you can use jetifier tool to patch them.
npm install jetifier
npx jetifier
To run it automatically after every package install, add "postinstall": "jetifier"
in the package.json
.
Create common variables
Create a android/variables.gradle
file with this content:
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'
}
In android/build.gradle
file, add 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()