Sunday, October 4, 2009

Aligning resources in Android Packages( using zipalign)

Zipaling: 
The zipalign tool was added to the SDK in version 1.6. Zipalign optimizes the way the application is packed, this optimizes the way android interacts with your application and may increase the performance of an applciation. 

Why Zipaling:
 The purpose of zipalign is to ensure that all uncompressed data starts with a particular alignment relative to the start of the application file. Specifically, it causes all uncompressed data within the .apk such as images or raw files to be aligned on 4-byte boundaries. This allows for all portions of the apk file to be accessed directly by memory-mapping them with a function known as mmap(), rather than copying all of the data out of them. The mmap() function establishes a mapping between a process' address space and a file, shared memory or typed memory object, in this case the .apk file. The result of this is that you can get a reduction in the amount of RAM that your applications consume when they run. If the resources are not aligned, Android has to explicitly read them, which is slower and also consume more memory.

For end users using unaligned application can make the Home application and the unaligned application launch slower than they otherwise should. The worst case, installing several applications with unaligned resource will increase the memory pressure, and cause the system to suffer by having constantly having to start and kill processes. This will have the user end up with a slow device with poor battery life.

Zipalign and signing:
 Zipalign must be run after the application, this is because the signing the application after aligning will disrupt the package alignment. As a result of this all of the "automatic integrated alignment" tools do not align packages until they can sign it. 

How do I use it?   
ADT:
If you are developing with Eclipse and using the ADT plug-in, the zipalign tool is integrated into the "Export Wizard". When you select "Export signed application package", ADT signs and automatically runs zipalign on the exported package. If you choose "Export unsigned application package" it won't be aligned due to reasons mentioned above, so if you want to align it, you must sign and align it manually. If you're wondering where the "Export" functions is, it can be found at the first page of the AndroidManifest.xml editor as well as in the "Android tools"  menu selection in the projects context menu. 

Ant:
With the Ant build script that targets the newest Android API (1.6, API Level 4) have the ability to align the application packages. Older ant build scripts that target older versions of the Android platform will not be aligned and will need to be aligned manually. Debug packages that are built with Ant with 1.6 as a target are aligned and signed by default. Release packages are aligned automatically if Ant has enough information to sign the packages, this cause the aligning has to happen after signing. So in order to have the application signed, and thereby align them, Ant needs to know the location of the keystore and the name of the key in the build.properties. The name of the properties are key.store and key.alias, if these properties are present the signing tool will ask for the store/key password during the build, and after this the tool will sign and align the apk file. 

Manually:
 To manually align a package, you'll find the zipalign tool in the in the "tools" folder in the Android 1.6 SDK. It can be used on any application package irrelevant of target version. As stated previously this should be done after the package is signed. To use the tool you will need to use the following syntax: 

zipalign [-f] [-v] infile.apk outfile.apk 
-f : overwrite existing outfile.zip
-v : verbose output
-c : confirm the alignment of the given file, in other words verify the alignment
.
Alignment must always be 4, as Android uses 32 bit alignment, if it is anything else this will be as if it was not aligned.

So:
zipalign -v 4 source.apk destination.apk
Will align your android package. 

Verification: 
You can verify that your package has been aligned properly on can: 
zipalign -c -v 4 application.apk 

It is strongly recommended to align your Android applications as it can improve the performance and memory usage, and as it isn't that hard to do I don't see any good reason not to do it.
Happy aligning.
Android documentation on Zipaling.

1 comment:

  1. Is this strictly done by developers or can any DroidX user do this? How does one access the spot to type in the commands?

    ReplyDelete