What is Zipalign?
Zipalign is a tool for organizing APK files on the android platform. It was first introduced in the SDK software development kit of Android 1.6. It can optimize the packaged Android Application to make the interaction between the Android operating system and the application more efficient, which allows the application and the entire system to run faster. The execution time of applications processed with Zipalign reaches the minimum. When the device runs the APK application, it occupies less RAM (Random Access Memory) to randomly Access the Memory, we strongly recommend that you use the zipalign tool on new and released programs to get the optimized version, even if your program is developed on the old Android platform. This article describes how zipalign runs and how to use it to optimize your app.
How can Zipalign be optimized?
Zipalign is aligned on the 4-byte boundary of uncompressed data in the apk file. When the resource file is aligned to the 4-byte boundary through memory ing, the code used to access the resource file is efficient. After the 4-byte alignment, the android system can call the mmap function to read files. The process can perform operations on common files like the read/write memory, and the system shares the memory IPC, in order to obtain high performance in reading resources. If the resource itself is not aligned, it must read them explicitly-this process will be slow and will consume additional memory.
Mmap system calls enable shared memory between processes by ing the same common file. After a common file is mapped to the process address space, the process can access the file like accessing the common memory without calling read (), write (), and other operations.
Mmap is widely used in programs, and mmap is used to access files like accessing common memory. To frequently access a file and move the pointer back and forth, calling mmap is much faster than using the conventional method.
Alignment on the boundary of four bytes means that the compiler reads 4 bytes as a unit. In this case, the CPU can perform efficient and fast access to variables (not aligned before ).
In the android system, the Davlik virtual machine uses its proprietary DEX format. The DEX structure is compact. In order to improve the running performance, you can further optimize it with alignment, but the size usually increases.
In an apk that is not aligned with each other, reading resources is slow and consumes a large amount of memory. The best case is that the Home Program and the unaligned program start slowly than the aligned one (which is also the only visible effect ). The worst case is that installing apps with UN-aligned resources increases the memory pressure and causes the system to repeatedly start and kill processes. Eventually, the user gives up using devices that are so slow and consume power.
How to Use Zipalign?
Use ADT:
- If you use the export wizard, the ADT plug-in Eclipse (starting with Ver. 0.9.3) will automatically align the Release package. Use the wizard, right-click Project properties, and choose "Android Tools"> "Export Signed Application Package ...". You can also use the first page of the AndroidManifest. xml editor.
Use Ant:
- The Ant compilation script (starting from Android 1.6) can align the package. Versions of the old platform cannot be aligned using the Ant compilation script, and must be aligned manually.
- Ant automatically alignment and signature packages during compilation in Debug mode from Android 1.6.
- In Release mode, Ant performs alignment only if there is sufficient information signature package, because alignment processing occurs after the signature. To sign the package and perform the alignment operation, Ant must know the location of the keystore and the name of the key in build. properties. The corresponding attributes are key. store and key. alias. If these attributes are empty, the signature tool prompts you to enter the store/key password during the compilation process. Then, the script executes the signature and alignment of the apk file. If none of these attributes exist, the Release package will not be signed and will naturally not be aligned.
Manual:
- To manually align the package, zipalign is available in the tools/folders of Android 1.6 and later sdks. You can use it to align packages of any version. You must sign the apk file and run the following command: zipalign-v 4 source.apk destination.apk
- Verify alignment:
- Run the following command to check whether the package is aligned: zipalign-c-v 4 application.apk