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