Understanding Live Updates in Capgo
实时更新是Capacitor应用中最强大的功能之一,允许在不通过应用商店提交的情况下进行实时更新。让我们深入了解Capgo如何实现此功能。
核心概念
一个 Capacitor 应用程序由两个主要层次组成:
- Web层:包含在WebView中加载的HTML、CSS和JavaScript文件
- Native层:包含平台特定的 code (Android为Java/Kotlin,iOS为Swift)
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_KEEP_0__
-
客户端安全:
- __CAPGO_KEEP_1__
- 失败的解密触发自动回滚
- 在应用程序的受保护存储中安全存储密钥
本全面安全方法确保您的应用程序更新始终受到以下保护:
- 中间人攻击
- 服务器端违规
- 未经授权的修改
- 重放攻击
- 内容篡改
更新生命周期
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__CAPGO_KEEP_0__ - 是 true
directUpdate下一次应用后台化 - 如果
__CAPGO_KEEP_0__
- 移除失败的更新如果
autoDeleteFailed__CAPGO_KEEP_0__为真 - 清理旧版本如果
autoDeletePrevious__CAPGO_KEEP_0__为真
延迟更新
您可以控制更新何时安装使用延迟条件:
// 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:
此功能有用於:
- 在非高峰时间调度更新
- 与用户活动协调更新
- 确保平滑的更新体验
- 防止在关键任务期间中断
更新状态
在自动化过程中,捆绑包会通过这些状态转换:
- 下载中:
- 待安装: 下载完成,等待安装
- 成功: 更新已安装并激活
- 失败: 更新失败(触发自动回滚)
商店合规
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 可以下载到应用程序,但仅在以下条件下: (a) 不改变应用程序的主要目的,提供与应用程序提交到 App Store 的宣传目的不一致的功能或功能; (b) 不创建其他 __CAPGO_KEEP_2__ 或应用程序的商店或商店; (c) 不绕过签名、沙盒或其他操作系统的安全功能。”
__CAPGO_KEEP_0__ 更新仅修改 web 层,同时尊严所有平台安全边界。
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.
最佳实践
- 分阶段发布: 分段发布更新
- 版本控制: 跟踪所有部署的版本
- 回滚支持: 快速恢复问题
- 差分更新: 只下载更改的文件
何时使用实时更新
适用于:
- bug修复
- UI改进
- 内容更新
- 功能开关
不适用于:
- 原生code更改
- 重大版本更新
- 需要原生更改的安全补丁