应用包更新App package updates

新式 Windows 应用包的更新方式经过优化,可确保仅下载最重要的已更改应用位来更新现有的 Windows 应用。Updating modern Windows app packages is optimized to ensure that only the essential changed bits of the app are downloaded to update an existing Windows app.

AppxBlockMap.xml 文件中的元数据Metadata in the AppxBlockMap.xml file

从较高层面看,在创建包的过程中,将会创建元数据片段并将其存储在应用包文件(.appx 或 .msix)中,使 Windows 能够唯一标识包的组成部分。At a high level, during package creation, a piece of metadata is created and stored in the app package file (.appx or .msix) which allows parts of the package to be uniquely identified by Windows. 更新应用包时,Windows 将使用元数据文件来比较旧包与新包,并确定需要将哪些内容下载到设备。When updating an app package, Windows uses the metadata file to compare the old package to the new package and determine what needs to be downloaded to the device.

由于使用元数据可以唯一标识包的组成部分,因此这意味着,差异更新机制完全可以在不同版本的包中正常运行(假设源包的版本低于目标包)。Since the metadata allows parts of the package to be uniquely identified, this means the differential update machinery fully functions from any version of a package to any other version of a package (assuming the source package has a lower version than the target package).

元数据包含在 AppxBlockMap.xml 文件中(前面所述的元数据)。The metadata is contained in the AppxBlockMap.xml file (the aforementioned metadata). AppxBlockMap.xml 文件是一个 XML 文件,其中包含有关包中文件的信息的二维列表。The AppxBlockMap.xml file is an XML file that contains a two dimensional list of information about files in the package. 第一维列出有关文件的高级别详细信息(例如名称和大小),第二维提供该文件的每个 64KB 切片的 SHA2-256 哈希表示形式(“块”)。The first dimension lays out high level details on the file (e.g., name and size) and the second dimension provides SHA2-256 hash representations of each 64KB slice of that file (the "block").

下面是 AppxBlockMap.xml 文件的示例。Here's a sample of an AppxBlockMap.xml file.

<!--?xml version="1.0" encoding="UTF-8"?-->
<blockmap hashmethod="http://www.w3.org/2001/04/xmlenc#sha256" 
          xmlns="http://schemas.microsoft.com/appx/2010/blockmap">
  <file lfhsize="66" size="101188" name="asset1.jpg">
    <block hash="2bidNE0JyaO+FjaTpRe0g8HzUCblUf/cfBcTXiZR74c="/>
    <block hash="+jeFwKrGk5gw9wSICWsWRtEQXwcLC7af4EWS7DgrAkY="/>
  </file>
  <file lfhsize="61" size="108823" name="asset2.jpg">
    <block hash="u0+5S0GOzwyAfYx54tKycZyHRBYm2ybvq27dkIKqDsQ="/>
    <block hash="F9h0FRMetL6BNCszAYB0bgyx2KWN+dO1bls4Q9m267c="/>
  </file>
  ...
</blockmap>

第一个文件 (asset1.jpg) 包含两个块哈希。 第一个哈希表示该文件的第一个 64KB 块,第二个哈希表示剩余的 35KB - 假设文件大小为 101188 字节。The first hash represents the first 64KB block of the file and the second hash represents the remaining 35KB - given the file is 101188 bytes.

在更新期间,如果该文件的第二个块已修改,则哈希也会更新以反映此操作。During an update, if the second block of that file is modified, the hash is also updated to reflect that. 下载组件提取第二个块,并重复使用旧包中第一个未经更改的块。The download component pulls down the second block and reuses the first unchanged block from the old package.

操作规模更大时,如果整个文件未更改(根据整组不变化的块确定),则可以从现有包中重复使用该文件,从而节省时间和资源。On a larger scale, if an entire file does not change (determined by a full set of blocks not changing), that file can be reused from the existing package, saving time and resources.

应用更新约束App update constraints

更新是在相同的包系列内部执行的Updates are performed within the same package family

包系列由包名称和发布者构成。The package family is comprised of the Package Name and Publisher. 为了能够更新,新包元数据需要与以前安装的包相同。To be able to update, the new package metadata will need to be the same as the previously installed package.

应用更新必须递增到更高版本App updates must increment to a higher version

一般情况下,应用更新要求新包的版本高于当前版本。In general, app updates require the version of the new package to be higher than the current one. 默认情况下,应用更新进程不允许安装版本更低的包。The app update process will not allow packages with lower versions to be installed by default. 自 Windows 10 版本 1809 起,当提供重写开关作为更新参数的一部分时,可以使用 ForceUpdateToAnyVersion 允许安装更低版本的包。Starting in Windows 10 version 1809, you can use ForceUpdateToAnyVersion to allow lower version packages to be installed when an override switch is provided as part of the update arguments. 目前可以在 PowerShell 中使用 ForceUpdateFromAnyVersion 选项、通过 PackageManager APIEnterpriseModernAppManagement CSP,以及在 AppInstaller 文件中执行此操作。It is currently available in PowerShell using the ForceUpdateFromAnyVersion option, via PackageManager API, EnterpriseModernAppManagement CSP and in the AppInstaller file.

备注

如果对来自 Windows Store 的应用使用 ForceUpdateToAnyVersion,Windows 更新会自动将该应用更新到最新的适用版本。If you use ForceUpdateToAnyVersion on an app from the Windows Store, Windows Update will automatically update the to the latest applicable version.

应用更新包可以采用不同的体系结构App update package can have a different architecture

只要部署到的 OS 支持新体系结构,则更新包与当前安装的应用包可以采用不同的体系结构。The update package to the currently installed app package can be of a different architecture as long as the new architecture is supported on the OS where it is being deployed to. 例如:如果在 x64 Windows 10 设备上安装了 x86 版本的 MyFavApp (v1.0.0.0),并且更新包 (v2.0.0.0) 采用 x64 版本:MyFavApp (1.0.0.0) 将成功更新到 MyFavApp (v2.0.0.0)。For example: If you have x86 version of MyFavApp(v1.0.0.0) installed on a x64 Windows 10 device and the update package(v2.0.0.0) is x64 version: MyFavApp(1.0.0.0) will be updated to MyFavApp(v2.0.0.0) successfully.

可以从 MSIX 将包更新到 MSIXbundlePackages can update from an MSIX to an MSIXbundle

可以从 MSIX 包更新到 MSIXbundle 包,但反之不然。An update package can go from MSIX package to an MSIXbundle package but not vice-versa. 安装 MSIXbundle 时,包更新需要保留一个捆绑包。When an MSIXbundle is installed, the package update will need to remain a bundle.

优化差异更新技术Optimize differential update technology

可通过多种方式最大程度地优化差异更新技术。There are a few ways to ensure that the differential update technology is optimized to the max.

  • 在包中保留小文件 - 这可以确保在需要执行会影响整个文件的更改时,更新规模仍然很小。Keep files in the package small - doing this will ensure that if a change is needed that would impact the full file, the update would still be small.
  • 应该尽可能地对文件进行加法式更改 - 加法式更改可确保最终用户设备仅下载已更改的块。Changes to files should be additive if possible - additive changes will ensure that end-user devices only download those changed blocks.
  • 应该尽可能地将文件更改包含在 64KB 块中 - 如果应用包含大文件并且需要对文件的中间部分进行更改,则在一组块中包含更改可以提供显著的帮助。Changes to files should be contained to 64KB blocks if possible - if your app does have large files and requires changes to the middle of a file, containing changes to a set of blocks will help significantly.