Home » Android » android – why unaligned apk is needed?

android – why unaligned apk is needed?

Posted by: admin March 10, 2020 Leave a comment

Questions:

Android gradle produces apk in two binaries: unaligned and aligned.

The document said…

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.

Seems like aligned apk is strongly recommended to distribute. For me, I only use aligned apk as a result product and ignore unaligned apk.

Does unaligned apk have any special usage during development?

How to&Answers:

It is a two step process. The unaligned apk is just an intermediate product.

  1. the unaligned apk is generated
  2. the unaligned gets aligned and produces the aligned apk

Answer:

The unaligned(signed) APK is needed because signing an aligned apk will undo the alignment.

From the docs:

Caution: zipalign must only be performed after the .apk file has been signed with your private key. If you perform zipalign before signing, then the signing procedure will undo the alignment.

See this answer for more. Here is the detailed build process:
enter image description here