Understanding Live Updates in Capgo
实时更新是 Capacitor 应用中的一个强大功能,允许在不通过应用商店提交的情况下实现实时更新。让我们深入了解 Capgo 如何实现此功能。
核心概念
一个 Capacitor 应用程序由两个主要层次组成:
- Web层:包含在WebView中加载的HTML、CSS和JavaScript文件
- Native层:包含平台特定的 code (Java/Kotlin用于Android,Swift用于iOS)
Capgo 的实时更新系统通过在运行时替换Web层来工作,因为这些文件不会编译到应用程序二进制文件中。
技术实施
Capacitor 服务器路径
Capgo 管理两个关键路径:
- 当前服务器路径:指向WebView中当前加载的文件
- 下一个服务器路径: 指向将在下一个应用重启时加载的文件
Android 实现
在 Android 上,Capgo 通过以下方式管理路径:
// Store next server path
private void setNextCapacitorServerPath(String path) {
SharedPreferences prefs = context.getSharedPreferences("CapWebViewSettings", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("serverBasePath", path);
editor.apply();
}
// Update current path and reload
private void setCurrentCapacitorServerPath(String path) {
bridge.setServerBasePath(path);
bridge.reload();
}
iOS 实现
在 iOS 上,路径通过以下方式管理:
// Store next server path
private func setNextCapacitorServerPath(path: String) {
KeyValueStore.standard["serverBasePath"] = path
}
// Update current path
private func setCurrentCapacitorServerPath(path: String) {
bridge.viewController.setServerBasePath(path: path)
}
安全措施
Capgo 实现了军用级安全性,通过端到端加密,确保您的应用程序更新在开发到部署之间保持完全安全。我们的加密系统超越了传统的code签名,提供了真正的零知识安全。
端到端加密架构
-
端到端加密 (E2EE): 每个更新包在离开开发环境之前都使用 AES-256-GCM 加密。这种军用级加密确保了您的应用程序更新在整个交付过程中保持完全私密和安全。
-
零知识架构: 与其他 OTA 更新解决方案不同,它们只签名更新,Capgo 使用真正的零知识加密。 这意味着:
- 更新内容在上传前进行加密
- Capgo 服务器只存储加密数据
- 仅在终端设备上发生解密
- 中间人无法访问您的更新内容
-
安全密钥管理:
- 加密密钥在您的 CI/CD 环境中生成并存储
- 私钥永远不会触及Capgo的服务器
- 每个应用程序版本都可以使用独特的加密密钥
- 密钥轮换支持以增强安全性
了解有关我们的加密系统的详细指南: Capgo Live Updates 中的端到端加密
安全更新流程
-
预上传加密:
- 在您的CI/CD管道中,更新将被加密
- 每个文件都将单独加密
- 元数据也将加密,确保完全的隐私
-
安全存储:
- 加密的包将存储在Capgo的全球CDN上
- 我们的服务器上永远不会有明文数据
- 即使在服务器被泄露的情况下,数据也仍然安全
-
安全交付:
- 更新将通过加密通道传递
- 每个应用实例都将验证加密完整性
- 自动重试机制用于失败的解密
-
客户端安全:
- 更新在安装之前会被验证
- 解密失败会触发自动回滚
- 在应用的保护存储中安全存储密钥
这种全面安全的方法确保您的应用更新始终受到保护,免受以下攻击:
- 中间人攻击
- 服务器端的安全漏洞
- 未经授权的修改
- 重放攻击
- 内容篡改
更新生命周期
Capgo的自动更新过程是默认开启的。以下是自动更新过程的工作原理:
1. 自动更新检查
插件在以下情况下自动检查更新:
- 应用启动时
此行为由以下设置控制: autoUpdate 您也可以手动检查更新
// capacitor.config.json
{
"plugins": {
"CapacitorUpdater": {
"autoUpdate": true // Enable automatic updates
}
}
}
2. 自动下载 getLatest()
当检测到新版本时,如果
自动下载开启: autoUpdate 下载开始自动
- 进度被内部跟踪
- __CAPGO_KEEP_0__
- 下载失败会在每次应用打开时自动重试
- 下载成功的文件会存储在应用存储中
您可以通过事件监控此过程:
CapacitorUpdater.addListener('download', (info: DownloadEvent) => {
console.log('Auto-download progress:', info.percent);
});
CapacitorUpdater.addListener('downloadComplete', (info: DownloadCompleteEvent) => {
console.log('Auto-download complete:', info.bundle);
});
3. 自动安装
安装的时间取决于您的配置:
// capacitor.config.json
{
"plugins": {
"CapacitorUpdater": {
"autoUpdate": true,
"directUpdate": false // install update on app backgrounding
"resetWhenUpdate": true, // reset live updates on native update (true by default)
"autoDeleteFailed": true, // Auto cleanup failed updates (true by default)
"autoDeletePrevious": true // Auto cleanup old versions (true by default)
}
}
}
安装发生在:
- 立即如果
directUpdate为真 - 在下一次应用后台化时如果
directUpdate为假 - 安装失败时自动回滚
插件还会自动管理存储:
- 移除失败的更新如果
autoDeleteFailed是 true - 清理旧版本如果
autoDeletePrevious是 true
延迟更新
您可以控制更新何时安装使用延迟条件:
// Delay until app goes to background
await CapacitorUpdater.setDelay({
kind: 'background'
});
// Delay until specific date
await CapacitorUpdater.setDelay({
kind: 'date',
value: '2024-03-20T10:00:00.000Z'
});
// Delay until next native version
await CapacitorUpdater.setDelay({
kind: 'nativeVersion'
});
// Multiple conditions
await CapacitorUpdater.setMultiDelay({
delayConditions: [
{
kind: 'background'
},
{
kind: 'date',
value: '2024-03-20T10:00:00.000Z'
}
]
});
可用延迟条件:
- background: 应用程序进入后台时安装
- date: 在特定日期/时间后安装
- nativeVersion: 安装后下一次本地更新
- kill: 安装后应用被杀死
This is useful for:
- 在非高峰时间调度更新
- 与用户活动协调更新
- 确保平滑更新体验
- 防止在关键任务期间中断
Update States
在自动过程中,捆绑包会通过这些状态转换:
- 下载中: 下载进行中
- pending下载完成,等待安装
- success:更新已安装并激活
- error:更新失败(触发自动回滚)
Store Compliance
Apple App Store ✅
Live Updates are fully compliant with the Apple App Store policies. As stated in the Apple Developer Program License Agreement:
“interpreted code may be downloaded to an Application but only so long as such code: (a) does not change the primary purpose of the Application by providing features or functionality that are inconsistent with the intended and advertised purpose of the Application as submitted to the App Store, (b) does not create a store or storefront for other code or applications, and (c) does not bypass signing, sandbox, or other security features of the OS.”
Capgo updates only modify the web layer while respecting all platform security boundaries.
Google Play Store ✅
Live Updates 符合 Google Play 政策。设备和网络滥用政策特别指出:
“This restriction does not apply to code that runs in a virtual machine or an interpreter where either provides indirect access to Android APIs (such as JavaScript in a webview or browser).”
Since Capgo only updates WebView content, it falls within these permitted guidelines.
Best Practices
- Phased Rollouts: Deploy updates gradually
- Version Control: Track all deployed versions
- Rollback Support: Quick recovery from issues
- Delta Updates: Only download changed files
When to Use Live Updates
适合使用的场景:
- 修复bug
- UI改进
- 内容更新
- 功能开关
不适合的场景:
- 原生code变更
- 重大版本更新
- 需要原生变更的安全补丁
继续阅读从Capgo中的Live Updates工作原理
如果您正在使用 Capgo的实时更新是如何工作的 为了计划原生插件的工作,连接它与 Capgo原生插件目录 在Capgo原生插件目录中, Capacitor的Capgo插件 在Capacitor的Capgo插件中, 添加或更新插件 添加或更新插件的实现细节, Ionic企业插件的替代品 在Ionic企业插件的替代品中, Capgo原生构建 在Capgo原生构建中,