8

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?

Corey Ogburn
  • 21,622
  • 25
  • 107
  • 169

1 Answers1

3

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

Community
  • 1
  • 1
cjds
  • 7,573
  • 9
  • 41
  • 75
  • It's not about page alignment, it's about word alignment. If you want page alignment, you'd configure it to align on a 4096-byte boundary rather than a 4-byte boundary. cf. http://stackoverflow.com/questions/30155519/ – fadden Nov 05 '15 at 00:01