Home | History | Annotate | Download | only in articles
      1 page.title=Zipalign: an Easy Optimization
      2 parent.title=Articles
      3 parent.link=../browser.html?tag=article
      4 @jd:body
      5 
      6 <p>The Android SDK includes a tool called <a
      7 href="{@docRoot}guide/developing/tools/zipalign.html"><code>zipalign</code></a>
      8 that optimizes the way an application is packaged. Running zipalign against your
      9 application enables Android to interact it more efficiently at run time and thus
     10 has the potential to make it and the overall system run faster. We strongly
     11 encourage you to use <code>zipalign</code> on both new and already published
     12 applications and to make the optimized version available &mdash; even if your
     13 application targets a previous version of Android. This article describes how
     14 <code>zipalign</code> helps performance and how to use it to optimize your
     15 app.</p>
     16 
     17 <p>In Android, data files stored in each application's apk are accessed by
     18 multiple processes: the installer reads the manifest to handle the
     19 permissions associated with that application; the Home application
     20 reads resources to get the application's name and icon; the system
     21 server reads resources for a variety of reasons (e.g. to display that
     22 application's notifications); and last but not least, the resource
     23 files are obviously used by the application itself.</p>
     24 
     25 <p>The resource-handling code in Android can efficiently access resources when
     26 they're aligned on 4-byte boundaries by memory-mapping them. But for resources
     27 that are not aligned (that is, when <code>zipalign</code> hasn't been run on an
     28 apk), it has to fall back to explicitly reading them &mdash; which is slower and
     29 consumes additional memory.</p>
     30 
     31 <p>For an application developer, this fallback mechanism is very
     32 convenient. It provides a lot of flexibility by allowing for several
     33 different development methods, including those that don't include
     34 aligning resources as part of their normal flow.</p>
     35 
     36 <p>Unfortunately, for users the situation is reversed &mdash; reading resources
     37 from unaligned apks is slow and takes a lot of memory. In the best case, the
     38 only visible result is that both the Home application and the unaligned
     39 application launch slower than they otherwise should. In the worst case,
     40 installing several applications with unaligned resources increases memory
     41 pressure, thus causing the system to thrash around by having to constantly start
     42 and kill processes. The user ends up with a slow device with a poor battery
     43 life.</p>
     44 
     45 <p>Luckily, it's very easy for you to align the resources in your application:</p>
     46 
     47 <ul>
     48 <li>Using ADT:</li>
     49 <li style="list-style-type: none; list-style-image: none; list-style-position: outside;">
     50 <ul>
     51 <li>The ADT plugin for Eclipse (starting from version 0.9.3) will automatically
     52 align release application packages if the export wizard is used to create them.
     53 To use the wizard, right click the project and choose "Android Tools" &gt;
     54 "Export Signed Application Package..." It can also be accessed from the first
     55 page of the <code>AndroidManifest.xml</code> editor.</li>
     56 </ul>
     57 </li>
     58 <li>Using Ant:</li><li style="list-style-type: none; list-style-image: none; list-style-position: outside;">
     59 
     60 <ul>
     61 <li>The <em>Ant</em> build script (starting from Android 1.6) can align
     62 application packages. Targets for older versions of the Android platform are not
     63 aligned by the <em>Ant</em> build script and need to be manually aligned.</li>
     64 <li>Starting from the Android 1.6 SDK, Ant aligns and signs packages automatically, 
     65 when building in debug mode.</li>
     66 <li>In release mode, Ant aligns packages only if it has enough
     67 information to sign the packages, since aligning has to happen after signing. In
     68 order to be able to sign packages, and therefore to align them, <em>Ant</em>
     69 needs to know the location of the keystore and the name of the key in
     70 <code>ant.properties</code>. The name of the properties are
     71 <code>key.store</code> and <code>key.alias</code> respectively. If those
     72 properties are present, the signing tool will prompt to enter the store/key
     73 passwords during the build, and the script will sign and then align the apk
     74 file. If the properties are missing, the release package will not be signed, and
     75 therefore will not get aligned either.</li>
     76 </ul>
     77 </li>
     78 <li>Manually:</li>
     79 <li style="list-style-type: none; list-style-image: none; list-style-position: outside;">
     80 <ul>
     81 <li>In order to manually align a package, <code>zipalign</code>
     82 is in the <code>tools/</code> folder of Android 1.6 and later SDKs. You can use
     83 it to align application packages targeting any version of Android. You should run
     84 it only after signing the apk file, using the following command:
     85 <br><code>zipalign -v 4 source.apk destination.apk</code></li>
     86 </ul>
     87 </li>
     88 <li>Verifying alignment:</li>
     89 <li style="list-style-type: none; list-style-image: none; list-style-position: outside;">
     90 <ul>
     91 <li>The following command verifies that a package is aligned:<br><code>zipalign -c -v 4 application.apk</code>
     92 </li>
     93 </ul>
     94 </li>
     95 </ul>
     96 
     97 <p>We encourage you manually run <code>zipalign</code>
     98 on your currently published applications and to make the newly aligned
     99 versions available to users. Also, don't forget to align any new
    100 applications going forward!</p>
    101