android學習之路:關於zipalign

https://developer.android.com/studio/command-line/zipalign.html


zipalign is an archive alignment tool that provides important optimization to Android application (APK) files. The purpose is to ensure that all uncompressed data starts with a particular alignment relative to the start of the file. Specifically, it causes all uncompressed data within the APK, such as images or raw files, to be aligned on 4-byte boundaries. This allows all portions to be accessed directly with mmap() even if they contain binary data with alignment restrictions. The benefit is a reduction in the amount of RAM consumed when running the application.

zipalign是一個存檔文件的對齊工具,他能對APK進行最優優化。目的是確保apk中所有未被壓縮的文件都能以一個特定的字節對齊。具體而言他使得所有的圖片和源文件以4字節邊界對齊,這使得我們可以直接使用mmap函數儘管他們有一些二進制對齊限制。好處是減小了RAM使用。

This tool should always be used to align your APK file before distributing it to end-users. The Android build tools can handle this for you. Android Studio automatically aligns your APK.

這個工具用在你發佈應用之前,Android構建工具會幫你處理這些,Android studio可以自動對齊你的apk。

Caution: You must use zipalign at one of two specific points in the app-building process, depending on which app-signing tool you use:

注意:你有2種情況需要使用aipalign,取決於你的簽名工具。

  • If you use apksigner, zipalign must only be performed 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.
  • 如果你使用 apksignerzipalign 必須在你對apk簽名後再使用,如果你用apksigner 簽名後又對apk進行了修改,那麼 簽名是無效的
  • If you use jarsigner, zipalign must only be performed after the APK file has been signed
  • 如果你用 jarsigner, zipalign 需要用在一個已經簽名過的APK。

The adjustment is made by altering the size of the "extra" field in the zip Local File Header sections. Existing data in the "extra" fields may be altered by this process.

這個改變的原因是因爲在zip的頭文件中調整了“extra”字段的大小。在這個過程中現有的“extra”字段的數據可能被改變。

For more information about how to use zipalign when building your application, please read Signing Your Application.

要了解更多zipalign 請去Signing Your Application.

Usage

To align infile.apk and save it as outfile.apk:

對齊infile.apk 保存爲outfile.apk:

zipalign [-f] [-v] <alignment> infile.apk outfile.apk

To confirm the alignment of existing.apk:

檢查apk是否已經使用過zipalign 

zipalign -c -v <alignment> existing.apk

The <alignment> is an integer that defines the byte-alignment boundaries. This must always be 4 (which provides 32-bit alignment) or else it effectively does nothing.

alignment 是一個int值,定義字節對齊的邊界,必須是4(提供32位對齊),否則無效

Flags:

  • -f : overwrite existing outfile.zip
  • -f:重寫已經存在的outfile.zip
  • -v : verbose output
  • -v : 輸出詳細信息
  • -p : outfile.zip should use the same page alignment for all shared object files within infile.zip
  • -p :outfile.zip應該對於infile.zip中的文件使用相同的頁面調整方式
  • -c : confirm the alignment of the given file
  • -c : 確認文件的對齊方式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章