1page.title=Signing Your Applications
2@jd:body
3
4<div id="qv-wrapper">
5<div id="qv">
6
7<h2>In this document</h2>
8
9<ol>
10<li><a href="#overview">Signing Overview</a>
11  <ol>
12  	<li><a href="#debug-mode">Signing in Debug Mode</a></li>
13  	<li><a href="#release-mode">Signing in Release Mode</a></li>
14  	<li><a href="#wear-apps">Signing Android Wear Apps</a></li>
15  </ol>
16</li>
17<li><a href="#studio">Signing Your App in Android Studio</a>
18  <ol>
19  	<li><a href="sign-auto">Automatically Signing Your App</a></li>
20  </ol>
21</li>
22<li><a href="#studio">Signing Your App with Android Studio</a></li>
23<li><a href="#considerations">Signing Considerations</a></li>
24<li><a href="#secure-key">Securing Your Private Key</a></li>
25<li><a href="#expdebug">Expiry of the Debug Certificate</a></li>
26<li><a href="#signing-manually">Signing Your App Manually</a></li>
27</ol>
28
29<h2>See also</h2>
30
31<ol>
32<li><a href="{@docRoot}tools/publishing/versioning.html">Versioning Your Applications</a></li>
33<li><a href="{@docRoot}tools/publishing/preparing.html">Preparing to Publish</a></li>
34</ol>
35
36</div>
37</div>
38
39
40<p>Android requires that all apps be digitally signed with a certificate before they can be
41installed. Android uses this certificate to identify the author of an app, and the certificate
42does not need to be signed by a certificate authority. Android apps often use self-signed
43certificates. The app developer holds the certificate's private key.</p>
44
45
46<h2 id="overview">Signing Overview</h2>
47
48<p>You can sign an app in debug or release mode. You sign your app in debug mode during development
49and in release mode when you are ready to distribute your app. The Android SDK generates a
50certificate to sign apps in debug mode. To sign apps in release mode, you need to generate
51your own certificate.</p>
52
53<h3 id="debug-mode">Signing in Debug Mode</h3>
54
55<p>In debug mode, you sign your app with a debug certificate generated by the Android SDK tools.
56This certificate has a private key with a known password, so you can run and debug your app
57without typing the password every time you make a change to your project.</p>
58
59<p>Android Studio signs your app in debug mode automatically when
60you run or debug your project from the IDE.</p>
61
62<p>You can run and debug an app signed in debug mode on the emulator and on devices connected
63to your development manchine through USB, but you cannot distribute an app signed in debug
64mode.</p>
65
66<p>By default, the <em>debug</em> configuration uses a debug keystore, with a known
67password and a default key with a known password.
68The debug keystore is located in $HOME/.android/debug.keystore, and is created if not present.
69
70The debug build type is set to use this debug <code>SigningConfig</code> automatically. </p>
71
72<p>For more information about how to build and run apps in debug mode, see
73<a href="{@docRoot}tools/building/index.html">Building and Running</a>.</p>
74
75<h3 id="release-mode">Signing in Release Mode</h3>
76
77<p>In release mode, you sign your app with your own certificate:</p>
78
79<ol>
80<li><em>Create a keystore.</em> A <strong>keystore</strong> is a binary file that contains a
81set of private keys. You must keep your keystore in a safe and secure place.</li>
82<li><em>Create a private key.</em> A <strong>private key</strong> represents the entity to
83be identified with the app, such as a person or a company.</li>
84<li><p>Add the signing configuration to the build file for the <code>app</code> module:</p>
85        <p><pre>
86...
87android {
88    ...
89    defaultConfig { ... }
90    signingConfigs {
91        release {
92            storeFile file("myreleasekey.keystore")
93            storePassword "password"
94            keyAlias "MyReleaseKey"
95            keyPassword "password"
96        }
97    }
98    buildTypes {
99        release {
100            ...
101            signingConfig signingConfigs.release
102        }
103    }
104}
105...
106</pre></p>
107    </li>
108<li>Invoke the <code>assembleRelease</code> build task from Android Studio.</li>
109</ol>
110
111<p>The package in <code>app/build/apk/app-release.apk</code> is now signed with your release key.</p>
112
113<p class="note"><strong>Note:</strong> Including the passwords for your release key and keystore
114inside the build file is not a good security practice. Alternatively, you can configure the build
115file to obtain these passwords from environment variables or have the build process prompt you
116for these passwords.</p>
117
118<p>To obtain these passwords from environment variables:</p>
119
120<pre>
121storePassword System.getenv("KSTOREPWD")
122keyPassword System.getenv("KEYPWD")
123</pre>
124
125<p>To have the build process prompt you for these passwords if you are invoking the build from
126the command line:</p>
127
128<pre>
129storePassword System.console().readLine("\nKeystore password: ")
130keyPassword System.console().readLine("\nKey password: ")
131</pre>
132
133
134<p>After you complete this process, you can distribute your app and publish it on Google Play.</p>
135
136<p class="warning"><strong>Warning:</strong> Keep your keystore and private key in a safe and
137secure place, and ensure that you have secure backups of them. If you publish an app to Google
138Play and then lose the key with which you signed your app, you will not be able to publish
139any updates to your app, since you must always sign all versions of your app with the same
140key.</p>
141
142<p>The rest of this document provides detailed instructions about how to generate a private
143key and sign your apps in release mode with Android Studio.</p>
144
145<h3 id="wear-apps">Signing Android Wear Apps</h3>
146
147<p>When publishing Android Wear apps, you package the wearable app inside of a handheld app,
148because users cannot browse and install apps directly on the wearable. Both apps must be signed.
149For more information on packaging and signing Android Wear apps, see
150<a href="{@docRoot}training/wearables/apps/packaging.html">Packaging Wearable Apps</a>.</p>
151
152
153<h2 id="studio">Signing Your App in Android Studio</h2>
154
155<p>To sign your app in release mode in Android Studio, follow these steps:</p>
156
157<ol>
158<li>On the menu bar, click <strong>Build</strong> &gt; <strong>Generate Signed APK</strong>.</li>
159<li><p>On the <em>Generate Signed APK Wizard</em> window, click <strong>Create new</strong> to create
160a new keystore.</p><p>If you already have a keystore, go to step 4.</p></li>
161<li><p>On the <em>New Key Store</em> window, provide the required information as shown
162in figure 1.</p><p>Your key should be valid for at least 25 years, so you can sign app updates
163with the same key through the lifespan of your app.</p>
164<img src="{@docRoot}images/tools/signstudio2.png" alt=""
165     width="416" height="364" style="margin-top:15px"/>
166<p class="img-caption"><strong>Figure 1</strong>. Create a new keystore in Android Studio.</p>
167</li>
168<li><p>On the <em>Generate Signed APK Wizard</em> window, select a keystore, a private key, and enter
169the passwords for both. Then click <strong>Next</strong>.</p>
170<img src="{@docRoot}images/tools/signstudio1.png" alt=""
171     width="349" height="232" style="margin-top:15px"/>
172<p class="img-caption"><strong>Figure 2</strong>. Select a private key in Android Studio.</p>
173</li>
174<li><p>On the next window, select a destination for the signed APK and click
175<strong>Finish</strong>.</p>
176<img src="{@docRoot}images/tools/signstudio3.png" alt=""
177     width="350" height="175" style="margin-top:15px"/>
178<p class="img-caption"><strong>Figure 3</strong>. Generate a signed APK in Android Studio.</p>
179</li>
180</ol>
181
182<h3 id="sign-auto">Automatically Signing Your App</h3>
183
184<p>In Android Studio, you can configure your project to sign your release APK automatically
185during the build process:</p>
186
187<ol>
188<li>On the project browser, right click on your app and select <strong>Open Module
189Settings</strong>.</li>
190<li>On the <em>Project Structure</em> window, select your app's module under <em>Modules</em>.</li>
191<li>Click on the <strong>Signing</strong> tab.</li>
192<li><p>Select your keystore file, enter a name for this signing configuration (as you may create
193more than one), and enter the required information.</p>
194<img src="{@docRoot}images/tools/signstudio10.png" alt=""
195     width="623" height="372" style="margin-top:15px"/>
196<p class="img-caption"><strong>Figure 4</strong>. Create a signing configuration in Android Studio.</p>
197</li>
198<li>Click on the <strong>Build Types</strong> tab.</li>
199<li>Select the <strong>release</strong> build.</li>
200<li><p>Under <em>Signing Config</em>, select the signing configuration you just created.</p>
201<img src="{@docRoot}images/tools/signstudio11.png" alt=""
202     width="623" height="372" style="margin-top:15px"/>
203<p class="img-caption"><strong>Figure 5</strong>. Select a signing configuration in Android Studio.</p>
204</li>
205<li>Click <strong>OK</strong>.</li>
206</ol>
207
208<p>You can also specify your signing settings in Gradle configuration files. For more information,
209see <a href="{@docRoot}tools/building/configuring-gradle.html#configureSigning">Configuring Gradle
210Builds</a>.</p>
211
212
213
214<h2 id="considerations">Signing Considerations</h2>
215
216<p>You should sign all of your apps with the same certificate throughout the expected lifespan
217of your applications. There are several reasons why you should do so:</p>
218
219<ul>
220<li>App upgrade: When the system is installing an update to an app, it compares the certificate(s)
221in the new version with those in the existing version. The system allows the update if the
222certificates match. If you sign the new version with a different certificate, you must assign a
223different package name to the application&mdash;in this case, the user installs the new version as
224a completely new application.</li>
225<li>App modularity: Android allows apps signed by the same certificate to run in the same process,
226if the applications so requests, so that the system treats them as a single application. In this
227way you can deploy your app in modules, and users can update each of the modules independently.</li>
228<li>Code/data sharing through permissions: Android provides signature-based permissions
229enforcement, so that an app can expose functionality to another app that is signed with a
230specified certificate. By signing multiple apps with the same certificate and using
231signature-based permissions checks, your apps can share code and data in a secure manner.</li>
232</ul>
233
234<p>If you plan to support upgrades for an app, ensure that your key has a validity
235period that exceeds the expected lifespan of that app. A validity period of 25 years or more is
236recommended. When your key's validity period expires, users will no longer be able to seamlessly
237upgrade to new versions of your application.</p>
238
239<p>If you plan to publish your apps on Google Play, the key you use to sign these apps must have
240a validity period ending after 22 October 2033. Google Play enforces this requirement to ensure
241that users can seamlessly upgrade apps when new versions are available.</p>
242
243
244<h2 id="secure-key">Securing Your Private Key</h2>
245
246<p>Maintaining the security of your private key is of critical importance, both to you and to
247the user. If you allow someone to use your key, or if you leave your keystore and passwords in
248an unsecured location such that a third-party could find and use them, your authoring identity
249and the trust of the user are compromised.</p>
250
251<p>If a third party should manage to take your key without your knowledge or permission, that
252person could sign and distribute apps that maliciously replace your authentic apps or corrupt
253them. Such a person could also sign and distribute apps under your identity that attack
254other apps or the system itself, or corrupt or steal user data.</p>
255
256<p>Your private key is required for signing all future versions of your app. If you lose or
257misplace your key, you will not be able to publish updates to your existing appn. You cannot
258regenerate a previously generated key.</p>
259
260<p>Your reputation as a developer entity depends on your securing your private key properly, at
261all times, until the key is expired. Here are some tips for keeping your key secure:</p>
262
263<ul>
264<li>Select strong passwords for the keystore and key.</li>
265<li>Do not give or lend anyone your private key, and do not let unauthorized persons know your
266keystore and key passwords.</li>
267<li>Keep the keystore file containing your private key in a safe, secure place.</li>
268</ul>
269
270<p>In general, if you follow common-sense precautions when generating, using, and storing
271your key, it will remain secure.</p>
272
273
274<h2 id="expdebug">Expiry of the Debug Certificate</h2>
275
276<p>The self-signed certificate used to sign your application in debug mode has an expiration date
277of 365 days from its creation date. When the certificate expires, you will get a build error.</p>
278
279<p>To fix this problem, simply delete the <code>debug.keystore</code> file. The default storage
280location is in <code>~/.android/</code> on OS X and Linux, in <code>C:\Documents and
281Settings\&lt;user&gt;\.android\</code> on Windows XP, and in
282<code>C:\Users\&lt;user&gt;\.android\</code> on Windows Vista and Windows 7.</p>
283
284<p>The next time you build, the build tools will regenerate a new keystore and debug key.</p>
285
286<p>Note that, if your development machine is using a non-Gregorian locale, the build tools may
287erroneously generate an already-expired debug certificate, so that you get an error when trying
288to compile your application. For workaround information, see the troubleshooting topic
289<a href="{@docRoot}resources/faq/troubleshooting.html#signingcalendar">I can't compile my app
290because the build tools generated an expired debug certificate</a>.</p>
291
292
293<h2 id="signing-manually">Signing Your App Manually</h2>
294
295<p>You do not need Android Studio to sign your app. You can sign
296your app from the command line using standard tools from the Android SDK and the JDK. To sign
297an app in release mode from the command line:</p>
298
299<ol>
300<li>
301  <p>Generate a private key using
302  <code><a href="http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/keytool.html">keytool</a></code>.
303  For example:</p>
304<pre>
305$ keytool -genkey -v -keystore my-release-key.keystore
306-alias alias_name -keyalg RSA -keysize 2048 -validity 10000
307</pre>
308  <p>This example prompts you for passwords for the keystore and key, and to provide the
309  Distinguished Name fields for your key. It then generates the keystore as a file called
310  <code>my-release-key.keystore</code>. The keystore contains a single key, valid for 10000 days.
311  The alias is a name that you will use later when signing your app.</p>
312</li>
313<li style="margin-top:18px">
314  <p>Compile your app in release mode to obtain an unsigned APK.</p>
315</li>
316<li style="margin-top:18px">
317  <p>Sign your app with your private key using
318  <code><a href="http://docs.oracle.com/javase/6/docs/technotes/tools/windows/jarsigner.html">jarsigner</a></code>:
319  </p>
320<pre>
321$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1
322-keystore my-release-key.keystore my_application.apk alias_name
323</pre>
324  <p>This example prompts you for passwords for the keystore and key. It then modifies the APK
325  in-place to sign it. Note that you can sign an APK multiple times with different keys.</p>
326</li>
327<li style="margin-top:18px">
328  <p>Verify that your APK is signed. For example:</p>
329<pre>
330$ jarsigner -verify -verbose -certs my_application.apk
331</pre>
332</li>
333<li style="margin-top:18px">
334  <p>Align the final APK package using
335  <code><a href="{@docRoot}tools/help/zipalign.html">zipalign</a></code>.</p>
336<pre>
337$ zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk
338</pre>
339  <p><code>zipalign</code> ensures that all uncompressed data starts with a particular byte
340  alignment relative to the start of the file, which reduces the amount of RAM consumed by an
341  app.</p>
342</li>
343</ol>
344