Home | History | Annotate | Download | only in publishing
      1 page.title=Signing Your Applications
      2 @jd:body
      3 
      4 <div id="qv-wrapper">
      5 <div id="qv">
      6 
      7 <h2>Quickview</h2>
      8 
      9 <ul>
     10 <li>All Android apps <em>must</em> be signed</a></li>
     11 <li>You can sign with a self-signed key</li>
     12 <li>How you sign your apps is critical &mdash; read this document carefully</li>
     13 <li>Determine your signing strategy early in the development process</li>
     14 </ul>
     15 
     16 <h2>In this document</h2>
     17 
     18 <ol>
     19 <li><a href="#overview">Overview</a></li>
     20 <li><a href="#strategies">Signing Strategies</a></li>
     21 <li><a href="#setup">Basic Setup for Signing</a></li>
     22 <li><a href="#debugmode">Signing in Debug Mode</a></li>
     23 <li><a href="#releasemode">Signing for Public Release</a>
     24     <ol>
     25     <li><a href="#cert">Obtain a suitable private key</a></li>
     26     <li><a href="#releasecompile">Compile the application in release mode</a></li>
     27     <li><a href="#signapp">Sign your application with your private key</a></li>
     28     <li><a href="#align">Align the final APK package</a></li>
     29     <li><a href="#ExportWizard">Compile and sign with Eclipse ADT</a></li>
     30     </ol>
     31 </li>
     32 <li><a href="#secure-key">Securing Your Private Key</a></li>
     33 
     34 </ol>
     35 
     36 <h2>See also</h2>
     37 
     38 <ol>
     39 <li><a href="{@docRoot}guide/publishing/versioning.html">Versioning Your Applications</a></li>
     40 <li><a href="{@docRoot}guide/publishing/preparing.html">Preparing to Publish</a></li>
     41 </ol>
     42 
     43 </div>
     44 </div>
     45 
     46 <p>This document provides information about signing your Android applications prior to publishing them for mobile device users.</p>
     47 
     48 <h2 id="overview">Overview</h2>
     49 
     50 <p>The Android system requires that all installed applications be digitally
     51 signed with a certificate whose private key is held by the application's
     52 developer. The Android system uses the certificate as a means of identifying the author of
     53 an application and establishing trust relationships between applications. The certificate is not
     54 used to control which applications the user can install. The certificate
     55 does not need to be signed by a certificate authority: it is perfectly
     56 allowable, and typical, for Android applications to use self-signed
     57 certificates.</p>
     58 
     59 <p>The important points to understand about signing Android applications are:</p>
     60 
     61 <ul>
     62   <li>All applications <em>must</em> be signed. The system will not install an application 
     63 that is not signed.</li>
     64   <li>You can use self-signed certificates to sign your applications. No certificate authority 
     65 is needed.</li>
     66   <li>When you are ready to release your application for end-users, you must sign it with a suitable private 
     67 key. You can not publish an application that is signed with the debug key generated 
     68 by the SDK tools. 
     69   </li>
     70   <li>The system tests a signer certificate's expiration date only at install time. If an 
     71 application's signer certificate expires after the application is installed, the application 
     72 will continue to function normally.</li>
     73   <li>You can use standard tools &mdash; Keytool and Jarsigner &mdash; to generate keys and 
     74 sign your application .apk files.</li>
     75   <li>Once you have signed the application, use the <code>zipalign</code> tool to optimize the final APK package.</li>
     76 </ul>
     77 
     78 <p>The Android system will not install or run an application that is not signed appropriately. This 
     79 applies wherever the Android system is run, whether on an actual device or on the emulator. 
     80 For this reason, you must set up signing for your application before you will be able to 
     81 run or debug it on an emulator or device.</p>
     82 
     83 <p>The Android SDK tools assist you in signing your applications when debugging. Both the ADT Plugin 
     84 for Eclipse and the Ant build tool offer two signing modes &mdash; <em>debug mode</em> 
     85 and <em>release mode</em>. 
     86 
     87 <ul>
     88 <li>While developing and testing, you can compile in debug mode. 
     89 In debug mode, the build tools use the Keytool utility, included in the JDK, to create 
     90 a keystore and key with a known alias and password. At each compilation, the tools then use 
     91 the debug key to sign the application .apk file. Because the password is known, the tools 
     92 don't need to prompt you for the keystore/key password each time you compile.</li>
     93 
     94 <li>When your application is ready for release, you must compile in release mode
     95 and then sign the .apk <span style="color:red">with your private key</span>. 
     96 There are two ways to do this:
     97   <ul>
     98     <li>Using Keytool and Jarsigner in the command-line. In this approach,
     99     you first compile your application to an <em>unsigned</em> .apk. You must then sign
    100     the .apk manually with your private key
    101     using Jarsigner (or similar tool). If you do not have a suitable private key already, 
    102     you can run Keytool manually to generate your own keystore/key and then sign your 
    103     application with Jarsigner.</li>
    104     <li>Using the ADT Export Wizard. If you are developing in Eclipse with the ADT plugin, 
    105     you can use the Export Wizard to compile the application, generate a private key 
    106     (if necessary), and sign the .apk, all in a single process using the Export Wizard.
    107     </li>
    108   </ul>
    109 </li>
    110 </ul>
    111 
    112 <p>Once your application is signed, don't forget to run {@code zipalign} on the APK
    113 for additional optimization.</p>
    114 
    115 <h2 id="strategies">Signing Strategies</h2>
    116 
    117 <p>Some aspects of application signing may affect how you approach the development
    118 of your application, especially if you are planning to release multiple
    119 applications. </p>
    120 
    121 <p>In general, the recommended strategy for all developers is to sign
    122 all of your applications with the same certificate, throughout the expected
    123 lifespan of your applications. There are several reasons why you should do so: </p>
    124 
    125 <ul>
    126 <li>Application upgrade &ndash; As you release updates to your application, you
    127 will want to continue to sign the updates with the same certificate or set of
    128 certificates, if you want users to upgrade seamlessly to the new version. When
    129 the system is installing an update to an application, it compares the
    130 certificate(s) in the new version with those in the existing version. If the
    131 certificates match exactly, including both the certificate data and order, then
    132 the system allows the update. If you sign the new version without using matching
    133 certificates, you will also need to assign a different package name to the
    134 application &mdash; in this case, the user installs the new version as a
    135 completely new application. </li>
    136 
    137 <li>Application modularity &ndash; The Android system allows applications that
    138 are signed by the same certificate to run in the same process, if the
    139 applications so requests, so that the system treats them as a single application.
    140 In this way you can deploy your application in modules, and users can update
    141 each of the modules independently if needed.</li>
    142 
    143 <li>Code/data sharing through permissions &ndash; The Android system provides
    144 signature-based permissions enforcement, so that an application can expose
    145 functionality to another application that is signed with a specified
    146 certificate. By signing multiple applications with the same certificate and
    147 using signature-based permissions checks, your applications can share code and
    148 data in a secure manner. </li>
    149 
    150 </ul>
    151 
    152 <p>Another important consideration in determining your signing strategy is 
    153 how to set the validity period of the key that you will use to sign your 
    154 applications.</p>
    155 
    156 <ul>
    157 <li>If you plan to support upgrades for a single application, you should ensure 
    158 that your key has a validity period that exceeds the expected lifespan of
    159 that application. A validity period of 25 years or more is recommended. 
    160 When your key's validity period expires, users will no longer be
    161 able to seamlessly upgrade to new versions of your application.</li>
    162 
    163 <li>If you will sign multiple distinct applications with the same key, 
    164 you should ensure that your key's validity period exceeds the expected 
    165 lifespan of <em>all versions of all of the applications</em>, including 
    166 dependent applications that may be added to the suite in the future. </li>
    167 
    168 <li>If you plan to publish your application(s) on Android Market, the
    169 key you use to sign the application(s) must have a validity period 
    170 ending after 22 October 2033. The Market server enforces this requirement
    171 to ensure that users can seamlessly upgrade Market applications when
    172 new versions are available. </li>
    173 </ul>
    174 
    175 <p>As you design your application, keep these points in mind and make sure to
    176 use a <a href="#cert">suitable certificate</a> to sign your applications. </p>
    177 
    178 <h2 id="setup">Basic Setup for Signing</h2>
    179 
    180 <p>Before you begin, you should make sure that 
    181 Keytool is available to the SDK build 
    182 tools. In most cases, you can tell the SDK build tools how to find Keytool by setting
    183 your <code>JAVA_HOME</code> environment variable to references a suitable JDK. 
    184 Alternatively, you can add the JDK version of Keytool to your <code>PATH</code> variable.</p>
    185 
    186 <p>If you are developing on a version of Linux that originally came with GNU Compiler for 
    187 Java, make sure that the system is using the JDK version of Keytool, rather than the gcj 
    188 version. If Keytool is already in your <code>PATH</code>, it might be pointing to a symlink at 
    189 <code>/usr/bin/keytool</code>. In this case, check the symlink target to be sure it points 
    190 to the Keytool in the JDK.</p>
    191 
    192 <p>If you will release your application to the public, you will also need to have
    193 the Jarsigner tool available on your machine. Both Jarsigner and Keytool are included
    194 in the JDK. </p>
    195 
    196 <h2 id="debugmode">Signing in Debug Mode</h2>
    197 
    198 <p>The Android build tools provide a debug signing mode that makes it easier for you 
    199 to develop and debug your application, while still meeting the Android system
    200 requirement for signing your .apk.
    201 When using debug mode to build your app, the SDK tools invoke Keytool to automatically create
    202 a debug keystore and key. This debug key is then used to automatically sign the .apk, so
    203 you do not need to sign the package with your own key.</p>
    204 
    205 <p>The SDK tools create the debug keystore/key with predetermined names/passwords:</p>
    206 <ul>
    207 <li>Keystore name: "debug.keystore"</li>
    208 <li>Keystore password: "android"</li>
    209 <li>Key alias: "androiddebugkey"</li>
    210 <li>Key password: "android"</li>
    211 <li>CN: "CN=Android Debug,O=Android,C=US"</li>
    212 </ul></p>
    213 
    214 <p>If necessary, you can change the location/name of the debug keystore/key or
    215 supply a custom debug keystore/key to use. However, any custom debug
    216 keystore/key must use the same keystore/key names and passwords as the default
    217 debug key (as described above). (To do so in Eclipse/ADT, go to
    218 <strong>Windows</strong> &gt; <strong>Preferences</strong> &gt;
    219 <strong>Android</strong> &gt; <strong>Build</strong>.) </p>
    220 
    221 <p class="caution"><strong>Caution:</strong> You <em>cannot</em> release your application 
    222 to the public when signed with the debug certificate.</p>
    223 
    224 <h3>Eclipse Users</h3>
    225 
    226 <p>If you are developing in Eclipse/ADT (and have set up Keytool as described above in 
    227 <a href="#setup">Basic Setup for Signing</a>),
    228 signing in debug mode is enabled by default. When you run or debug your
    229 application, ADT signs the .apk with the debug certificate, runs {@code zipalign} on the 
    230 package, then installs it on 
    231 the selected emulator or connected device. No specific action on your part is needed, 
    232 provided ADT has access to Keytool.</p>
    233 
    234 <h3>Ant Users</h3>
    235 
    236 <p>If you are using Ant to build your .apk files, debug signing mode 
    237 is enabled by using the <code>debug</code> option with the <code>ant</code> command
    238 (assuming that you are using a <code>build.xml</code> file generated by the
    239 <code>android</code> tool). When you run <code>ant debug</code> to 
    240 compile your app, the build script generates a keystore/key and signs the .apk for you. 
    241 The script then also aligns the .apk with the <code>zipalign</code> tool.
    242 No other action on your part is needed. Read 
    243 <a href="{@docRoot}guide/developing/other-ide.html#DebugMode">Developing In Other IDEs: Building
    244 in debug mode</a> for more information.</p>
    245 
    246 
    247 <h3 id="debugexpiry">Expiry of the Debug Certificate</h3>
    248 
    249 <p>The self-signed certificate used to sign your application in debug mode (the default on 
    250 Eclipse/ADT and Ant builds) will have an expiration date of 365 days from its creation date.</p>
    251 
    252 <p>When the certificate expires, you will get a build error. On Ant builds, the error 
    253 looks like this:</p>
    254 
    255 <pre>debug:
    256 [echo] Packaging bin/samples-debug.apk, and signing it with a debug key...
    257 [exec] Debug Certificate expired on 8/4/08 3:43 PM</pre>
    258 
    259 <p>In Eclipse/ADT, you will see a similar error in the Android console.</p>
    260 
    261 <p>To fix this problem, simply delete the <code>debug.keystore</code> file. 
    262 The default storage location for AVDs is in <code>~/.android/avd</code> on OS X and Linux, 
    263 in <code>C:\Documents and Settings\<user>\.android\</code> on Windows XP, and in
    264 <code>C:\Users\<user>\.android\</code> on Windows Vista.</p>
    265 
    266 
    267 <p>The next time you build, the build tools will regenerate a new keystore and debug key.</p>
    268 
    269 <p>Note that, if your development machine is using a non-Gregorian locale, the build 
    270 tools may erroneously generate an already-expired debug certificate, so that you get an 
    271 error when trying to compile your application. For workaround information, see the 
    272 troubleshooting topic <a href="{@docRoot}resources/faq/troubleshooting.html#signingcalendar">
    273 I&nbsp;can't&nbsp;compile my app because the build tools generated an expired debug 
    274 certificate</a>. </p>
    275 
    276 
    277 <h2 id="releasemode">Signing for Public Release</h2>
    278 
    279 <p>When your application is ready for release to other users, you must:</p>
    280 <ol>
    281   <li><a href="#cert">Obtain a suitable private key</a></li>
    282   <li><a href="#releasecompile">Compile the application in release mode</li>
    283   <li><a href="#signapp">Sign your application with your private key</a></li>
    284   <li><a href="#align">Align the final APK package</a></li>
    285 </ol>
    286 
    287 <p>If you are developing in Eclipse with the ADT plugin, you can use the Export Wizard
    288 to perform the compile, sign, and align procedures. The Export Wizard even allows you to 
    289 generate a new keystore and private key in the process. So if you use Eclipse, you can 
    290 skip to <a href="#ExportWizard">Compile and sign with Eclipse ADT</a>.</p>
    291 
    292 
    293 
    294 <h3 id="cert">1. Obtain a suitable private key</h3>
    295 
    296 <p>In preparation for signing your application, you must first ensure that 
    297 you have a suitable private key with which to sign. A suitable private 
    298 key is one that:</p>
    299 
    300 <ul>
    301 <li>Is in your possession</li>
    302 <li>Represents the personal, corporate, or organizational entity to be identified 
    303 with the application</li>
    304 <li>Has a validity period that exceeds the expected lifespan of the application
    305 or application suite. A validity period of more than 25 years is recommended. 
    306 <p>If you plan to publish your application(s) on Android Market, note that a 
    307 validity period ending after 22 October 2033 is a requirement. You can not upload an
    308 application if it is signed with a key whose validity expires before that date. 
    309 </p></li>
    310 <li>Is not the debug key generated by the Android SDK tools. </li>
    311 </ul>
    312 
    313 <p>The key may be self-signed. If you do not have a suitable key, you must 
    314 generate one using Keytool. Make sure that you have Keytool available, as described
    315 in <a href="#setup">Basic Setup</a>.</p>
    316 
    317 <p>To generate a self-signed key with Keytool, use the <code>keytool</code>
    318 command and pass any of the options listed below (and any others, as
    319 needed). </p>
    320 
    321 <p class="warning"><strong>Warning:</strong> Keep your private key secure. 
    322 Before you run Keytool, make sure to read 
    323 <a href="#secure-key">Securing Your Private Key</a> for a discussion of how to keep
    324 your key secure and why doing so is critically important to you and to users. In
    325 particular, when you are generating your key, you should select strong passwords
    326 for both the keystore and key.</p>
    327 
    328 <table>
    329 <tr>
    330 <th>Keytool Option</th>
    331 <th>Description</th>
    332 </tr>
    333 <tr>
    334 <td><code>-genkey</code></td><td>Generate a key pair (public and private
    335 keys)</td>
    336 </tr>
    337 <tr>
    338 <td><code>-v</code></td><td>Enable verbose output.</td>
    339 </tr>
    340 <tr>
    341 <td><code>-alias &lt;alias_name&gt;</code></td><td>An alias for the key. Only
    342 the first 8 characters of the alias are used.</td>
    343 </tr>
    344 <tr>
    345 <td><code>-keyalg &lt;alg&gt;</code></td><td>The encryption algorithm to use 
    346 when generating the key. Both DSA and RSA are supported.</td>
    347 </tr>
    348 <tr>
    349 <td><code>-keysize &lt;size&gt;</code></td><td>The size of each generated key
    350 (bits). If not supplied, Keytool uses a default key size of 1024 bits. In
    351 general, we recommend using a key size of 2048 bits or higher. </td>
    352 </tr>
    353 <tr>
    354 <td><code>-dname &lt;name&gt;</code></td><td><p>A Distinguished Name that describes
    355 who created the key. The value is used as the issuer and subject fields in the
    356 self-signed certificate. </p><p>Note that you do not need to specify this option
    357 in the command line. If not supplied, Jarsigner prompts you to enter each 
    358 of the Distinguished Name fields (CN, OU, and so on).</p></td>
    359 </tr>
    360 <tr>
    361 <td><code>-keypass &lt;password&gt;</code></td><td><p>The password for the
    362 key.</p> <p>As a security precaution, do not include this option in your command
    363 line. If not supplied, Keytool prompts you to enter the password. In this way,
    364 your password is not stored in your shell history.</p></td>
    365 </tr>
    366 <tr>
    367 <td><code>-validity &lt;valdays&gt;</code></td><td><p>The validity period for the
    368 key, in days. </p><p><strong>Note:</strong> A value of 10000 or greater is recommended.</p></td>
    369 </tr>
    370 <tr>
    371 <td><code>-keystore&nbsp;&lt;keystore-name&gt;.keystore</code></td><td>A name
    372 for the keystore containing the private key.</td>
    373 </tr>
    374 <tr>
    375 <td><code>-storepass &lt;password&gt;</code></td><td><p>A password for the
    376 keystore.</p><p>As a security precaution, do not include this option in your
    377 command line. If not supplied, Keytool prompts you to enter the password. In
    378 this way, your password is not stored in your shell history.</p></td>
    379 </tr>
    380 </table>
    381 
    382 <p>Here's an example of a Keytool command that generates a private key:</p>
    383 
    384 <pre>$ keytool -genkey -v -keystore my-release-key.keystore 
    385 -alias alias_name -keyalg RSA -keysize 2048 -validity 10000</pre>
    386 
    387 <p>Running the example command above, Keytool prompts you to provide
    388 passwords for the keystore and key, and to provide the Distinguished
    389 Name fields for your key. It then generates the keystore as a file called
    390 <code>my-release-key.keystore</code>. The keystore and key are 
    391 protected by the passwords you entered. The keystore contains 
    392 a single key, valid for 10000 days. The alias is a name that you &mdash;
    393 will use later, to refer to this keystore when signing your application. </p>
    394 
    395 <p>For more information about Keytool, see the documentation at
    396 <a
    397 href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/#security">
    398 http://java.sun.com/j2se/1.5.0/docs/tooldocs/#security</a></p>
    399 
    400 
    401 
    402 <h3 id="releasecompile">2. Compile the application in release mode</h3>
    403 
    404 <p>In order to release your application to users, you must compile it in release mode. 
    405 In release mode, the compiled application is not signed by default and you will need
    406 to sign it with your private key.</p>
    407 
    408 <p class="caution"><strong>Caution:</strong>
    409 You can not release your application unsigned, or signed with the debug key.</p>
    410 
    411 <h4>With Eclipse</h4>
    412 
    413 <p>To export an <em>unsigned</em> .apk from Eclipse, right-click the project in the Package 
    414 Explorer and select <strong>Android Tools</strong> > <strong>Export Unsigned Application 
    415 Package</strong>. Then specify the file location for the unsigned .apk.
    416 (Alternatively, open your <code>AndroidManifest.xml</code> file in Eclipse, open 
    417 the <em>Overview</em> tab, and click <strong>Export an unsigned .apk</strong>.)</p>
    418 
    419 <p>Note that you can combine the compiling and signing steps with the Export Wizard. See 
    420 <a href="#ExportWizard">Compiling and signing with Eclipse ADT</a>.</p>
    421 
    422 <h4>With Ant</h4>
    423 
    424 <p>If you are using Ant, you can enable release mode by using the <code>release</code> option
    425 with the <code>ant</code> command. For example, if you are running Ant from the 
    426 directory containing your {@code build.xml} file, the command would look like this:</p>
    427 
    428 <pre>ant release</pre>
    429 
    430 <p>By default, the build script compiles the application .apk without signing it. The output file
    431 in your project {@code bin/} will be <code><em>&lt;your_project_name></em>-unsigned.apk</code>.
    432 Because the application .apk is still unsigned, you must manually sign it with your private
    433 key and then align it using {@code zipalign}.</p>
    434 
    435 <p>However, the Ant build script can also perform the signing
    436 and aligning for you, if you have provided the path to your keystore and the name of
    437 your key alias in the project's {@code build.properties} file. With this information provided,
    438 the build script will prompt you for your keystore and alias password when you perform
    439 <code>ant release</code>, it will sign the package and then align it. The final output
    440 file in {@code bin/} will instead be 
    441 <code><em>&lt;your_project_name></em>-release.apk</code>. With these steps
    442 automated for you, you're able to skip the manual procedures below (steps 3 and 4).
    443 To learn how to specify your keystore and alias in the {@code build.properties} file,
    444 see <a href="{@docRoot}guide/developing/other-ide.html#ReleaseMode">Developing In Other
    445 IDEs: Building in release mode</a>.</p>
    446 
    447 
    448 
    449 <h3 id="signapp">3. Sign your application with your private key</h3>
    450 
    451 <p>When you have an application package that is ready to be signed, you can do sign it
    452 using the Jarsigner tool. Make sure that you have Jarsigner available on your
    453 machine, as described in <a href="#setup">Basic Setup</a>. Also, make sure that
    454 the keystore containing your private key is  available.</p>
    455 
    456 <p>To sign your application, you run Jarsigner, referencing both the
    457 application's .apk and the keystore containing the private key with which to
    458 sign the .apk. The table below shows the options you could use. <p>
    459 
    460 <table>
    461 <tr>
    462 <th>Jarsigner Option</th>
    463 <th>Description</th>
    464 </tr>
    465 <tr>
    466 <td><code>-keystore&nbsp;&lt;keystore-name&gt;.keystore</code></td><td>The name of
    467 the keystore containing your private key.</td>
    468 </tr>
    469 <tr>
    470 <td><code>-verbose</code></td><td>Enable verbose output.</td>
    471 </tr>
    472 <tr>
    473 <td><code>-storepass &lt;password&gt;</code></td><td><p>The password for the
    474 keystore. </p><p>As a security precaution, do not include this option 
    475 in your command line unless you are working at a secure computer.
    476 If not supplied, Jarsigner prompts you to enter the password. In this 
    477 way, your password is not stored in your shell history.</p></td>
    478 </tr>
    479 <tr>
    480 <td><code>-keypass &lt;password&gt;</code></td><td><p>The password for the private
    481 key. </p><p>As a security precaution, do not include this option 
    482 in your command line unless you are working at a secure computer.
    483 If not supplied, Jarsigner prompts you to enter the password. In this 
    484 way, your password is not stored in your shell history.</p></td>
    485 </tr>
    486 </table>
    487 
    488 <p>Here's how you would use Jarsigner to sign an application package called
    489 <code>my_application.apk</code>, using the example keystore created above. 
    490 </p>
    491 
    492 <pre>$ jarsigner -verbose -keystore my-release-key.keystore 
    493 my_application.apk alias_name</pre>
    494 
    495 <p>Running the example command above, Jarsigner prompts you to provide
    496 passwords for the keystore and key. It then modifies the .apk 
    497 in-place, meaning the .apk is now signed. Note that you can sign an 
    498 .apk multiple times with different keys.</p>
    499 
    500 <p>To verify that your .apk is signed, you can use a command like this:</p>
    501 
    502 <pre>$ jarsigner -verify my_signed.apk</pre>
    503 
    504 <p>If the .apk is signed properly, Jarsigner prints "jar verified". 
    505 If you want more details, you can try one of these commands:</p>
    506 
    507 <pre>$ jarsigner -verify -verbose my_application.apk</pre>
    508 
    509 <p>or</p>
    510 
    511 <pre>$ jarsigner -verify -verbose -certs my_application.apk</pre>
    512 
    513 <p>The command above, with the <code>-certs</code> option added, will show you the 
    514 "CN=" line that describes who created the key.</p>
    515 
    516 <p class="note"><strong>Note:</strong> If you see "CN=Android Debug", this means the .apk was
    517 signed with the debug key generated by the Android SDK. If you intend to release
    518 your application, you must sign it with your private key instead of the debug
    519 key.</p>
    520 
    521 <p>For more information about Jarsigner, see the documentation at
    522 <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/#security">
    523 http://java.sun.com/j2se/1.5.0/docs/tooldocs/#security</a></p>
    524 
    525 
    526 <h3 id="align">4. Align the final APK package</h3>
    527 
    528 <p>Once you have signed the .apk with your private key, run <code>zipalign</code> on the file.
    529 This tool ensures that all uncompressed data starts with a particular byte alignment,
    530 relative to the start of the file. Ensuring alignment at 4-byte boundaries provides
    531 a performance optimization when installed on a device. When aligned, the Android
    532 system is able to read files with {@code mmap()}, even if
    533 they contain binary data with alignment restrictions, rather than copying all
    534 of the data from the package. The benefit is a reduction in the amount of
    535 RAM consumed by the running application.</p>
    536 
    537 <p>The <code>zipalign</code> tool is provided with the Android SDK, inside the 
    538 <code>tools/</code> directory. To align your signed .apk, execute:</p>
    539 
    540 <pre>zipalign -v 4 <em>your_project_name</em>-unaligned.apk <em>your_project_name</em>.apk</pre>
    541 
    542 <p>The {@code -v} flag turns on verbose output (optional). {@code 4} is the
    543 byte-alignment (don't use anything other than 4). The first file argument is
    544 your signed .apk (the input) and the second file is the destination .apk file (the output). 
    545 If you're overriding an existing .apk, add the {@code -f} flag.</p>
    546 
    547 <p class="caution"><strong>Caution:</strong> Your input .apk must be signed with your
    548 private key <strong>before</strong> you optimize the package with {@code zipalign}.
    549 If you sign it after using {@code zipalign}, it will undo the alignment.</p>
    550 
    551 <p>For more information, read about the 
    552 <a href="{@docRoot}guide/developing/tools/zipalign.html">zipalign</a> tool.
    553 
    554 
    555 <h3 id="ExportWizard">Compile and sign with Eclipse ADT</h3>
    556 
    557 <p>If you are using Eclipse with the ADT plugin, you can use the Export Wizard to
    558 export a <em>signed</em> .apk (and even create a new keystore,
    559 if necessary). The Export Wizard performs all the interaction with
    560 the Keytool and Jarsigner for you, which allows you to sign the package using a GUI
    561 instead of performing the manual procedures to compile, sign,
    562 and align, as discussed above. Once the wizard has compiled and signed your package,
    563 it will also perfom package alignment with {@code zipalign}.
    564 Because the Export Wizard uses both Keytool and Jarsigner, you should
    565 ensure that they are accessible on your computer, as described above
    566 in the <a href=#setup">Basic Setup for Signing</a>.</p>
    567 
    568 <p>To create a signed and aligned .apk in Eclipse:</p>
    569 
    570 <ol>
    571   <li>Select the project in the Package 
    572 Explorer and select <strong>File > Export</strong>.</li>
    573   <li>Open the Android folder, select Export Android Application, 
    574   and click <strong>Next</strong>.
    575   <p>The Export Android Application wizard now starts, which will 
    576   guide you through the process of signing your application,
    577   including steps for selecting the private key with which to sign the .apk
    578   (or creating a new keystore and private key).</p>
    579   <li>Complete the Export Wizard and your application will be compiled, 
    580   signed, aligned, and ready for distribution.</li>
    581 </ol>
    582 
    583 
    584 
    585 <h2 id="secure-key">Securing Your Private Key</h2>
    586 
    587 <p>Maintaining the security of your private key is of critical importance, both
    588 to you and to the user. If you allow someone to use your key, or if you leave
    589 your keystore and passwords in an unsecured location such that a third-party
    590 could find and use them, your authoring identity and the trust of the user
    591 are compromised. </p>
    592 
    593 <p>If a third party should manage to take your key without your knowledge or 
    594 permission, that person could sign and distribute applications that maliciously
    595 replace your authentic applications or corrupt them. Such a person could also
    596 sign and distribute applications under your identity that attack other 
    597 applications or the system itself, or corrupt or steal user data. </p>
    598 
    599 <p>Your reputation as a developer entity depends on your securing your private
    600 key properly, at all times, until the key is expired. Here are some tips for
    601 keeping your key secure: </p>
    602 
    603 <ul>
    604 <li>Select strong passwords for the keystore and key.</li>
    605 <li>When you generate your key with Keytool, <em>do not</em> supply the 
    606 <code>-storepass</code> and <code>-keypass</code> options at the command line. 
    607 If you do so, your passwords will be available in your shell history, 
    608 which any user on your computer could access.</li>
    609 <li>Similarly, when signing your applications with Jarsigner, 
    610 <em>do not</em> supply the <code>-storepass</code> and <code>-keypass</code> 
    611 options at the command line. </li>
    612 <li>Do not give or lend anyone your private key, and do not let unauthorized
    613 persons know your keystore and key passwords.</li>
    614 </ul>
    615 
    616 <p>In general, if you follow common-sense precautions when generating, using,
    617 and storing your key, it will remain secure. </p>