Updating Your Capacitor Android Project
Occasionally, you'll need to make Capacitor updates to your Android app, including updating the version of Capacitor used in your app, or using new ways of interfacing with Capacitor inside of your Android codebase.
Updating Capacitor Android Library
To update the version of @capacitor/android used in your app, just npm install latest version:
npm install @capacitor/android@2
Then from Android Studio click the "Sync Project with Gradle Files" button.
Updating Android Project
To update the base structure of your Android project, view the android-template project in the Capacitor repo, under the tag corresponding to the latest stable release of Capacitor. The core project is kept simple on purpose: it shouldn't take much time to see what is different from the core project and your project.
From 1.0.0 to 1.1.0
Recommended change:
- Update
.gitignore
file insideandroid
folder with this changes
From <= 1.3.0 to 1.4.0
Recommended change:
- Update
strings.xml
file insideandroid/app/src/main/res/values/
folder with this change
From <= 1.5.1 to 2.0.0
Mandatory change:
-
Use Android X
Capacitor 2.0 uses Android X for Android support library dependencies as recommended by Google, so the native project needs to be updated to use Android X too.
From Android Studio do
Refactor -> Migrate to AndroidX
. Then click onMigrate
button and finally click onDo Refactor
.If using Cordova or Capacitor plugins that don't use Android X yet, you can use jetifier tool to patch them.
npm install -D jetifier
npx jetifier
To run it automatically after every package install, add "postinstall": "jetifier"
in the package.json
under "scripts".
Recommended changes:
-
Create common variables
Create a
android/variables.gradle
file with this contentext {
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, addapply from: "variables.gradle"
as shown here. -
Use common variables
If you created the
variables.gradle
file, update your project to use them. In theandroid/app/build.gradle
file, change:compileSdkVersion 28
tocompileSdkVersion rootProject.ext.compileSdkVersion
minSdkVersion 21
tominSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion 28
totargetSdkVersion rootProject.ext.targetSdkVersion
implementation 'androidx.appcompat:appcompat:1.0.0'
toimplementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
testImplementation 'junit:junit:4.12'
totestImplementation "junit:junit:$junitVersion"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
toandroidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
toandroidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
Note that they use double quote instead of single quote now, that's required for variables to work.
-
Android Studio Plugin Update Recommended
When you open the Android project in Android Studio, a
Plugin Update Recommended
message will appear. Click onupdate
. It will tell you to update Gradle plugin and Gradle. ClickUpdate
button.You can also manually update the Gradle plugin and Gradle.
To manually update Gradle plugin, edit
android/build.gradle
file. Changeclasspath 'com.android.tools.build:gradle:3.3.2'
toclasspath 'com.android.tools.build:gradle:3.6.1'
.To manually update Gradle, edit
android/gradle/wrapper/gradle-wrapper.properties
. Changegradle-4.10.1-all.zip
togradle-5.6.4-all.zip
. -
Update Google Services plugin
In
android/build.gradle
file, changeclasspath 'com.google.gms:google-services:4.2.0'
toclasspath 'com.google.gms:google-services:4.3.3'
. -
Change configChanges to avoid app restarts
In
android/app/src/main/AndroidManifest.xml
file, add|smallestScreenSize|screenLayout|uiMode
in the activityandroid:configChanges
attribute. -
Add caches folder to FileProvider file paths to avoid permission error on editing gallery images.
In
android/app/src/main/res/xml/file_paths.xml
add<cache-path name="my_cache_images" path="." />
.
For API changes, check the Release Notes.