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