从Capacitor 3升级到Capacitor 4
相比之前的版本升级,Capacitor 3到4的破坏性变更非常少。本指南将为您提供升级至当前Capacitor 4版本的步骤,以及官方插件的破坏性变更清单。
使用CLI进行迁移
使用npm i -D @capacitor/cli@latest-4
为项目安装最新版Capacitor CLI。安装完成后,只需运行npx cap migrate
即可让CLI自动处理迁移。如果迁移过程中有任何步骤无法完成,终端输出中会提供额外信息。以下是手动迁移的具体步骤。
iOS平台
本指南介绍如何将Capacitor 3的iOS项目升级至Capacitor 4。
提高iOS部署目标
在Xcode项目中执行以下操作:在项目编辑器中选中Project,打开Build Settings选项卡。在Deployment部分,将iOS Deployment Target修改为iOS 13.0。对所有应用Targets重复相同步骤。
然后打开ios/App/Podfile
并按以下步骤操作:
- 在第一行添加:
require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'
- 将iOS版本更新至13.0:
platform :ios, '13.0'
- 在最后一行添加:
post_install do |installer|
assertDeploymentTarget(installer)
end
移除冗余代码
从AppDelegate.swift
中移除未使用的touchesBegan
方法:
-override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- super.touchesBegan(touches, with: event)
-
- let statusBarRect = UIApplication.shared.statusBarFrame
- guard let touchPoint = event?.allTouches?.first?.location(in: self.window) else { return }
-
- if statusBarRect.contains(touchPoint) {
- NotificationCenter.default.post(name: .capacitorStatusBarTapped, object: nil)
- }
-}
可选:移除Info.plist中的NSAppTransportSecurity条目
NSAppTransportSecurity
仅用于实时重载功能,如果您不使用实时重载或使用Ionic CLI进行实时重载,则不再需要此配置。
-<key>NSAppTransportSecurity</key>
-<dict>
- <key>NSAllowsArbitraryLoads</key>
- <true/>
-</dict>
Android平台
本指南介绍如何将Capacitor 3的Android项目升级至Capacitor 4。
更新Android项目变量
在variables.gradle
文件中,更新以下最低版本要求并新增coreSplashScreenVersion
和androidxWebkitVersion
:
minSdkVersion = 22
compileSdkVersion = 32
targetSdkVersion = 32
androidxActivityVersion = '1.4.0'
androidxAppCompatVersion = '1.4.2'
androidxCoordinatorLayoutVersion = '1.2.0'
androidxCoreVersion = '1.8.0'
androidxFragmentVersion = '1.4.1'
coreSplashScreenVersion = '1.0.0-rc01'
androidxWebkitVersion = '1.4.0'
junitVersion = '4.13.2'
androidxJunitVersion = '1.1.3'
androidxEspressoCoreVersion = '3.4.0'
cordovaAndroidVersion = '10.1.1'
在Android清单中添加android:exported
标签
在AndroidManifest.xml
文件中,需要为<activity>
标签添加以下属性:
android:exported="true"
该标签确保您可以打开应用中的"Activity"(即屏幕)。有关此标签及其他属性的更多信息,请参阅Android的<activity>
参考文档。
:::提示
默认情况下,您的AndroidManifest.xml
文件位于android/app/src/main/AndroidManifest.xml
。
:::
更新Gradle Google Services插件
在android/build.gradle
文件中将classpath 'com.google.gms:google-services:4.3.5'
更新为classpath 'com.google.gms:google-services:4.3.13'
。
升级至Gradle 7
在File > Project Structure > Project
中调整Gradle项目设置。Android Gradle插件版本应为7.2.1或更高,Gradle版本应为7.4.2或更高。应用更改后,点击Android Studio右上角的大象图标执行Gradle同步。
:::提示
Android Studio可能提供自动升级至Gradle 7的选项。建议接受此建议!升级方法:打开build.gradle
文件,点击💡图标后选择"Upgrade Gradle"。项目迁移完成后,按上述方法执行Gradle同步。
另一种方法是使用Android Gradle插件升级助手自动完成迁移。 :::
确保使用Java 11
Capacitor 3支持Java 8和Java 11,而Capacitor 4将 仅支持Java 11。在Android Studio中通过以下菜单修改配置:
Preferences > Build, Execution, Deployment > Build Tools > Gradle
在此处将"Gradle JDK"修改为Java 11。
:::提示 Java 11已包含在最新版Android Studio中,无需额外下载! :::
切换至自动加载Android插件
这在Capacitor 3中是可选功能,但由于移除了init方法,Capacitor 4升级后变为强制要求。在MainActivity.java
中可移除onCreate
方法。现在通过npm安装或移除插件时无需再编辑此文件。
public class MainActivity extends BridgeActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- // Initializes the Bridge
- this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
- // Additional plugins you've installed go here
- add(Plugin1.class);
- add(Plugin2.class);
- }});
- }
}