Android 为什么需要未对齐的 apk?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22058210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why unaligned apk is needed?
提问by Youngjae
Android gradle produces apk in two binaries: unaligned and aligned.
Android gradle 以两个二进制文件生成 apk:未对齐和对齐。
The documentsaid...
该文件说...
Once you have signed the APK with your private key, run zipalign on the file. This tool ensures that all uncompressed data starts with a particular byte alignment, relative to the start of the file. Ensuring alignment at 4-byte boundaries provides a performance optimization when installed on a device. When aligned, the Android system is able to read files with mmap(), even if they contain binary data with alignment restrictions, rather than copying all of the data from the package. The benefit is a reduction in the amount of RAM consumed by the running application.
使用私钥对 APK 进行签名后,对文件运行 zipalign。此工具可确保所有未压缩的数据以特定的字节对齐开始,相对于文件的开头。安装在设备上时,确保在 4 字节边界对齐可提供性能优化。对齐后,Android 系统能够使用 mmap() 读取文件,即使它们包含具有对齐限制的二进制数据,而不是从包中复制所有数据。好处是减少了正在运行的应用程序消耗的 RAM 量。
Seems like aligned apk is strongly recommended to distribute. For me, I only use alignedapk as a result product and ignore unalignedapk.
似乎强烈建议分发对齐的 apk。对我来说,我只使用对齐的apk 作为结果产品而忽略未对齐的apk。
Does unaligned apk have any special usage during development?
未对齐的apk在开发过程中有什么特殊用途吗?
回答by flx
It is a two step process. The unaligned apk is just an intermediate product.
这是一个两步过程。未对齐的 apk 只是一个中间产品。
- the unaligned apk is generated
- the unaligned gets aligned and produces the aligned apk
- 生成未对齐的 apk
- 未对齐的对齐并生成对齐的 apk
回答by SMR
The unaligned(signed) APK is needed because signing an aligned apk will undo the alignment.
需要未对齐(已签名)的 APK,因为签署对齐的 apk 将撤消对齐。
From the docs:
从文档:
Caution:zipalign must only be performed afterthe .apk file has been signed with your private key. If you perform zipalign before signing, then the signing procedure will undo the alignment.
注意:zipalign 只能在 .apk 文件使用您的私钥签名后执行。如果您在签名之前执行 zipalign,那么签名过程将撤消对齐。
See this answerfor more. Hereis the detailed build process: