1 page.title=Building and Running from the Command Line 2 parent.title=Building and Running 3 parent.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> 21 <li><a href="#PluginReference">Plugin Language Reference</a></li> 22 </ol> 23 <h2>See also</h2> 24 <ol> 25 <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> 33 </ol> 34 </div> 35 </div> 36 37 <p>By default, there are two build types to build your application using the Gradle build settings: 38 one for debugging your application — <em>debug</em> — and one for building your 39 final package for release — <em>release mode</em>. Regardless of which build type 40 your modules use, the app must be signed before it can install on an emulator or device—with 41 a debug key when building in debug mode and with your own private key when building in release mode.</p> 42 43 <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 build.gradle file, 47 so it's instantly ready for installation onto an emulator or attached 48 development device. You cannot distribute an application that is signed with a debug key. 49 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 <code>build.gradle</code> file.</p> 52 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 60 <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 61 home page</a>. Install it and make sure it is in your executable PATH. Before calling Gradle, 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 using <code>ant</code> and installing JDK on Windows, 66 the default is to install in the "Program Files" directory. This location will cause 67 <code>ant</code> to fail, because of the space. To fix the problem, you can specify the JAVA_HOME 68 variable like this: 69 <pre>set JAVA_HOME=c:\Progra~1\Java\<jdkdir></pre> 70 71 <p>The easiest solution, however, is to install JDK in a non-space directory, for example:</p> 72 73 <pre>c:\java\jdk1.7</pre> 74 75 <h2 id="DebugMode">Building in Debug Mode</h2> 76 77 <p>For immediate application testing and debugging, you can build your application in debug mode 78 and immediately install it on an emulator. In debug mode, the build tools automatically sign your 79 application with a debug key and optimize the package with {@code zipalign}.</p> 80 81 <p>To build in debug mode, open a command-line and navigate to the root of your project directory. 82 Use Gradle to build your project in debug mode, invoke the <code>assembleDebug</code> build task 83 using the Gradle wrapper script (<code>gradlew assembleRelease</code>). 84 85 <p>This creates your debug <code>.apk</code> file inside the module <code>build/</code> 86 directory, named <code><your_module_name>-debug.apk</code>. The file is already signed 87 with the debug key and has been aligned with 88 <a href="{@docRoot}tools/help/zipalign.html"><code>zipalign</code></a>. </p> 89 90 <p>On Windows platforms, type this command:</p> 91 92 <pre> 93 > gradlew.bat assembleDebug 94 </pre> 95 96 <p>On Mac OS and Linux platforms, type these commands:</p> 97 98 <pre> 99 $ chmod +x gradlew 100 $ ./gradlew assembleDebug 101 </pre> 102 103 <p>The first command (<code>chmod</code>) adds the execution permission to the Gradle wrapper 104 script and is only necessary the first time you build this project from the command line.</p> 105 106 <p>After you build the project, the output APK for the app module is located in 107 <code>app/build/outputs/apk/</code>, and the output AAR for any lib modules is located in 108 <code>lib/build/outputs/libs/</code>.</p> 109 110 <p>To see a list of all available build tasks for your project, type this command:</p> 111 112 <pre> 113 $ ./gradlew tasks 114 </pre> 115 116 <p>Each time you change a source file or resource, you must run Gradle again in order to package up 117 the latest version of the application.</p> 118 119 <p>To install and run your application on an emulator, see the section about <a href= 120 "{@docRoot}tools/building/building-studio.html">Running on the Emulator</a>.</p> 121 122 <h2 id="ReleaseMode">Building in Release Mode</h2> 123 124 <p>When you're ready to release and distribute your application to end-users, you must build your 125 application in release mode. Once you have built in release mode, it's a good idea to perform 126 additional testing and debugging with the final .apk.</p> 127 128 <p>Before you start building your application in release mode, be aware that you must sign the 129 resulting application package with your private key, and should then align it using the {@code 130 zipalign} tool. There are two approaches to building in release mode: build an unsigned package 131 in release mode and then manually sign and align the package, or allow the build script to sign 132 and align the package for you.</p> 133 134 <h3 id="ManualReleaseMode">Build unsigned</h3> 135 136 <p>If you build your application <em>unsigned</em>, then you will need to manually sign and align 137 the package.</p> 138 139 <p>To build an <em>unsigned</em> .apk in release mode, open a command-line and navigate to the 140 root of your module directory. Invoke the <code>assembleRelease</code> build task.</li> 141 142 <p>On Windows platforms, type this command:</p> 143 144 <pre> 145 > gradlew.bat assembleRelease 146 </pre> 147 148 <p>On Mac OS and Linux platforms, type this command:</p> 149 150 <pre> 151 $ ./gradlew assembleRelease 152 </pre> 153 154 155 <p>This creates your Android application .apk file inside the project <code>bin/</code> 156 directory, named <code><em><your_module_name></em>-unsigned.apk</code>.</p> 157 158 <p class="note"><strong>Note:</strong> The .apk file is <em>unsigned</em> at this point and can't 159 be installed until signed with your private key.</p> 160 161 <p>Once you have created the unsigned .apk, your next step is to sign the .apk with your private 162 key and then align it with {@code zipalign}. To complete this procedure, read <a href= 163 "{@docRoot}tools/publishing/app-signing.html">Signing Your Applications</a>.</p> 164 165 <p>When your <code>.apk</code> has been signed and aligned, it's ready to be distributed to end-users. 166 You should test the final build on different devices or AVDs to ensure that it 167 runs properly on different platforms.</p> 168 169 <h3 id="AutoReleaseMode">Build signed and aligned</h3> 170 171 <p>If you would like, you can configure the Android build script to automatically sign and align 172 your application package. To do so, you must provide the path to your keystore and the name of 173 your key alias in your modules's build.gradle file. With this information provided, 174 the build will prompt you for your keystore and alias password when you build using the release 175 build type and produce your final application package, which will be ready for distribution.</p> 176 177 <p>To specify your keystore and alias, open the module build.gradle file (found in 178 the root of the module directory) and add entries for {@code storeFile}, {@code storePassword}, 179 {@code keyAlias} and {@code keyPassword}. 180 For example:</p> 181 <pre> 182 storeFile file("myreleasekey.keystore") 183 keyAlias "MyReleaseKey" 184 </pre> 185 186 <p>Save your changes. Now you can build a <em>signed</em> .apk in release mode:</p> 187 188 <ol> 189 <li>Open a command-line and navigate to the root of your module directory.</li> 190 191 <li>Edit the build.gradle file to build your project in release mode: 192 <p><pre> 193 ... 194 android { 195 ... 196 defaultConfig { ... } 197 signingConfigs { 198 release { 199 storeFile file("myreleasekey.keystore") 200 storePassword "password" 201 keyAlias "MyReleaseKey" 202 keyPassword "password" 203 } 204 } 205 buildTypes { 206 release { 207 ... 208 signingConfig signingConfigs.release 209 } 210 } 211 } 212 ... 213 </pre></p> 214 </li> 215 216 <li>When prompted, enter you keystore and alias passwords. 217 218 <p class="caution"><strong>Caution:</strong> As described above, your password will be 219 visible on the screen.</p> 220 </li> 221 </ol> 222 223 <p>This creates your Android application .apk file inside the module <code>build/</code> 224 directory, named <code><em><your_module_name></em>-release.apk</code>. This .apk file has 225 been signed with the private key specified in build.gradle file and aligned with {@code 226 zipalign}. It's ready for installation and distribution.</p> 227 228 <h3 id="OnceBuilt">Once built and signed in release mode</h3> 229 230 <p>Once you have signed your application with a private key, you can install and run it on an 231 <a href="#RunningOnEmulator">emulator</a> or <a href="#RunningOnDevice">device</a>. You can 232 also try installing it onto a device from a web server. Simply upload the signed .apk to a web 233 site, then load the .apk URL in your Android web browser to download the application and begin 234 installation. (On your device, be sure you have enabled 235 <em>Settings > Applications > Unknown sources</em>.)</p> 236 237 <h2 id="RunningOnEmulator">Running on the Emulator</h2> 238 239 <p>Before you can run your application on the Android Emulator, you must <a href= 240 "{@docRoot}tools/devices/managing-avds.html">create an AVD</a>.</p> 241 242 <p>To run your application:</p> 243 244 <ol> 245 <li> 246 <strong>Open the AVD Manager and launch a virtual device</strong> 247 248 <p>From your SDK's <code>platform-tools/</code> directory, execute the {@code android} tool 249 with the <code>avd</code> options:</p> 250 <pre> 251 android avd 252 </pre> 253 254 <p>In the <em>Virtual Devices</em> view, select an AVD and click <strong>Start</strong>.</p> 255 </li> 256 257 <li> 258 <strong>Install your application</strong> 259 260 <p>From your SDK's <code>tools/</code> directory, install the {@code .apk} on the 261 emulator:</p> 262 <pre> 263 adb install <em><path_to_your_bin></em>.apk 264 </pre> 265 266 <p>Your .apk file (signed with either a release or debug key) is in your module {@code build/} 267 directory after you build your application.</p> 268 269 <p>If there is more than one emulator running, you must specify the emulator upon which to 270 install the application, by its serial number, with the <code>-s</code> option. For 271 example:</p> 272 <pre> 273 adb -s emulator-5554 install <em>path/to/your/app</em>.apk 274 </pre> 275 276 <p>To see a list of available device serial numbers, execute {@code adb devices}.</p> 277 </li> 278 </ol> 279 280 <p>If you don't see your application on the emulator, try closing the emulator and launching the 281 virtual device again from the AVD Manager. Sometimes when you install an application for the 282 first time, it won't show up in the application launcher or be accessible by other applications. 283 This is because the package manager usually examines manifests completely only on emulator 284 startup.</p> 285 286 <p>Be certain to create multiple AVDs upon which to test your application. You should have one 287 AVD for each platform and screen type with which your application is compatible. For instance, if 288 your application compiles against the Android 4.0 (API Level 14) platform, you should create an 289 AVD for each platform equal to and greater than 4.0 and an AVD for each <a href= 290 "{@docRoot}guide/practices/screens_support.html">screen type</a> you support, then test your 291 application on each one.</p> 292 293 <p class="note"><strong>Tip:</strong> If you have <em>only one</em> emulator running, you can 294 build your application and install it on the emulator in one simple step. Navigate to the root of 295 your project directory and use Ant to compile the project with <em>install mode</em>: <code>ant 296 install</code>. This will build your application, sign it with the debug key, and install it on 297 the currently running emulator.</p> 298 299 <h2 id="RunningOnDevice">Running on a Device</h2> 300 301 <p>Before you can run your application on a device, you must perform some basic setup for your 302 device:</p> 303 304 <ul> 305 <li>Enable <strong>USB debugging</strong> on your device. 306 <ul> 307 <li>On most devices running Android 3.2 or older, you can find the option under 308 <strong>Settings > Applications > Development</strong>.</li> 309 <li>On Android 4.0 and newer, it's in <strong>Settings > Developer options</strong>. 310 <p class="note"><strong>Note:</strong> On Android 4.2 and newer, <strong>Developer 311 options</strong> is hidden by default. To make it available, go 312 to <strong>Settings > About phone</strong> and tap <strong>Build number</strong> 313 seven times. Return to the previous screen to find <strong>Developer options</strong>.</p> 314 </li> 315 </ul> 316 </li> 317 318 <li>Ensure that your development computer can detect your device when connected via USB</li> 319 </ul> 320 321 <p>Read <a href="{@docRoot}tools/device.html#setting-up">Setting up a Device for 322 Development</a> for more information.</p> 323 324 <p>Once your device is set up and connected via USB, navigate to your SDK's <code>platform-tools/</code> 325 directory and install the <code>.apk</code> on the device:</p> 326 <pre> 327 adb -d install <em>path/to/your/app</em>.apk 328 </pre> 329 330 <p>The {@code -d} flag specifies that you want to use the attached device (in case you also have 331 an emulator running).</p> 332 333 <p>For more information on the tools used above, please see the following documents:</p> 334 335 <ul> 336 <li><a href="{@docRoot}tools/help/android.html">android Tool</a></li> 337 338 <li><a href="{@docRoot}tools/devices/emulator.html">Android Emulator</a></li> 339 340 <li><a href="{@docRoot}tools/help/adb.html">Android Debug Bridge</a> (ADB)</li> 341 </ul> 342 343 <h2 id="Signing">Application Signing</h2> 344 345 <p>As you begin developing Android applications, understand that all Android applications must be 346 digitally signed before the system will install them on an emulator or device. There are two ways 347 to do this: with a <em>debug key</em> (for immediate testing on an emulator or development 348 device) or with a <em>private key</em> (for application distribution).</p> 349 350 <p>The Android build tools help you get started by automatically signing your .apk files with a 351 debug key at build time. This means that you can build your application and install it on the 352 emulator without having to generate your own private key. However, please note that if you intend 353 to publish your application, you <strong>must</strong> sign the application with your own private 354 key, rather than the debug key generated by the SDK tools.</p> 355 356 <p>Please read <a href="{@docRoot}tools/publishing/app-signing.html">Signing Your 357 Applications</a>, which provides a thorough guide to application signing on Android and what it 358 means to you as an Android application developer. The document also includes a guide to publishing 359 and signing your application.</p> 360 361 <h2 id="PluginReference">Android Plugin for Gradle</h2> 362 363 <p>The Android build system uses the Android plugin for Gradle to support the Gradle Domain 364 Specific Language (DSL) and declarative language elements. See the 365 <a href="{@docRoot}tools/building/plugin-for-gradle.html">Android Plug-in for Gradle</a> section for 366 a description of the plugin and a link to the complete list of the supported Gradle DSL elements.</p> 367 368 369 370