跳过主要内容

How Live Updates Work in Capgo

Deep dive into the technical implementation of live updates in Capgo, understanding how it works under the hood for both iOS and Android.

文章贡献者

马丁·多纳迪

作者

瓦莱里亚

审阅者

乔丹

编辑器

How Live Updates Work in Capgo

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 应用程序由两个主要层次组成:

  1. Web层:包含在 WebView 中加载的 HTML、CSS 和 JavaScript 文件
  2. 原生层:包含平台特定的 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签名,提供了真正的零知识安全性。

端到端加密架构

  1. 端到端加密(E2EE)每个更新包在离开开发环境之前都使用 AES-256-GCM 加密。这种军用级加密确保了您的应用程序更新在整个交付过程中始终保持完全私密和安全。

  2. 零知识架构与其他 OTA 更新解决方案不同,Capgo 实施了真正的零知识加密。这意味着:

    • 更新内容在上传之前进行加密
    • Capgo 服务器只存储加密数据
    • 加密仅在终端用户设备上发生
    • 中间者无法访问您的更新内容
  3. 安全密钥管理:

    • 加密密钥在您的 CI/CD 环境中生成和存储
    • 私钥永远不会触及Capgo的服务器
    • 每个应用程序版本都可以使用独特的加密密钥
    • 加密密钥轮换支持增强的安全性

了解有关我们的加密系统的详细指南: Capgo Live Updates中的端到端加密

更新安全流程

  1. 预上传加密:

    • 更新在您的CI/CD管道中进行加密
    • 每个文件都单独加密
    • 元数据也加密以实现完全的隐私
  2. 安全存储:

    • 加密的包在Capgo的全球CDN上存储
    • 从未有明文数据接触到我们的服务器
    • 即使服务器遭到入侵,数据也仍然安全
  3. 安全交付:

    • 更新通过加密通道传递
    • 每个应用实例都验证加密完整性
    • 自动重试机制用于失败的解密
  4. 客户端安全:

    • 更新在安装之前进行验证
    • 失败的解密触发自动回滚
    • 应用的受保护存储中安全存储密钥

这种全面安全的方法确保您的应用更新始终受到以下保护:

  • 中间人攻击
  • 服务器侧入侵
  • 未经授权的修改
  • 重放攻击
  • 内容篡改

更新生命周期

Capgo的更新流程是默认自动的。自动更新流程如下:

1. 自动更新检查

插件在以下情况下自动检查更新:

  • 应用启动

此行为由以下设置控制: autoUpdate 您也可以手动检查使用

// capacitor.config.json
{
  "plugins": {
    "CapacitorUpdater": {
      "autoUpdate": true // Enable automatic updates
    }
  }
}

2. 自动下载 getLatest()

Update Lifecycle

When a new version is detected, if autoUpdate 是启用时:

  1. 下载自动开始
  2. 进度在内部跟踪
  3. 下载失败时会在每次打开应用时自动重试
  4. 成功下载的内容会存储在应用存储中

您可以通过事件监控此过程:

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 是true
  • On下次应用后台时如果 directUpdate is false
  • 安装失败时自动回滚

此插件还自动管理存储:

  • 移除失败的更新如果 autoDeleteFailed is true
  • 清理旧版本如果 autoDeletePrevious is 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'
    }
  ]
});

可用延迟条件:

  • 后台: 应用后台时安装
  • date: 指定日期/时间后安装
  • nativeVersion: 下一次原生更新后安装
  • kill: 应用被杀死后安装

这对以下有用:

  • 在非高峰时段计划更新
  • 与用户活动协调更新
  • 确保平滑的更新体验
  • 在关键任务期间防止中断

更新状态

在自动更新过程中,bundle会通过以下状态:

  1. 下载中: 下载进行中
  2. 等待安装: 下载完成,等待安装
  3. 成功: 更新已安装并激活
  4. 错误: 更新失败(触发自动回滚)

商店合规

Apple App Store

Live Updates完全符合苹果App Store政策。根据苹果开发者计划许可协议规定:

“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内容,因此符合这些允许的指南。

最佳实践

  1. 阶段性发布:逐步发布更新
  2. 版本控制:跟踪所有部署的版本
  3. 回滚支持:快速恢复问题
  4. 差分更新:仅下载更改的文件

何时使用实时更新

适合:

  • 修复bug
  • UI改进
  • 内容更新
  • 功能开关

不适合:

  • 原生code更改
  • 重大版本更新
  • 需要原生更改的安全补丁

Keep going from How Live Updates Work in Capgo

如果您正在使用 How Live Updates Work in Capgo 来规划原生插件工作,连接它与 Capgo插件目录 Capgo插件目录中的产品工作流程 Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, 查看__CAPGO_KEEP_0__插件 __CAPGO_KEEP_1__中的实现细节, Ionic Enterprise Plugin Alternatives 为 Ionic Enterprise Plugin Alternatives 的产品工作流程 Capgo 原生构建 为 Capgo 原生构建 的产品工作流程

实时更新 Capacitor 应用

当 web 层 bug 生效时,通过 Capgo 发布修复,而不是等待几天的 app 店审批。用户在后台接收更新,而原生变化仍在正常审批路径中。

人工支持从 Martin

立即开始

最新博客

Capgo为您提供创建真正专业的移动应用所需的最佳见解。