zipalign
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
zipalign
是一种 zip 归档文件对齐工具,有助于确保归档文件中的所有未压缩文件相对于文件开头对齐。这样一来,您便可直接通过 mmap(2)
访问这些文件,而无需在 RAM 中复制这些数据并减少了应用的内存用量。
在将 APK 文件分发给最终用户之前,先使用 zipalign
进行优化。如果您通过使用 Android Gradle 插件 (AGP) 的 Android Studio 进行构建,系统会自动完成此操作。在这种情况下,您仍应使用 zipalign
来验证 APK 是否已对齐,但您无需对齐。本文档主要适用于自定义构建系统的维护者。
注意:您必须在构建流程的特定时间点使用 zipalign
。该时间点取决于您使用的应用签名工具:
-
如果您使用的是
apksigner
,则必须在为 APK 文件签名之前使用 zipalign
。如果您在使用 apksigner
为 APK 签名之后对 APK 做出了进一步更改,签名便会失效。
-
如果您使用的是
jarsigner
(不推荐),则必须在为 APK 文件签名之后使用 zipalign
。
为了实现对齐,zipalign
会更改 zip 本地文件标头部分中 "extra"
字段的大小。此过程还会更改 "extra"
字段中的现有数据。
用法
如果您的 APK 包含共享库(.so
个文件),请使用 -P 16
以确保它们与适合 mmap(2)
的 16KiB 页面边界对齐
在 16KiB 和 4KiB 设备中。对于其他文件,其对齐方式由
zipalign
的强制性对齐参数,应按 4 个字节对齐
在 32 位和 64 位系统上运行
如需对齐 infile.apk
并将其保存为 outfile.apk
,请运行以下命令:
zipalign -P 16 -f -v 4 infile.apk outfile.apk
如需确认 existing.apk
的对齐方式,请使用以下命令。
zipalign -c -P 16 -v 4 existing.apk
选项
下表列出了可用的 zipalign
选项:
选项 |
说明 |
-c |
仅检查对齐情况(不会修改文件)。 |
-f |
覆盖现有输出文件。 |
-h |
显示工具帮助。 |
-P <页面大小_kb> |
将未压缩的 .so 文件对齐到指定的页面大小(以 KiB 为单位)。有效选项
为 4、16 和 64。<pagesize_kb> |
-p |
4KiB 页面对齐未压缩的 .so 文件。建议使用
-P 16 ,因为 -p 已废弃。 |
-v |
详细输出。 |
-z |
使用 Zopfli 重新压缩。 |
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# zipalign\n\n`zipalign` is a zip archive alignment tool that helps ensure that all uncompressed files\nin the archive\nare aligned relative to the start of the file. This lets the files be accessed directly via\n[mmap(2)](https://man7.org/linux/man-pages/man2/mmap.2.html)`\n`, removing the need to copy this data in RAM and reducing your app's memory usage.\n\nUse `zipalign` to optimize your APK file before distributing it to\nend users. If you build using Android Studio, which uses the Android Gradle plugin (AGP), this is\ndone automatically. In this case, you should still use `zipalign` to verify that the APK\nis aligned, but you don't need to align it. This documentation is mainly for maintainers of\ncustom build systems. \n\n**Caution:** You must use `zipalign` at a specific point in\nthe build process. That point depends on which app-signing tool you use:\n\n- If you use [apksigner](/studio/command-line/apksigner), `zipalign` must be used **before** the APK file has been signed. If you sign your APK using `apksigner` and make further changes to the APK, its signature is invalidated.\n- If you use [jarsigner](//docs.oracle.com/javase/tutorial/deployment/jar/signing.html) (not recommended), `zipalign` must be used **after** the APK file has been signed.\n\nTo achieve alignment, `zipalign` alters the size of the `\"extra\"` field in the zip **Local File Header**\nsections. This process can also alter existing data in the `\"extra\"` fields.\n\nUsage\n-----\n\nIf your APK contains shared libraries (`.so` files), use `-P 16`\nto ensure that they're aligned to a 16KiB page boundary suitable for `mmap(2)`\nin both 16KiB and 4KiB devices. For other files, whose alignment is determined by the\nmandatory alignment argument to `zipalign`, should be aligned to 4 bytes\non both 32-bit and 64-bit systems.\n\nTo align `infile.apk` and save it as `outfile.apk`: \n\n```\nzipalign -P 16 -f -v 4 infile.apk outfile.apk\n```\n\nTo confirm the alignment of `existing.apk`, use the following command. \n\n```\nzipalign -c -P 16 -v 4 existing.apk\n```\n\n### Options\n\nThe following table lists the available `zipalign` options:\n\n| Option | Description |\n|--------------------|-------------------------------------------------------------------------------------------------------------------------|\n| -c | Checks alignment only (does not modify file). |\n| -f | Overwrites existing output file. |\n| -h | Displays tool help. |\n| -P \\\u003cpagesize_kb\\\u003e | aligns uncompressed `.so` files to the specified page size in KiB. Valid options for `\u003cpagesize_kb\u003e` are 4, 16, and 64. |\n| -p | 4KiB page-aligns uncompressed `.so` files. It is recommended to use `-P 16` instead, as `-p` is deprecated. |\n| -v | Verbose output. |\n| -z | Recompresses using Zopfli. |"]]