zipalign
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
「zipalign
」是 ZIP 封存檔對齊工具,可協助確保所有未壓縮的檔案
封存資料
都會根據檔案開頭對齊這可讓您直接透過瀏覽器
mmap(2)
,不再需要複製 RAM 中的資料,並降低應用程式的記憶體用量。
在發布 APK 檔案前,請先使用 zipalign
將 APK 檔案最佳化
而非使用者如果您是透過 Android Studio 使用 Android Gradle 外掛程式 (AGP) 進行建構,
自動完成作業在這種情況下,您仍應使用 zipalign
驗證 APK
但您不需要對齊本說明文件主要供
以及自訂建構系統
注意:您必須使用 zipalign
建構程序實際時機取決於您使用的應用程式簽署工具:
-
如果你使用
apksigner
,zipalign
必須在簽署 APK 檔案之前使用。如果您在
使用 apksigner
的 APK,並在 APK 的後續變更中簽名,簽名就會失效。
-
如果您使用
jarsigner
(不建議) 必須在 APK 檔案含有 zipalign
檔案「之後」使用
已簽署。
為了確實對齊,zipalign
會變更 ZIP「本機檔案標頭」中 "extra"
欄位的大小
專區。這項程序可能也會變更 "extra"
欄位中的現有資料。
用量
如果您的 APK 含有共用資料庫 (.so
檔案),請使用 -P 16
確保它們與適合 mmap(2)
的 16KiB 頁面邊界對齊
支援 16 KiB 與 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 |
覆寫現有的輸出檔案。 |
- 小時 |
顯示工具說明。 |
-P <pagesize_kb> |
將未壓縮的 .so 檔案對齊 KiB 中的指定頁面大小。有效選項
<pagesize_kb> 為 4、16 和 64。 |
-p |
4KiB 頁面對齊未壓縮的 .so 檔案。建議使用
-P 16 ,因為 -p 已淘汰。 |
- V |
詳細輸出模式。 |
- Z |
使用 Zopfli 重新壓縮。 |
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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. |"]]