Understanding Live Updates in Capgo
Live updates are one of the most powerful features in Capacitor apps, allowing real-time updates without app store submissions. Let’s dive deep into how Capgo implements this functionality.
核心概念
一个 Capacitor 应用程序由两个主要层次组成:
- Web层:包含在WebView中加载的HTML、CSS和JavaScript文件
- 原生层:包含平台特定的 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 的端到端加密
安全更新流程
-
预上传加密:
- Capacitor更新在您的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安装失败时自动回滚 - 插件还会自动管理存储:
protectedTokens
- 移除失败的更新如果
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'
}
]
});
可用延迟条件:
- 背景: 应用程序进入背景时安装
- 日期: 安装在特定日期/时间后
- 原生版本: 安装后下一个原生更新
- kill: 应用被杀死后安装
这对以下有用:
- 在非高峰时间调度更新
- 与用户活动协调更新
- 确保平滑的更新体验
- 防止在关键任务期间中断
更新状态
在自动过程中,捆绑包会通过这些状态转换:
- 下载中: 下载进行中
- pending: 下载完成,等待安装
- success: 更新已安装并激活
- error: 更新失败(触发自动回滚)
Store Compliance
Apple App Store
Live Updates 与 Apple App Store 政策完全符合。根据 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 更新仅修改 web 层,同时尊严所有平台安全边界。
Google Play Store
Live Updates 符合 Google Play 政策。设备和网络滥用政策特别指出:
“此限制不适用于在虚拟机或解释器中运行的code,前提是其中之一提供了对 Android API 的间接访问(例如在 webview 或浏览器中使用 JavaScript)。”
由于Capgo 只更新 WebView 内容,因此它符合这些允许的指南。
最佳实践
- 阶段性发布: 部署更新逐步
- 版本控制: 跟踪所有部署的版本
- 回滚支持: 快速恢复问题
- 差分更新: 只下载更改的文件
什么时候使用实时更新
适合:
- 修复bug
- UI改进
- 内容更新
- 功能开关
不适合:
- 原生code变更
- 重大版本更新
- 需要原生变更的安全补丁
Keep going from How Live Updates Work in Capgo
如果您正在使用 How Capgo Live Updates Work to plan native plugin work, connect it with Capgo Native Plugin Directory for the product workflow in Capgo Native Plugin Directory Capacitor Native Plugins by Capgo for the implementation detail in Capacitor Native Plugins by Capgo Adding or Updating Native Plugins for the implementation detail in Adding or Updating Native Plugins Ionic Enterprise Native Plugin Alternatives for the product workflow in Ionic Enterprise Native Plugin Alternatives, and Capgo Native App Builds for the product workflow in Capgo Native App Builds.