1page.title=Building and Running from Android Studio 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="#buildProject">Building your Project in Android Studio</a> 11 <ol> 12 <li><a href="#buildRelease">Build a release version</a> </li> 13 </ol> 14 </li> 15 <li><a href=id="RunningApp">Running your App</a> 16 <ol> 17 <li><a href="#RunConfig">Creating a Run Configuration</a></li> 18 <li><a href="#AutoAndManualTargetModes">Automatic and manual target modes</a></li> 19 <li><a href="#RunningOnEmulatorStudio">Running on an Emulator</a></li> 20 <li><a href="#RunningOnDeviceStudio">Running on a Device</a></li> 21 </ol> 22 </li> 23 </ol> 24 25 <h2>See also</h2> 26 <ol> 27 <li><a href="{@docRoot}sdk/installing/studio-build.html"> 28 Build System</a></li> 29 <li><a href="{@docRoot}tools/devices/managing-avds.html"> 30 Managing AVDs with AVD Manager</a></li> 31 <li><a href="{@docRoot}tools/devices/emulator.html"> 32 Using the Android Emulator</a></li> 33 <li><a href="{@docRoot}tools/publishing/app-signing.html"> 34 Signing Your Applications</a></li> 35 </ol> 36 37 </div> 38</div> 39 40 41<p>This document shows you how to use Android Studio to build an application <code>.apk</code> for 42testing or release and how to run your application on an emulator or a real device.</p> 43 44 45<h2 id="buildProject">Build your Project in Android Studio</h2> 46 47<p>To build the project on Android Studio, click <strong>Build</strong> and select 48<strong>Make Project</strong>. The status bar at the bottom of the window shows the current 49progress of the build:</p> 50 51<p><code>Gradle: Executing tasks: [:app:assembleDebug, :lib:bundleDebug]</code></p> 52 53<p>Click <img src="{@docRoot}images/tools/as-gradlebutton.png" alt="" 54style="vertical-align:bottom;margin:0;"/> on the bottom 55right part of the window to show the <em>Gradle Console</em>, as shown in figure 1.</p> 56 57<img src="{@docRoot}images/tools/studio-gradle-console.png" alt="" /> 58<p class="img-caption"><strong>Figure 1.</strong> The Gradle Console in Android Studio.</p> 59 60<p>The Gradle Console shows the build tasks and subtasks that the build system runs for 61Android Studio. If the build fails, you can find more details on the console. To hide the Gradle 62Console, click <img src="{@docRoot}images/tools/as-gradlebutton.png" alt="" 63style="vertical-align:bottom;margin:0;"/> again.</p> 64 65<p class="note">If your project uses product flavors, Android Studio invokes the task for the 66selected build variant. For more information, see the 67<a href="{@docRoot}sdk/installing/studio-build.html">Build System</a> guide.</p> 68 69<p>To view the list of all available build tasks in Android Studio, click <strong>Gradle</strong> 70on the right side of the IDE window. The <em>Gradle tasks</em> panel appears.</p> 71 72 73<h3 id="buildRelease">Build a release version</h3> 74 75<p>You can now use the <strong>Build</strong> menu options to build the release version of your 76application for distribution. </p> 77 78<p>The build generates an APK for each build variant: 79the <code>app/build/apk/</code> directory contains packages named 80<code>app-<flavor>-<buildtype>.apk</code>; for example, <code>app-full-release.apk</code> and 81<code>app-demo-debug.apk</code>.</p> 82 83 84<p>For more build system information, see 85<a href="{@docRoot}sdk/installing/studio-build.html">Build System</a>.</p> 86 87 88 <h2 id="RunningApp">Running your app</h2> 89 90 <p>This section shows you how to run your application on an emulator or a real device 91 from Android Studio—all of which is done using the debug version of your application. 92 For more information about how to sign your application with a private key for release, see 93 <a href="{@docRoot}tools/publishing/app-signing.html">Signing Your Applications</a></p> 94 95 96 <h3 id="RunConfig">Creating a Run Configuration</h3> 97 98 <p>The run configuration specifies the module to run, package to deploy, Activity to start, 99 target device, emulator settings, and Logcat options. Run configuration can be set at the project, 100 default, and module levels. When you first run a module as an <em>Android Application</em>, 101 Android Studio will automatically create a run configuration. The default run 102 configuration will launch the default project Activity and use automatic target mode for device 103 selection (with no preferred AVD). If the default settings don't suit your project or module, you 104 can customize the run configuration or even create a new one.</p> 105 106 <p>To create or modify a run configuration, see the IntelliJ documentation on 107 <a href="https://www.jetbrains.com/idea/webhelp/run-debug-configuration-android-application.html"> 108 Run/Debug configurations</a>.</p> 109 110 <p>The following steps highlight the important things you need to do for an Android project:</p> 111 112 <ol> 113 <li>Open <strong>Edit</strong> Configurations from the <strong>Run</strong> Menu.</li> 114 115 <li>Expand the <strong>Android Application</strong> item and create a new configuration or open 116 an existing one.</li> 117 118 <li>With the Run Configuration selected, adjust your desired run configuration settings: 119 <ul> 120 <li>In the General tab, specify the Module settings to launch. In Target tab, consider whether 121 you'd like to use Manual or Automatic mode when selecting an AVD to run your application. See 122 the following section on <a href="#AutoAndManualTargetModes">Automatic and manual target modes</a>). 123 </li> 124 <li>In the Emulator tab, specify any emulator options to the Additional Emulator Command Line 125 Options field. For example, you could add <code>-scale 96dpi</code> to scale the AVD's screen 126 to an accurate size, based on the dpi of your computer monitor. For a full list of emulator 127 options, see the <a href="{@docRoot}tools/help/emulator.html">Android 128 Emulator</a> document.</p> 129 </li> 130 <li>In the Logcat tab, set the LogCat options for the application. </li> 131 </ul> 132 </li> 133 </ol> 134 135 136 <h3 id="AutoAndManualTargetModes">Automatic and manual target modes</h3> 137 138 <p>By default, a run configuration uses the <strong>automatic</strong> target mode in order to 139 select an AVD. In this mode, Android Studio will select an AVD for the application in the following 140 manner:</p> 141 142 <ol> 143 <li>If there's a device or emulator already running and its AVD configuration meets the 144 requirements of the application's build target, the application is installed and run upon 145 it.</li> 146 147 <li>If there's more than one device or emulator running, each of which meets the requirements 148 of the build target, a device chooser is shown to let you select which device to use.</li> 149 150 <li>If there are no devices or emulators running that meet the requirements of the build 151 target, Android Studio looks at the available AVDs. If there is an AVD that matches the build 152 target of the project, Android Studio chooses that AVD. If the AVD versions are newer than the 153 build target of the project, Android Studio chooses the oldest possible version of an AVD that 154 meets the project or module build target requirement.</li> 155 156 <li>If there are no suitable AVDs, the application is not installed and a console error warning 157 tells you that there is no existing AVD that meets the build target requirements.</li> 158 </ol> 159 160 <p>However, if a "preferred" AVD is selected in the run configuration, then the application will 161 <em>always</em> be deployed to that AVD. If it's not already running, then a new emulator will be 162 launched.</p> 163 164 <p>If your run configuration uses <strong>manual</strong> mode, then the "device chooser" is 165 presented every time that your application is run, so that you can select which AVD to use.</p> 166 167 168 169 <h3 id="RunningOnEmulatorStudio">Running on the emulator</h3> 170 171 <p>Before you can run your application on the Android Emulator, you verify the default AVD or 172 <a href="{@docRoot}tools/devices/managing-avds.html">create an AVD</a>.</p> 173 174 <p>To run (or debug) your application, select <strong>Run</strong> > <strong>Run</strong> (or 175 <strong>Run</strong> > <strong>debug</strong>) from the Android Studio menu bar. Android Studio 176 automatically creates a default run configuration for the project. Android Studio will then perform 177 the following:</p> 178 179 <ol> 180 <li>Compile the project (if there have been changes since the last build).</li> 181 182 <li>Create a default run configuration (if one does not already exist for the project).</li> 183 184 <li>Install and start the application on an emulator (or device), based on the Deployment 185 Target defined by the run configuration. 186 187 <p>By default, Android run configurations use an "automatic target" mode for selecting a 188 device target. For information on how automatic target mode selects a deployment target, see 189 <a href="#AutoAndManualTargetModes">Automatic and manual target modes</a> above.</p> 190 </li> 191 </ol> 192 193 <p>If you run the application with <strong>Debug</strong>, the <em>Choose a Device</em> option 194 appears so you can select an attached device or emulator. Once the device or emulator is selected, 195 Android Studio opens the Debug console and starts the application's main activity. Otherwise, if 196 you run the application with the normal Run command, Android Studio installs the application on the 197 device and launches the main activity.</p> 198 199 <p>To set or change the run configuration used for your project or module, select 200 <strong>Run</strong> > <strong>Edit Configurations</strong>. See the section below about 201 <a href="#RunConfig">Creating a Run Configuration</a> for more information.</p> 202 203 <p>Be certain to create multiple AVDs upon which to test your application. You should have one 204 AVD for each platform and screen type with which your application is compatible. For instance, if 205 your application compiles against the Android 4.0 (API Level 14) platform, you should create an 206 AVD for each platform equal to and greater than 4.0 and an AVD for each <a href= 207 "{@docRoot}guide/practices/screens_support.html">screen type</a> you support, then test your 208 application on each one.</p> 209 210 <h3 id="RunningOnDeviceStudio">Running on a device</h3> 211 212 <p>Before you can run your application on a device, you must perform some basic setup for your 213 device:</p> 214 215 <ul> 216 <li>Ensure that your application is debuggable by setting the 217 <code>android:debuggable</code> attribute of the <code><application></code> 218 element to <code>true</code> in the build.gradle file. </li> 219 220 <li>Enable <strong>USB debugging</strong> on your device. 221 <ul> 222 <li>On most devices running Android 3.2 or older, you can find the option under 223 <strong>Settings > Applications > Development</strong>.</li> 224 <li>On Android 4.0 and newer, it's in <strong>Settings > Developer options</strong>. 225 <p class="note"><strong>Note:</strong> On Android 4.2 and newer, <strong>Developer 226 options</strong> is hidden by default. To make it available, go 227 to <strong>Settings > About phone</strong> and tap <strong>Build number</strong> 228 seven times. Return to the previous screen to find <strong>Developer options</strong>.</p> 229 </li> 230 </ul> 231 </li> 232 233 <li>Ensure that your development computer can detect your device when connected via USB</li> 234 </ul> 235 236 <p>Read <a href="{@docRoot}tools/device.html">Using Hardware Devices</a> 237 for more information.</p> 238 239 <p>Once set up and your device is connected via USB, install your application on the device by 240 selecting <strong>Run</strong> > <strong>Run</strong> (or <strong>Run</strong> > 241 <strong>Debug</strong>) from the Android Studio menu bar.</p> 242 243 244