blob: ec00b507cccfc2b95a679c999475d1106c1d501a [file] [log] [blame]
Scott Main50e990c2012-06-21 17:14:39 -07001page.title=Building and Running from the Command Line
2parent.title=Building and Running
3parent.link=index.html
4@jd:body
5
6 <div id="qv-wrapper">
7 <div id="qv">
8 <h2>In this document</h2>
9 <ol>
10 <li><a href="#DebugMode">Building in Debug Mode</a></li>
11 <li><a href="#ReleaseMode">Building in Release Mode</a>
12 <ol>
13 <li><a href="#ManualReleaseMode">Build unsigned</a></li>
14 <li><a href="#AutoReleaseMode">Build signed and aligned</a></li>
15 <li><a href="#OnceBuilt">Once built and signed in release mode</a></li>
16 </ol>
17 </li>
18 <li><a href="#RunningOnEmulator">Running on the Emulator</a></li>
19 <li><a href="#RunningOnDevice">Running on a Device</a></li>
20 <li><a href="#Signing">Application Signing</a></li>
Rich Slogarc48e56e2014-12-30 12:09:05 -080021 <li><a href="#PluginReference">Plugin Language Reference</a></li>
Scott Main50e990c2012-06-21 17:14:39 -070022 </ol>
23 <h2>See also</h2>
24 <ol>
Rich Slogar40833272014-11-06 17:15:28 -080025 <li><a href="{@docRoot}sdk/installing/studio-build.html">
26 Build System</a></li>
27 <li><a href="{@docRoot}tools/devices/managing-avds-cmdline.html">
28 Managing AVDs from the Command Line</a></li>
29 <li><a href="{@docRoot}tools/devices/emulator.html">
30 Using the Android Emulator</a></li>
31 <li><a href="{@docRoot}tools/publishing/app-signing.html">
32 Signing Your Applications</a></li>
Scott Main50e990c2012-06-21 17:14:39 -070033 </ol>
34 </div>
35 </div>
36
Rich Slogar40833272014-11-06 17:15:28 -080037 <p>By default, there are two build types to build your application using the gradle.build settings:
38 one for debugging your application &mdash; <em>debug</em> &mdash; and one for building your
39 final package for release &mdash; <em>release mode</em>. Regardless of which way you build type
40 your modules use, the app must be signed before it can install on an emulator or device&mdash;with
41 a debug key when building in debug mode and with your own private key when building in release mode.</p>
Scott Main50e990c2012-06-21 17:14:39 -070042
Rich Slogar40833272014-11-06 17:15:28 -080043 <p>Whether you're building with the debug or release build type, you need to run
44 and build your module. This will create the .apk file that you can install on an emulator or device.
45 When you build using the debug build type, the .apk file is automatically signed by the SDK tools
46 with a debug key based on the <code>debuggable true</code> setting in the module's gradle.build file,
47 so it's instantly ready for installation onto an emulator or attached
Scott Main50e990c2012-06-21 17:14:39 -070048 development device. You cannot distribute an application that is signed with a debug key.
Rich Slogar40833272014-11-06 17:15:28 -080049 When you build using the release build type, the .apk file is <em>unsigned</em>, so you
50 must manually sign it with your own private key, using Keytool and Jarsigner settings in the
51 module's gradle.build file.</p>
Scott Main50e990c2012-06-21 17:14:39 -070052
53 <p>It's important that you read and understand <a href=
54 "{@docRoot}tools/publishing/app-signing.html">Signing Your Applications</a>, particularly once
55 you're ready to release your application and share it with end-users. That document describes the
56 procedure for generating a private key and then using it to sign your .apk file. If you're just
57 getting started, however, you can quickly run your applications on an emulator or your own
58 development device by building in debug mode.</p>
59
Rich Slogar40833272014-11-06 17:15:28 -080060 <p>If you don't have <a href="http://www.gradle.org/">Gradle</a>, you can obtain it from the <a href="http://gradle.org/">Gradle
Scott Main50e990c2012-06-21 17:14:39 -070061 home page</a>. Install it and make sure it is in your executable PATH. Before calling Ant, you
62 need to declare the JAVA_HOME environment variable to specify the path to where the JDK is
63 installed.</p>
64
65 <p class="note"><strong>Note:</strong> When installing JDK on Windows, the default is to install
66 in the "Program Files" directory. This location will cause <code>ant</code> to fail, because of
67 the space. To fix the problem, you can specify the JAVA_HOME variable like this:
68 <pre>set JAVA_HOME=c:\Progra~1\Java\&lt;jdkdir&gt;</pre>
69
70 <p>The easiest solution, however, is to install JDK in a non-space directory, for example:</p>
71
Rich Slogar40833272014-11-06 17:15:28 -080072 <pre>c:\java\jdk1.7</pre>
Scott Main50e990c2012-06-21 17:14:39 -070073
74 <h2 id="DebugMode">Building in Debug Mode</h2>
75
76 <p>For immediate application testing and debugging, you can build your application in debug mode
77 and immediately install it on an emulator. In debug mode, the build tools automatically sign your
78 application with a debug key and optimize the package with {@code zipalign}.</p>
79
Rich Slogar40833272014-11-06 17:15:28 -080080 <p>To build in debug mode, open a command-line and navigate to the root of your project directory.
81 Use Gradle to build your project in debug mode, invoke the <code>assembleDebug</code> build task
82 using the Gradle wrapper script (<code>gradlew assembleRelease</code>).
Scott Main50e990c2012-06-21 17:14:39 -070083
Rich Slogar40833272014-11-06 17:15:28 -080084 <p>This creates your debug <code>.apk</code> file inside the module <code>build/</code>
85 directory, named <code>&lt;your_module_name&gt;-debug.apk</code>. The file is already signed
86 with the debug key and has been aligned with
87 <a href="{@docRoot}tools/help/zipalign.html"><code>zipalign</code></a>. </p>
88
89 <p>On Windows platforms, type this command:</p>
90
91<pre>
92> gradlew.bat assembleDebug
Scott Main50e990c2012-06-21 17:14:39 -070093</pre>
94
Rich Slogar40833272014-11-06 17:15:28 -080095<p>On Mac OS and Linux platforms, type these commands:</p>
Scott Main50e990c2012-06-21 17:14:39 -070096
Rich Slogar40833272014-11-06 17:15:28 -080097<pre>
98$ chmod +x gradlew
99$ ./gradlew assembleDebug
100</pre>
101
102 <p>The first command (<code>chmod</code>) adds the execution permission to the Gradle wrapper
103 script and is only necessary the first time you build this project from the command line.</p>
104
105 <p>After you build the project, the output APK for the app module is located in
106 <code>app/build/outputs/apk/</code>, and the output AAR for any lib modules is located in
107 <code>lib/build/outputs/libs/</code>.</p>
108
109 <p>To see a list of all available build tasks for your project, type this command:</p>
110
111<pre>
112$ ./gradlew tasks
113</pre>
114
115 <p>Each time you change a source file or resource, you must run Gradle again in order to package up
Scott Main50e990c2012-06-21 17:14:39 -0700116 the latest version of the application.</p>
117
Rich Slogar40833272014-11-06 17:15:28 -0800118 <p>To install and run your application on an emulator, see the section about <a href=
119 "{@docRoot}tools/building/building-studio.html">Running on the Emulator</a>.</p>
Scott Main50e990c2012-06-21 17:14:39 -0700120
121 <h2 id="ReleaseMode">Building in Release Mode</h2>
122
123 <p>When you're ready to release and distribute your application to end-users, you must build your
124 application in release mode. Once you have built in release mode, it's a good idea to perform
125 additional testing and debugging with the final .apk.</p>
126
127 <p>Before you start building your application in release mode, be aware that you must sign the
128 resulting application package with your private key, and should then align it using the {@code
129 zipalign} tool. There are two approaches to building in release mode: build an unsigned package
130 in release mode and then manually sign and align the package, or allow the build script to sign
131 and align the package for you.</p>
132
133 <h3 id="ManualReleaseMode">Build unsigned</h3>
134
135 <p>If you build your application <em>unsigned</em>, then you will need to manually sign and align
136 the package.</p>
137
Rich Slogar40833272014-11-06 17:15:28 -0800138 <p>To build an <em>unsigned</em> .apk in release mode, open a command-line and navigate to the
139 root of your module directory. Invoke the <code>assembleRelease</code> build task.</li>
Scott Main50e990c2012-06-21 17:14:39 -0700140
Rich Slogar40833272014-11-06 17:15:28 -0800141 <p>On Windows platforms, type this command:</p>
Scott Main50e990c2012-06-21 17:14:39 -0700142
Rich Slogar40833272014-11-06 17:15:28 -0800143<pre>
144> gradlew.bat assembleRelease
Scott Main50e990c2012-06-21 17:14:39 -0700145</pre>
Rich Slogar40833272014-11-06 17:15:28 -0800146
147<p>On Mac OS and Linux platforms, type this command:</p>
148
149<pre>
150$ ./gradlew assembleRelease
151</pre>
152
Scott Main50e990c2012-06-21 17:14:39 -0700153
154 <p>This creates your Android application .apk file inside the project <code>bin/</code>
Rich Slogar40833272014-11-06 17:15:28 -0800155 directory, named <code><em>&lt;your_module_name&gt;</em>-unsigned.apk</code>.</p>
Scott Main50e990c2012-06-21 17:14:39 -0700156
157 <p class="note"><strong>Note:</strong> The .apk file is <em>unsigned</em> at this point and can't
158 be installed until signed with your private key.</p>
159
160 <p>Once you have created the unsigned .apk, your next step is to sign the .apk with your private
161 key and then align it with {@code zipalign}. To complete this procedure, read <a href=
162 "{@docRoot}tools/publishing/app-signing.html">Signing Your Applications</a>.</p>
163
164 <p>When your <code>.apk</code> has been signed and aligned, it's ready to be distributed to end-users.
165 You should test the final build on different devices or AVDs to ensure that it
166 runs properly on different platforms.</p>
167
168 <h3 id="AutoReleaseMode">Build signed and aligned</h3>
169
170 <p>If you would like, you can configure the Android build script to automatically sign and align
171 your application package. To do so, you must provide the path to your keystore and the name of
Rich Slogar40833272014-11-06 17:15:28 -0800172 your key alias in your modules's build.gradle file. With this information provided,
173 the build will prompt you for your keystore and alias password when you build using the release
174 build type and produce your final application package, which will be ready for distribution.</p>
Scott Main50e990c2012-06-21 17:14:39 -0700175
Rich Slogar40833272014-11-06 17:15:28 -0800176 <p>To specify your keystore and alias, open the module gradle.build file (found in
177 the root of the module directory) and add entries for {@code storeFile}, {@code storePassword},
178 {@code keyAlias} and {@code keyPassword}.
Scott Main50e990c2012-06-21 17:14:39 -0700179 For example:</p>
180 <pre>
Rich Slogar40833272014-11-06 17:15:28 -0800181storeFile file("myreleasekey.keystore")
182keyAlias "MyReleaseKey"
Scott Main50e990c2012-06-21 17:14:39 -0700183</pre>
184
185 <p>Save your changes. Now you can build a <em>signed</em> .apk in release mode:</p>
186
187 <ol>
Rich Slogar40833272014-11-06 17:15:28 -0800188 <li>Open a command-line and navigate to the root of your module directory.</li>
Scott Main50e990c2012-06-21 17:14:39 -0700189
Rich Slogar40833272014-11-06 17:15:28 -0800190 <li>Edit the gradle.build file to build your project in release mode:
191 <p><pre>
192...
193android {
194 ...
195 defaultConfig { ... }
196 signingConfigs {
197 release {
198 storeFile file("myreleasekey.keystore")
199 storePassword "password"
200 keyAlias "MyReleaseKey"
201 keyPassword "password"
202 }
203 }
204 buildTypes {
205 release {
206 ...
207 signingConfig signingConfigs.release
208 }
209 }
210}
211...
212</pre></p>
Scott Main50e990c2012-06-21 17:14:39 -0700213 </li>
214
215 <li>When prompted, enter you keystore and alias passwords.
216
217 <p class="caution"><strong>Caution:</strong> As described above, your password will be
218 visible on the screen.</p>
219 </li>
220 </ol>
221
Rich Slogar40833272014-11-06 17:15:28 -0800222 <p>This creates your Android application .apk file inside the module <code>build/</code>
223 directory, named <code><em>&lt;your_module_name&gt;</em>-release.apk</code>. This .apk file has
224 been signed with the private key specified in gradle.build file and aligned with {@code
Scott Main50e990c2012-06-21 17:14:39 -0700225 zipalign}. It's ready for installation and distribution.</p>
226
227 <h3 id="OnceBuilt">Once built and signed in release mode</h3>
228
229 <p>Once you have signed your application with a private key, you can install and run it on an
230 <a href="#RunningOnEmulator">emulator</a> or <a href="#RunningOnDevice">device</a>. You can
231 also try installing it onto a device from a web server. Simply upload the signed .apk to a web
232 site, then load the .apk URL in your Android web browser to download the application and begin
233 installation. (On your device, be sure you have enabled
234 <em>Settings &gt; Applications &gt; Unknown sources</em>.)</p>
235
236 <h2 id="RunningOnEmulator">Running on the Emulator</h2>
237
238 <p>Before you can run your application on the Android Emulator, you must <a href=
239 "{@docRoot}tools/devices/managing-avds.html">create an AVD</a>.</p>
240
241 <p>To run your application:</p>
242
243 <ol>
244 <li>
245 <strong>Open the AVD Manager and launch a virtual device</strong>
246
247 <p>From your SDK's <code>platform-tools/</code> directory, execute the {@code android} tool
248with the <code>avd</code> options:</p>
249 <pre>
250android avd
251</pre>
252
253 <p>In the <em>Virtual Devices</em> view, select an AVD and click <strong>Start</strong>.</p>
254 </li>
255
256 <li>
257 <strong>Install your application</strong>
258
259 <p>From your SDK's <code>tools/</code> directory, install the {@code .apk} on the
260 emulator:</p>
261 <pre>
262adb install <em>&lt;path_to_your_bin&gt;</em>.apk
263</pre>
264
Rich Slogar40833272014-11-06 17:15:28 -0800265 <p>Your .apk file (signed with either a release or debug key) is in your module {@code build/}
Scott Main50e990c2012-06-21 17:14:39 -0700266 directory after you build your application.</p>
267
268 <p>If there is more than one emulator running, you must specify the emulator upon which to
269 install the application, by its serial number, with the <code>-s</code> option. For
270 example:</p>
271 <pre>
272adb -s emulator-5554 install <em>path/to/your/app</em>.apk
273</pre>
274
275 <p>To see a list of available device serial numbers, execute {@code adb devices}.</p>
276 </li>
277 </ol>
278
279 <p>If you don't see your application on the emulator, try closing the emulator and launching the
280 virtual device again from the AVD Manager. Sometimes when you install an application for the
281 first time, it won't show up in the application launcher or be accessible by other applications.
282 This is because the package manager usually examines manifests completely only on emulator
283 startup.</p>
284
285 <p>Be certain to create multiple AVDs upon which to test your application. You should have one
286 AVD for each platform and screen type with which your application is compatible. For instance, if
Scott Mainc8931202013-05-06 16:38:42 -0700287 your application compiles against the Android 4.0 (API Level 14) platform, you should create an
288 AVD for each platform equal to and greater than 4.0 and an AVD for each <a href=
Scott Main50e990c2012-06-21 17:14:39 -0700289 "{@docRoot}guide/practices/screens_support.html">screen type</a> you support, then test your
290 application on each one.</p>
291
292 <p class="note"><strong>Tip:</strong> If you have <em>only one</em> emulator running, you can
293 build your application and install it on the emulator in one simple step. Navigate to the root of
294 your project directory and use Ant to compile the project with <em>install mode</em>: <code>ant
295 install</code>. This will build your application, sign it with the debug key, and install it on
296 the currently running emulator.</p>
297
298 <h2 id="RunningOnDevice">Running on a Device</h2>
299
300 <p>Before you can run your application on a device, you must perform some basic setup for your
301 device:</p>
302
303 <ul>
Scott Mainda02c642012-10-11 14:37:59 -0700304 <li>Enable <strong>USB debugging</strong> on your device.
305 <ul>
306 <li>On most devices running Android 3.2 or older, you can find the option under
307 <strong>Settings > Applications > Development</strong>.</li>
308 <li>On Android 4.0 and newer, it's in <strong>Settings > Developer options</strong>.
309 <p class="note"><strong>Note:</strong> On Android 4.2 and newer, <strong>Developer
310 options</strong> is hidden by default. To make it available, go
311 to <strong>Settings > About phone</strong> and tap <strong>Build number</strong>
312 seven times. Return to the previous screen to find <strong>Developer options</strong>.</p>
313 </li>
314 </ul>
315 </li>
Scott Main50e990c2012-06-21 17:14:39 -0700316
317 <li>Ensure that your development computer can detect your device when connected via USB</li>
318 </ul>
319
320 <p>Read <a href="{@docRoot}tools/device.html#setting-up">Setting up a Device for
321 Development</a> for more information.</p>
322
323 <p>Once your device is set up and connected via USB, navigate to your SDK's <code>platform-tools/</code>
324 directory and install the <code>.apk</code> on the device:</p>
325 <pre>
326adb -d install <em>path/to/your/app</em>.apk
327</pre>
328
329 <p>The {@code -d} flag specifies that you want to use the attached device (in case you also have
330 an emulator running).</p>
331
332 <p>For more information on the tools used above, please see the following documents:</p>
333
334 <ul>
335 <li><a href="{@docRoot}tools/help/android.html">android Tool</a></li>
336
337 <li><a href="{@docRoot}tools/devices/emulator.html">Android Emulator</a></li>
338
339 <li><a href="{@docRoot}tools/help/adb.html">Android Debug Bridge</a> (ADB)</li>
340 </ul>
341
342 <h2 id="Signing">Application Signing</h2>
343
344 <p>As you begin developing Android applications, understand that all Android applications must be
345 digitally signed before the system will install them on an emulator or device. There are two ways
346 to do this: with a <em>debug key</em> (for immediate testing on an emulator or development
347 device) or with a <em>private key</em> (for application distribution).</p>
348
349 <p>The Android build tools help you get started by automatically signing your .apk files with a
Rich Slogar40833272014-11-06 17:15:28 -0800350 debug key at build time. This means that you can build your application and install it on the
Scott Main50e990c2012-06-21 17:14:39 -0700351 emulator without having to generate your own private key. However, please note that if you intend
352 to publish your application, you <strong>must</strong> sign the application with your own private
353 key, rather than the debug key generated by the SDK tools.</p>
354
Rich Slogar40833272014-11-06 17:15:28 -0800355 <p>Android Studio helps you get started quickly by signing your .apk files with a debug key,
Scott Main50e990c2012-06-21 17:14:39 -0700356 prior to installing them on an emulator or development device. This means that you can quickly
Rich Slogar40833272014-11-06 17:15:28 -0800357 run your application from Android Studio without having to generate your own private key. No
358 specific action on your part is needed, provided ADT has access to Keytool. However, please note
359 that if you intend to publish your application, you <strong>must</strong> sign the application
360 with your own private key, rather than the debug key generated by the SDK tools.</p>
Scott Main50e990c2012-06-21 17:14:39 -0700361
362 <p>Please read <a href="{@docRoot}tools/publishing/app-signing.html">Signing Your
363 Applications</a>, which provides a thorough guide to application signing on Android and what it
Rich Slogar40833272014-11-06 17:15:28 -0800364 means to you as an Android application developer. The document also includes a guide to publishing
365 and signing your application.</p>
Scott Main50e990c2012-06-21 17:14:39 -0700366
Rich Slogarc48e56e2014-12-30 12:09:05 -0800367 <h2 id="PluginReference">Android Plugin for Gradle</h2>
Scott Main50e990c2012-06-21 17:14:39 -0700368
Rich Slogarc48e56e2014-12-30 12:09:05 -0800369 <p>The Android build system uses the Android plugin for Gradle to support the Gradle Domain
370 Specific Language (DSL) and declarative language elements. See the
371 <a href="{@docRoot}tools/building/plugin-for-gradle.html">Android Plug-in for Gradle</a> section for
372 a description of the plugin and a link to the complete list of the supported Gradle DSL elements.</p>
373
374
Scott Main50e990c2012-06-21 17:14:39 -0700375