Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I ever use unaligned apks?

When I run gradle task "assembleDebug" for just getting a debug release I put on my phone it also generates another apk: MyApp-debug-unaligned.apk.

I think I understand what "alignment" of a zip means. That it has optimized placement of file boundaries for easy unzipping (correct me if I'm wrong). It's just an optimization and really doesn't have much to do with Android specifically.

So, since Android keeps all apps as apks and only seems to unzip them at run time, it would benefit to only install the aligned, optimized apks. It also takes a seemingly trivial amount of time to zip-align the package, but maybe that's just due to the size of my particular apps.

When would an unaligned zip be beneficial over it's aligned alternative? Or is it just because you have to have an unaligned version to align and the process doesn't clean up the unaligned file after it's done?

like image 406
Corey Ogburn Avatar asked Nov 04 '15 19:11

Corey Ogburn


1 Answers

You would Never use an unaligned APK.

It's an intermediate product that isn't cleaned up. In my opinion it should be.

How It works:

What aligning does is it puts images and other large bits of uncompressed data on a 4 byte boundary. This increases the file size but causes them to belong to a certain page. It avoids having to pick up multiple pages from the APK for a single image (that is it minimizes the number of pages picked up). Since the image begins on a 4 byte boundary, there is a higher chance we will not pick up junk data, that is related to other processes.

This in the end allows me to waste less RAM and run faster, by picking up less pages. A trivial but good optimization

About the time it takes, it is relatively trivial, so it is worth it. Obviously, the more uncompressed data you have the more time it takes but it never is very significant. IMHO the compiler should throw away the unaligned file but I guess someone wanted to keep it.

Resources:

Announcement of ZipAlign

http://android-developers.blogspot.com/2009/09/zipalign-easy-optimization.html

Current ZipAlign Docs

http://developer.android.com/tools/help/zipalign.html

About Data Structure Alignment (read about padding)

https://en.wikipedia.org/wiki/Data_structure_alignment

like image 198
cjds Avatar answered Oct 18 '22 21:10

cjds