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 : 确认文件的对齐方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值