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