Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 1 | page.title=Building and Running from Android Studio |
| 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> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 9 | <ol> |
Adarsh Fernando | 37cc54e | 2016-03-23 10:59:10 -0700 | [diff] [blame] | 10 | <li> |
| 11 | <a href="#run-configuration">Changing the run configuration</a> |
| 12 | </li> |
| 13 | |
| 14 | <li> |
| 15 | <a href="#changing-variant">Changing build variants</a> |
| 16 | </li> |
| 17 | |
| 18 | <li> |
| 19 | <a href="#gradle-console">Monitoring the build process</a> |
| 20 | </li> |
| 21 | |
| 22 | <li> |
| 23 | <a href="#generating-apks">Generating APKs</a> |
| 24 | </li> |
| 25 | |
| 26 | <li> |
| 27 | <a href="#instant-run">About Instant Run</a> |
| 28 | <ol> |
| 29 | <li> |
| 30 | <a href="#set-up-ir">Configuring and optimizing your project for Instant Run</a> |
| 31 | </li> |
| 32 | |
| 33 | <li> |
| 34 | <a href="#ir-limitations">Limitations of Instant Run</a> |
| 35 | </li> |
| 36 | </ol> |
| 37 | </li> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 38 | </ol> |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 39 | |
| 40 | <h2>See also</h2> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 41 | <ol> |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 42 | <li><a href="{@docRoot}sdk/installing/studio-build.html"> |
| 43 | Build System</a></li> |
| 44 | <li><a href="{@docRoot}tools/devices/managing-avds.html"> |
| 45 | Managing AVDs with AVD Manager</a></li> |
| 46 | <li><a href="{@docRoot}tools/devices/emulator.html"> |
| 47 | Using the Android Emulator</a></li> |
| 48 | <li><a href="{@docRoot}tools/device.html"> |
| 49 | Using Hardware Devices</a></li> |
| 50 | <li><a href="{@docRoot}tools/publishing/app-signing.html"> |
| 51 | Signing Your Applications</a></li> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 52 | </ol> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 53 | </div> |
| 54 | </div> |
| 55 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 56 | <p> |
| 57 | By default, Android Studio sets up new projects to deploy to the Emulator or |
Adarsh Fernando | 37cc54e | 2016-03-23 10:59:10 -0700 | [diff] [blame] | 58 | a physical device with just a few clicks. With Instant Run, you can push |
| 59 | changes to methods and existing app resources to a running app without |
| 60 | building a new APK, so code changes are visible almost instantly. |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 61 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 62 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 63 | <p> |
| 64 | To build and run your app, click <strong>Run 'app'</strong> <img src= |
| 65 | "{@docRoot}images/tools/as-run.png" alt="" style= |
| 66 | "vertical-align:bottom;margin:0;">. Android Studio builds your app with |
| 67 | Gradle, asks you to select a deployment target (an emulator or a connected |
| 68 | device), and then deploys your app to it. You can customize some of this |
| 69 | default behavior, such as selecting an automatic deployment target, by |
| 70 | <a href="#run-configuration">changing the run configuration</a>. |
| 71 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 72 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 73 | <p> |
| 74 | If you want to <a href="{@docRoot}tools/devices/emulator.html">use the Android |
| 75 | Emulator</a> to run your app, you need to have an Android Virtual Device |
| 76 | (AVD) ready. If you haven't already created one, then after you click |
| 77 | <strong>Run 'app'</strong>, click <strong>Create New Emulator</strong> in the |
| 78 | <strong>Select Deployment Target</strong> dialog. Follow the Virtual Device |
| 79 | Configuration wizard to define the type of device you want to emulate. For |
| 80 | more information, see <a href= |
| 81 | "{@docRoot}tools/devices/managing-avds.html">Managing AVDs with the AVD |
| 82 | Manager</a>. |
| 83 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 84 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 85 | <p> |
| 86 | If you're using a physical Android device, you need to enable USB debugging |
| 87 | on the device. For more information, see <a href= |
| 88 | "{@docRoot}tools/device.html">Using Hardware Devices</a>. |
| 89 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 90 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 91 | <p class="note"> |
| 92 | <strong>Note:</strong> You can also deploy your app in debug mode by clicking |
| 93 | <strong>Debug 'app'</strong> <img src= |
| 94 | "{@docRoot}images/tools/as-debugbutton.png" alt="" style= |
| 95 | "vertical-align:bottom;margin:0;">. Running your app in debug mode |
| 96 | allows you to set breakpoints in your code, examine variables and evaluate |
| 97 | expressions at run time, and run debugging tools. To learn more, read about |
| 98 | <a href="{@docRoot}tools/debugging/debugging-studio.html">Debugging with |
| 99 | Android Studio</a>. |
| 100 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 101 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 102 | <h3 id="run-configuration"> |
| 103 | Changing the run configuration |
| 104 | </h3> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 105 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 106 | <p> |
| 107 | The run configuration specifies the module to run, package to deploy, |
| 108 | activity to start, target device, emulator settings, and Logcat options. The |
| 109 | default run configuration launches the default project activity and uses the |
| 110 | <strong>Device Chooser</strong> for target device selection. If the default |
| 111 | settings don't suit your project or module, you can customize the run |
| 112 | configuration, or even create a new one, at the project, default, and module |
| 113 | levels. To edit a run configuration: |
| 114 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 115 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 116 | <ol> |
| 117 | <li>Select <strong>Run</strong> > <strong>Edit Configurations</strong>. |
| 118 | </li> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 119 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 120 | <li>Expand the <strong>Android Application</strong> item and select an |
| 121 | existing run configuration. |
| 122 | <ul> |
| 123 | <li>To create a new run configuration, click the '<strong>+</strong>' |
| 124 | button in the top left corner of the dialog box and select |
| 125 | <strong>Android Application</strong>. |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 126 | </li> |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 127 | </ul> |
| 128 | </li> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 129 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 130 | <li>With a run configuration selected, adjust your desired settings. For |
| 131 | example, in the <strong>General</strong> tab, you can specify the APK |
| 132 | installation settings, launch options, and deployment target options. |
| 133 | </li> |
| 134 | </ol> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 135 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 136 | <h3 id="changing-variant"> |
| 137 | Changing the build variant |
| 138 | </h3> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 139 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 140 | <p> |
| 141 | By default, Android Studio builds the debug version of your app, which is |
| 142 | intended only for testing, when you click <strong>Run 'app'</strong>. You |
| 143 | need to build the release version when <a href= |
| 144 | "{@docRoot}tools/publishing/preparing.html">preparing your app for public |
| 145 | release</a>. |
| 146 | </p> |
| 147 | <p> |
| 148 | To change the build variant Android Studio uses, go to <strong>Build</strong> |
| 149 | > <strong>Select Build Variant</strong> and select a different one from |
| 150 | the drop-down menu. By default, new projects are set up with a debug and |
| 151 | release build variant. |
| 152 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 153 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 154 | <p> |
| 155 | Using <em>product flavors</em>, you can create additional build variants for |
| 156 | different versions of your app, each having different features or device |
| 157 | requirements. To learn more about build variants and product flavors, read |
| 158 | <a href="{@docRoot}tools/building/configuring-gradle.html">Configuring Gradle |
| 159 | Builds</a>. |
| 160 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 161 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 162 | <h3 id="gradle-console"> |
| 163 | Monitoring the build process |
| 164 | </h3> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 165 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 166 | <p> |
Adarsh Fernando | 37cc54e | 2016-03-23 10:59:10 -0700 | [diff] [blame] | 167 | You can view details about the build process by clicking <strong>Gradle |
| 168 | Console</strong> <img src="{@docRoot}images/tools/as-gradlebutton.png" alt="" |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 169 | style="vertical-align:bottom;margin:0;">. The console displays each |
| 170 | task that Gradle executes in order to build your app, as shown in figure 1. |
| 171 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 172 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 173 | <img src="{@docRoot}images/tools/studio-gradle-console.png" alt=""> |
| 174 | <p class="img-caption"> |
| 175 | <strong>Figure 1.</strong> The Gradle Console in Android Studio. |
| 176 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 177 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 178 | <p> |
| 179 | If your build variants use product flavors, Gradle also invokes tasks to |
| 180 | build those product flavors. To view the list of all available build tasks, |
| 181 | click <strong>Gradle</strong> <img src= |
| 182 | "{@docRoot}images/tools/as-gradle.png" alt="" style= |
| 183 | "vertical-align:bottom;margin:0;"> on the right side of the IDE |
| 184 | window. |
| 185 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 186 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 187 | <p> |
| 188 | If an error occurs during the build process, the <em>Messages</em> window |
Adarsh Fernando | 37cc54e | 2016-03-23 10:59:10 -0700 | [diff] [blame] | 189 | appears to describe the issue. Gradle may recommend some command-line |
| 190 | options to help you resolve the issue, such as <code>--stacktrace</code> or |
| 191 | <code>--debug</code>. To use command-line options with your build process: |
| 192 | </p> |
| 193 | |
| 194 | <ol> |
| 195 | <li>Open the <strong>Settings</strong> or <strong>Preferences</strong> |
| 196 | dialog: |
| 197 | <ul> |
| 198 | <li>On Windows or Linux, select <strong>File</strong> > |
| 199 | <strong>Settings</strong> from the main menu. |
| 200 | </li> |
| 201 | |
| 202 | <li>On Mac OSX, select <strong>Android Studio</strong> > |
| 203 | <strong>Preferences</strong> from the main menu. |
| 204 | </li> |
| 205 | </ul> |
| 206 | </li> |
| 207 | |
| 208 | <li>Navigate to <strong>Build, Execution, Deployment</strong> > |
| 209 | <strong>Compiler</strong>. |
| 210 | </li> |
| 211 | |
| 212 | <li>In the text field next to <em>Command-line Options</em>, enter your |
| 213 | command-line options. |
| 214 | </li> |
| 215 | |
| 216 | <li>Click <strong>OK</strong> to save and exit. |
| 217 | </li> |
| 218 | </ol> |
| 219 | |
| 220 | <p> |
| 221 | Gradle will apply these command-line options the next time you try building |
| 222 | your app. |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 223 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 224 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 225 | <h3 id="generating-apks"> |
| 226 | Generating APKs |
| 227 | </h3> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 228 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 229 | <p> |
| 230 | When you click <strong>Run 'app'</strong>, Android Studio generates a debug |
| 231 | APK and deploys it to your target device. Before you can generate a release |
| 232 | version of your app for public distribution, however, you must first learn |
| 233 | how to <a href="{@docRoot}tools/publishing/app-signing.html#studio">sign your |
| 234 | app</a>. You can then generate multiple signed APKs of your debug or release |
| 235 | build variants. To locate the generated APK files, click the link in the |
| 236 | pop-up dialog, as shown in figure 2. |
| 237 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 238 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 239 | <p> |
| 240 | <img src="{@docRoot}images/tools/as-find-apk.png" alt=""> |
| 241 | </p> |
Rich Slogar | 4083327 | 2014-11-06 17:15:28 -0800 | [diff] [blame] | 242 | |
Adarsh Fernando | 8f13c20 | 2016-03-19 14:22:36 -0700 | [diff] [blame] | 243 | <p class="img-caption"> |
| 244 | <strong>Figure 2.</strong> Click the link to locate the generated APK |
| 245 | files. |
Adarsh Fernando | 37cc54e | 2016-03-23 10:59:10 -0700 | [diff] [blame] | 246 | </p> |
| 247 | |
| 248 | <h2 id="instant-run">About Instant Run</h3> |
| 249 | |
| 250 | <p> |
| 251 | Introduced in Android Studio 2.0, Instant Run is a behavior for the |
| 252 | <strong>Run</strong> <img src="{@docRoot}images/tools/as-run.png" alt="" |
| 253 | style="vertical-align:bottom;margin:0;"> and <strong>Debug</strong> <img src= |
| 254 | "{@docRoot}images/tools/as-debugbutton.png" alt="" style= |
| 255 | "vertical-align:bottom;margin:0;"> commands that significantly reduces the |
Adarsh Fernando | 29dfc1d | 2016-04-06 13:58:34 -0700 | [diff] [blame] | 256 | time between updates to your app. Although your first build may take longer |
| 257 | to complete, Instant Run pushes subsequent updates to your app without |
| 258 | building a new APK, so changes are visible much more quickly. |
Adarsh Fernando | 37cc54e | 2016-03-23 10:59:10 -0700 | [diff] [blame] | 259 | </p> |
| 260 | |
| 261 | <p> |
| 262 | Instant Run is supported only when you deploy the debug build variant, use |
| 263 | Android Plugin for Gradle version 2.0.0 or higher, and set |
| 264 | <code>minSdkVersion</code> to 15 or higher in your app's module-level |
| 265 | <code>build.gradle</code> file. For the best performance, set |
| 266 | <code>minSdkVersion</code> to 21 or higher. |
| 267 | </p> |
| 268 | |
| 269 | <p> |
| 270 | After deploying an app, a small, yellow thunderbolt icon appears within the |
| 271 | <strong>Run</strong> <img src= |
| 272 | "{@docRoot}images/tools/instant-run/as-irrun.png" alt="" style= |
| 273 | "vertical-align:bottom;margin:0;"> button (or <strong>Debug</strong> |
| 274 | <img src="{@docRoot}images/tools/instant-run/as-irdebug.png" alt="" style= |
| 275 | "vertical-align:bottom;margin:0;"> button), indicating that Instant Run is |
| 276 | ready to push updates the next time you click the button. Instead of building |
| 277 | a new APK, it pushes just those new changes and, in some cases, the app |
| 278 | doesn't even need to restart but immediately shows the effect of those code |
| 279 | changes. |
| 280 | </p> |
| 281 | |
| 282 | <p> |
| 283 | Instant Run pushes updated code and resources to your connected device or |
| 284 | emulator by performing a <em>hot swap</em>, <em>warm swap</em>, or <em>cold |
| 285 | swap</em>. It automatically determines the type of swap to perform based on |
| 286 | the type of change you made. The following table describes how Instant Run |
| 287 | behaves when you push certain code changes to a target device. |
| 288 | </p> |
| 289 | |
| 290 | <table id="ir-table"> |
| 291 | <col width="40%"> |
| 292 | <tr> |
| 293 | <th scope="col"> |
| 294 | Code Change |
| 295 | </th> |
| 296 | <th scope="col"> |
| 297 | Instant Run Behavior |
| 298 | </th> |
| 299 | </tr> |
| 300 | |
| 301 | <tr id="hot-swap"> |
| 302 | <td> |
| 303 | <ul> |
| 304 | <li>Change implementation code of an existing method |
| 305 | </li> |
| 306 | </ul> |
| 307 | </td> |
| 308 | <td> |
| 309 | <p> |
| 310 | Supported with <strong>hot swap</strong>: This is the |
| 311 | fastest type of swap and makes changes visible much more quickly. Your |
| 312 | application keeps running and a stub method with the new implementation is used |
| 313 | the next time the method is called. |
| 314 | </p> |
| 315 | |
| 316 | <p> |
| 317 | Hot swaps do not re-initialize objects in your running app. You may need to |
| 318 | restart the current activity, or <a href="#rerun">restart the app</a>, before |
| 319 | you see certain updates. By default, Android Studio automatically restarts the |
| 320 | current activity after performing a hot swap. If you do not want this behavior, |
| 321 | you can <a href="#activity-restart">disable automatic activity restarts</a>. |
| 322 | </p> |
| 323 | </td> |
| 324 | </tr> |
| 325 | |
| 326 | <tr id="warm-swap"> |
| 327 | <td> |
| 328 | <ul> |
| 329 | <li>Change or remove an existing resource |
| 330 | </li> |
| 331 | </ul> |
| 332 | </td> |
| 333 | <td> |
| 334 | Supported with <strong>warm swap</strong>: This swap |
| 335 | is still very fast, but Instant Run must restart the current activity when it |
| 336 | pushes the changed resources to your app. Your app keeps running, but a small |
| 337 | flicker may appear on the screen as the activity restarts—this is normal. |
| 338 | </td> |
| 339 | </tr> |
| 340 | |
| 341 | <tr id="cold-swap"> |
| 342 | <td> |
| 343 | Structural code changes, such as: |
| 344 | <ul> |
| 345 | <li>Add, remove, or change: |
| 346 | <ul> |
| 347 | <li>an annotation |
| 348 | </li> |
| 349 | |
| 350 | <li>an instance field |
| 351 | </li> |
| 352 | |
| 353 | <li>a static field |
| 354 | </li> |
| 355 | |
| 356 | <li>a static method signature |
| 357 | </li> |
| 358 | |
| 359 | <li>an instance method signature |
| 360 | </li> |
| 361 | </ul> |
| 362 | </li> |
| 363 | |
| 364 | <li>Change which parent class the current class inherits from |
| 365 | </li> |
| 366 | |
| 367 | <li>Change the list of implemented interfaces |
| 368 | </li> |
| 369 | |
| 370 | <li>Change a class's static initializer |
| 371 | </li> |
| 372 | |
| 373 | <li>Reorder layout elements that use dynamic resource IDs |
| 374 | </li> |
| 375 | </ul> |
| 376 | </td> |
| 377 | <td> |
| 378 | <p> |
| 379 | Supported with <strong>cold swap</strong> (API level 21 or higher): This swap |
| 380 | is a bit slower because, although a new APK is not required, Instant Run must |
| 381 | restart the whole app when it pushes structural code changes. |
| 382 | </p> |
| 383 | |
| 384 | <p> |
| 385 | For target devices running API level 20 or lower, Android Studio |
| 386 | deploys a full APK. |
| 387 | </p> |
| 388 | </td> |
| 389 | </tr> |
| 390 | |
| 391 | <tr> |
| 392 | <td> |
| 393 | <ul> |
| 394 | <li>Change the app manifest |
| 395 | </li> |
| 396 | |
| 397 | <li>Change resources reference by the app manifest |
| 398 | </li> |
| 399 | |
| 400 | <li>Change an Android widget UI element (requires a <a href="#rerun"> |
| 401 | Clean and Rerun</a>) |
| 402 | </li> |
| 403 | </ul> |
| 404 | </td> |
| 405 | <td> |
| 406 | <p> |
| 407 | When making changes to the app's manifest or resources referenced by the |
| 408 | manifest, Android Studio automatically <strong>deploys a new build</strong> |
| 409 | in order to apply these changes. This is because certain information about |
| 410 | the app, such as its name, app icon resources, and intent filters, are |
| 411 | determined from the manifest when the APK is installed on the device. |
| 412 | </p> |
| 413 | |
| 414 | <p> |
| 415 | If your build process automatically updates any part of the app manifest, |
| 416 | such as automatically iterating <code>versionCode</code> or |
| 417 | <code>versionName</code>, you will not be able to benefit from the full |
| 418 | performance of Instant Run. We recommend that you disable automatic updates |
| 419 | to any part in the app manifest in your debug build variants. |
| 420 | </p> |
| 421 | </td> |
| 422 | </tr> |
| 423 | </table> |
| 424 | |
| 425 | <p class="note"> |
| 426 | <strong>Note:</strong> If you need to restart your app after a crash, do not |
| 427 | launch it from your target device. Restarting your app from your target |
| 428 | device does not apply any of your code changes since the last cold swap or |
| 429 | <em>incremental build</em>. To launch your app with all your recent changes, |
| 430 | click <strong>Run</strong> <img src="{@docRoot}images/tools/as-run.png" alt= |
| 431 | "" style="vertical-align:bottom;margin:0;"> (or <strong>Debug</strong> |
| 432 | <img src="{@docRoot}images/tools/as-debugbutton.png" alt="" style= |
| 433 | "vertical-align:bottom;margin:0;">) from Android Studio. |
| 434 | </p> |
| 435 | |
| 436 | <h4 id="rerun"> |
| 437 | Using Rerun |
| 438 | </h4> |
| 439 | |
| 440 | <p> |
| 441 | When pushing code changes that affect certain initializers, such as changes |
| 442 | to an app's {@link android.app.Application#onCreate onCreate()} method, you |
| 443 | need to restart your app for the changes to take effect. To perform an |
| 444 | <em>incremental build</em> and restart the app, click <strong>Rerun</strong> |
| 445 | <img src="{@docRoot}images/tools/as-restart.png" alt="" style= |
| 446 | "vertical-align:bottom;margin:0;">. |
| 447 | </p> |
| 448 | |
| 449 | <p> |
| 450 | If you need to deploy a <em>clean build</em>, select <strong>Run</strong> |
| 451 | > <strong>Clean and Rerun 'app'</strong> <img src= |
| 452 | "{@docRoot}images/tools/as-cleanrerun.png" alt="" style= |
| 453 | "vertical-align:bottom;margin:0;"> from the main menu, or hold down the |
| 454 | <strong>Shift</strong> key while clicking <strong>Rerun</strong> <img src= |
| 455 | "{@docRoot}images/tools/as-restart.png" alt="" style= |
| 456 | "vertical-align:bottom;margin:0;">. This action stops the running app, |
| 457 | performs a full clean build, and deploys the new APK to your target device. |
| 458 | </p> |
| 459 | |
| 460 | <h4 id="activity-restart"> |
| 461 | Disabling automatic activity restart |
| 462 | </h4> |
| 463 | |
| 464 | <p> |
| 465 | When performing a hot swap, your app keeps running but Android Studio |
| 466 | automatically restarts the current activity. To disable this default setting: |
| 467 | </p> |
| 468 | |
| 469 | <ol> |
| 470 | <li>Open the <strong>Settings</strong> or <strong>Preferences</strong> |
| 471 | dialog: |
| 472 | <ul> |
| 473 | <li>On Windows or Linux, select <strong>File</strong> > |
| 474 | <strong>Settings</strong> from the main menu. |
| 475 | </li> |
| 476 | |
| 477 | <li>On Mac OSX, select <strong>Android Studio</strong> > |
| 478 | <strong>Preferences</strong> from the main menu. |
| 479 | </li> |
| 480 | </ul> |
| 481 | </li> |
| 482 | |
| 483 | <li>Navigate to <strong>Build, Execution, Deployment</strong> > |
| 484 | <strong>Instant Run</strong>. |
| 485 | </li> |
| 486 | |
| 487 | <li>Uncheck the box next to <strong>Restart activity on code |
| 488 | changes</strong>. |
| 489 | </li> |
| 490 | </ol> |
| 491 | |
| 492 | <p> |
| 493 | If automatic activity restart is disabled, you can manually restart the current |
| 494 | activity from the menu bar by selecting <strong>Run</strong> > <strong>Restart |
| 495 | Activity</strong>. |
| 496 | </p> |
| 497 | |
| 498 | <h3 id="set-up-ir"> |
| 499 | Configuring and optimizing your project for Instant Run |
| 500 | </h3> |
| 501 | |
| 502 | <p> |
| 503 | Android Studio enables Instant Run by default for projects built using |
| 504 | Android Plugin for Gradle 2.0.0 and higher. |
| 505 | </p> |
| 506 | |
| 507 | <p> |
| 508 | To update an existing project with the latest version of the plugin: |
| 509 | </p> |
| 510 | |
| 511 | <ol> |
| 512 | <li>Open the <strong>Settings</strong> or <strong>Preferences</strong> |
| 513 | dialog. |
| 514 | </li> |
| 515 | |
| 516 | <li> |
| 517 | <p> |
| 518 | Navigate to <strong>Build, Execution, Deployment</strong> > |
| 519 | <strong>Instant Run</strong> and click <strong>Update Project</strong>, |
| 520 | as shown in figure 3. |
| 521 | </p> |
| 522 | |
| 523 | <p> |
| 524 | If the option to update the project does not appear, it’s already |
| 525 | up-to-date with the latest Android Plugin for Gradle. |
| 526 | </p> |
| 527 | |
| 528 | <img src="{@docRoot}images/tools/instant-run/update-project-dialog.png" |
| 529 | alt="" height="51"> |
| 530 | |
| 531 | <p class="img-caption"> |
| 532 | <strong>Figure 3.</strong> Updating the Android Plugin for Gradle for an |
| 533 | existing project. |
| 534 | </p> |
| 535 | </li> |
| 536 | </ol> |
| 537 | |
| 538 | <p> |
| 539 | You also need to <a href="#changing-variant">change the build variant</a> to |
| 540 | a debug version of your app to start using Instant Run. |
| 541 | </p> |
| 542 | |
| 543 | <h4 id="configure-dexoptions"> |
| 544 | Improve build times by configuring DEX resources |
| 545 | </h4> |
| 546 | |
| 547 | <p> |
| 548 | When you deploy a clean build, Android Studio instruments your app to allow |
| 549 | Instant Run to push code and resource updates. Although updating the running |
| 550 | app happens much more quickly, the first build may take longer to complete. |
| 551 | You can improve the build process by configuring a few DEX options, such as |
| 552 | <code>maxProcessCount</code> and <code>javaMaxHeapSize</code>. |
| 553 | </p> |
| 554 | |
| 555 | <dl> |
| 556 | <dt> |
| 557 | <code>maxProcessCount</code> |
| 558 | </dt> |
| 559 | |
| 560 | <dd> |
| 561 | Sets the maximum number of DEX processes that can be started |
| 562 | concurrently. If the Gradle daemon is already running, you need to |
| 563 | stop the process before initializing it with a new maximum process count. |
| 564 | You can terminate the Gradle daemon by calling one of the following from |
| 565 | the <em>Terminal</em> window: |
| 566 | <ul> |
| 567 | <li>On Windows, call <code>gradlew --stop</code> |
| 568 | </li> |
| 569 | |
| 570 | <li>On Linux/Mac OSX, call <code>./gradlew --stop</code> |
| 571 | </li> |
| 572 | </ul> |
| 573 | </dd> |
| 574 | |
| 575 | <dt> |
| 576 | <code>javaMaxHeapSize</code> |
| 577 | </dt> |
| 578 | |
| 579 | <dd> |
| 580 | Sets the maximum memory allocation pool size for the dex operation. When |
| 581 | passing a value, you can append the letter 'k' to indicate kilobytes, 'm' |
| 582 | to indicate megabytes, or 'g' to indicate gigabytes. |
| 583 | </dd> |
| 584 | </dl> |
| 585 | |
| 586 | <p> |
| 587 | The following example sets <code>maxProcessCount</code> to 4 and |
| 588 | <code>javaMaxHeapSize</code> to "2g" in the module-level |
| 589 | <code>build.gradle</code> file: |
| 590 | </p> |
| 591 | |
| 592 | <pre> |
| 593 | android { |
| 594 | ... |
| 595 | dexOptions { |
| 596 | maxProcessCount 8 |
| 597 | javaMaxHeapSize "2g" |
| 598 | } |
| 599 | } |
| 600 | </pre> |
| 601 | |
| 602 | <p> |
| 603 | You should experiment with these settings by incrementing their values and |
| 604 | observing the effect on your build times. You could experience a negative |
| 605 | impact to performance if you allocate too many resources to the DEX'ing process. |
| 606 | </p> |
| 607 | |
| 608 | <h4 id="windows-defender"> |
| 609 | Excluding your project from Windows Defender |
| 610 | </h4> |
| 611 | |
| 612 | <p> |
| 613 | On Windows systems, Windows Defender may cause slowdowns while using Instant |
| 614 | Run. If you are using Windows Defender, you should <a class="external-link" |
| 615 | href= |
| 616 | "http://answers.microsoft.com/en-us/protect/wiki/protect_defender-protect_scanning/how-to-exclude-a-filefolder-from-windows-defender/f32ee18f-a012-4f02-8611-0737570e8eee"> |
| 617 | exclude your Android Studio project folder from Windows Defender malware |
| 618 | scans</a>. |
| 619 | </p> |
| 620 | |
| 621 | <h4 id="crashlytics"> |
| 622 | Disabling Crashlytics for your debug build variant |
| 623 | </h4> |
| 624 | |
| 625 | <p> |
| 626 | Using Crashlytics is known to cause slower build times. To improve build |
| 627 | performance while developing your app, you can <a class="external-link" href= |
| 628 | "https://docs.fabric.io/android/crashlytics/build-tools.html#disabling-crashlytics-for-debug-builds"> |
| 629 | disable Crashlytics for your debug build variant</a>. |
| 630 | </p> |
| 631 | |
| 632 | <h3 id="ir-limitations"> |
| 633 | Limitations of Instant Run |
| 634 | </h3> |
| 635 | |
| 636 | <p> |
| 637 | Instant Run is designed to speed up the build and deploy process in most |
| 638 | situations. However, there are some aspects to using Instant Run that might |
| 639 | affect its behavior and compatibility with your app. If you experience any |
| 640 | other issues while using Instant Run, please <a class="external-link" href= |
| 641 | "http://tools.android.com/filing-bugs">file a bug</a>. |
| 642 | </p> |
| 643 | |
| 644 | <h4 id="multiple-devices"> |
| 645 | Deploying to multiple devices |
| 646 | </h4> |
| 647 | |
| 648 | <p> |
| 649 | Instant Run uses different techniques to perform hot, warm, and cold swaps |
| 650 | that are specific to the API level of the target device. For this reason, |
| 651 | while deploying an app to multiple devices at once, Android Studio |
| 652 | temporarily turns off Instant Run. |
| 653 | </p> |
| 654 | |
| 655 | <h4 id="ir-multidex"> |
| 656 | Multidexing your app |
| 657 | </h4> |
| 658 | |
| 659 | <p> |
| 660 | If your project is configured for <a href= |
| 661 | "{@docRoot}tools/building/multidex.html#mdex-pre-l">Legacy Multidex</a>—that |
| 662 | is, when <code>build.gradle</code> is configured with <code>multiDexEnabled |
| 663 | true</code> and <code>minSdkVersion 20</code> or lower—and you deploy to |
| 664 | target devices running Android 4.4 (API level 20) or lower, Android Studio |
| 665 | disables Instant Run. |
| 666 | </p> |
| 667 | |
| 668 | <p> |
| 669 | If <code>minSdkVersion</code> is set to 21 or higher, Instant Run |
| 670 | automatically configures your app for multidex. Because Instant Run only |
| 671 | works with the debug version of your app, you may need to <a href= |
| 672 | "{@docRoot}tools/building/multidex.html#mdex-gradle">configure your app for |
| 673 | multidex</a> when deploying your release build variant. |
| 674 | </p> |
| 675 | |
| 676 | <h4 id="instrumented-tests"> |
| 677 | Running instrumented tests and performance profilers |
| 678 | </h4> |
| 679 | |
| 680 | <p> |
| 681 | Instrumented tests load both the debug APK and a test APK into the same |
| 682 | process on a test device, allowing control methods to override the normal |
| 683 | lifecycle of the app and perform tests. While running or debugging |
| 684 | instrumented tests, Android Studio does not inject the additional methods |
| 685 | required for Instant Run and turns the feature off. |
| 686 | </p> |
| 687 | |
| 688 | <p> |
| 689 | While profiling an app, you should disable Instant Run. There is a small |
| 690 | performance impact when using Instant Run and a slightly larger impact when |
| 691 | overriding methods with a hot swap. This performance impact could interfere |
| 692 | with information provided by performance profiling tools. Additionally, the |
| 693 | stub methods generated with each hot swap can complicate stack traces. |
| 694 | </p> |
| 695 | |
| 696 | <h4 id="plugins"> |
| 697 | Using third-party plugins |
| 698 | </h4> |
| 699 | |
| 700 | <p> |
| 701 | Android Studio temporarily disables the Java Code Coverage Library (JaCoCo) |
| 702 | and ProGuard while using Instant Run. Because Instant Run only works with |
| 703 | debug builds, this does not affect your release build. |
| 704 | </p> |
| 705 | |
| 706 | <p> |
| 707 | Certain third-party plugins that perform bytecode enhancement may cause |
| 708 | issues with how Instant Run instruments your app. If you experience these |
| 709 | issues, but want to continue using Instant Run, you should disable those |
| 710 | plugins for your debug build variant. You can also help improve compatibility |
| 711 | with third-party plugins by <a class="external-link" href= |
| 712 | "http://tools.android.com/filing-bugs">filing a bug</a>. |
| 713 | </p> |
| 714 | |
| 715 | <h4 id="multi-process-apps"> |
| 716 | Pushing changes to multi-process apps |
| 717 | </h4> |
| 718 | |
| 719 | <p> |
| 720 | Instant Run only instruments your app's main process in order to perform hot |
| 721 | swaps and warm swaps. When pushing code changes to other app processes, such |
| 722 | as changes to a method implementation or an existing resource, Instant Run |
| 723 | performs a <a href="#cold-swap">cold swap</a>. |
| 724 | </p> |
| 725 | |
| 726 | <h4 id="disable-ir"> |
| 727 | Disabling Instant Run |
| 728 | </h4> |
| 729 | |
| 730 | <p> |
| 731 | To disable Instant Run: |
| 732 | </p> |
| 733 | |
| 734 | <ol> |
| 735 | <li>Open the <strong>Settings</strong> or <strong>Preferences</strong> |
| 736 | dialog. |
| 737 | </li> |
| 738 | |
| 739 | <li>Navigate to <strong>Build, Execution, Deployment</strong> > |
| 740 | <strong>Instant Run</strong>. |
| 741 | </li> |
| 742 | |
| 743 | <li>Uncheck the box next to <strong>Enable Instant Run</strong>. |
| 744 | </li> |
| 745 | </ol> |