1. Introduction to zipalign
The Android SDK includes a "zipalign" tool that optimizes packaged applications. Running zipalign on your application makes the interaction between Android and the application more efficient at runtime. Therefore, this method can make the application and the whole system run faster. We strongly recommend using the zipalign tool on new and existing programs to get optimized versions.
According to the description of the official documentation, the application data in the Android system is stored in its APK file and can be accessed by multiple processes at the same time. The installation process includes the following steps:
1. The Installer obtains the permissions information associated with the current application through the manifest file of each apk.
2. The Home application reads the information such as the Name and Icon of the current APK.
3. The System server will read some information related to the operation of the Application, such as: obtaining and processing the Notifications request of the Application, etc.
4. Finally, the content contained in the APK is not limited to the use of the current Application, but can also be called by other Applications to improve the reusability of system resources.
The most fundamental purpose of zipalign optimization is to help the operating system to index resources more efficiently according to requests, and to uniformly limit the resource-handling code to Data structure alignment (Data Structure Alignment Standard: DSA) to 4-byte boundaries. If you are new to data structure alignment, it is highly recommended to search for more related content to fully understand the ultimate purpose of doing so, which is also the key to understanding how zipalign works. If the alignment standard is not adopted, the processor cannot accurately and quickly locate the relevant resources in the memory address.
2. How to use
Method 1: Use the command line
1. Find the zipalign.exe file in the tools folder of the Android SDK .2. Copy the apk you want to optimize to the tools folder you extracted.
Start -> Run -> CMD to bring up the command line window
Enter the path of the unzipped folder in the command line\zipalign -v 4 The name of the apk you want to optimize.apk The name of the apk after optimization.apk
例如C:\Windows\android-sdk-windows\tools\zipalign -v 4 Example.apk Example.1.apk
Where -v stands for verbose output, and 4 stands for alignment to 4 bytes.
Method 2: Configure in build.gradle
buildTypes { release { // Zipalign 优化 zipAlignEnabled true } debug { // Zipalign 优化 zipAlignEnabled false } }
3. zipalign parameters
Environment configuration (configured zipalign
environment variables)
if you are not usingohmyzsh
~ vim ~/.bashrc
- 1
If usingohmyzsh
vim ~/.zshrc
- 1
Write the third path below, remember that the previous directory is the path of your own sdk, don't copy it directly
#Android sdk
export ANDROID_HOME=/Users/guoyoujin/Library/Android/sdk
export PATH=$PATH:$HOME/Library/Android/sdk/tools:$HOME/Library/Android/sdk/platform-tools
export PATH=$PATH:$HOME/Library/Android/sdk/build-tools/24.0.3
#GOROOT
- 1
- 2
- 3
- 4
- 5
- 6
Last reload modified file
source ~/.bashrc or source ~/.zshrc
- 1
Enter zipalign to view details
➜ TxCustomerServices git:(v1.16.1) ✗ zipalign
Zip alignment utility
Copyright (C) 2009 The Android Open Source Project
Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip
zipalign -c [-v] <align> infile.zip
<align>: alignment in bytes, e.g. '4' provides 32-bit alignment
-c: check alignment only (does not modify file)
-f: overwrite existing outfile.zip
-p: page align stored shared object files
-v: verbose output
-z: recompress using Zopfli
Simple to use
The simple description of the official website is like this, I believe everyone can understand
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.
Flags:
-f : overwrite existing outfile.zip
-v : verbose output
-p : outfile.zip should use the same page alignment for all shared object files within infile.zip
-c : confirm the alignment of the given file
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Align package resources
zipalign -f -v 4 infile.apk outfile.apk
- 1
Compare whether the apk is aligned
zipalign -c -v 4 outfile.apk
- 1
The output Verification succesful
indicates that it has been compared
zipalign ;
-c : Check if the .apk file is zipalign optimized
-f : overwrite existing files
-p : the page stores object files for
-v : output optimized details xx.apk
-z : Zopfli will be used
zipalign -v 4 source.apk destination.apk mine