Android provides developers the zipalign tool to align your .apk file before distributing to end-users. The tool not only provides better optimization but also ensures that that all uncompressed data starts with a particular alignment relative to the start of the file. It is important to note that the 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.
Through this post, we will learn how to use the zipalign tool in Android.
Pre-requisites: Eclipse IDE, Android SDK
How to export signed apk?
Launch Eclipse IDE and switch to your Android project workspace. Choose any of your existing Android projects for which you need to zipalign the .apk. Before generating the signed apk for your Android project, it is necessary to follow the below steps as per the official documentation for signing an application.
1. Check the AndroidManifest.xml and verify that android:debuggable attribute is set to false. The attribute indicates whether or not the application can be debugged, even when running on a device in user mode.
2. Check the android:versionCode and android:versionName attributes. If it is a new version of an existing apk, make sure these values are larger than the previous apk file.
3. Finally, right click your application, go to Android Tools -> Export Signed Application Package and sign the application using a release key certificate (not debug) as mentioned over here.
Once you have obtained the signed apk, you can use the zipalign tool for optimization. The following command converts the signed apk to a zipalign signed apk that can be uploaded on the Google Play Store.
zipalign -v 4 AndroidRandomExamples.apk AndroidRandomExamples_aligned.apk
zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk
The output of the zipalign process is as follows!
Reference: Android ZipAlign