| page.title=Building and Running from the Command Line | |
| parent.title=Building and Running | |
| parent.link=index.html | |
| @jd:body | |
| <div id="qv-wrapper"> | |
| <div id="qv"> | |
| <h2>In this document</h2> | |
| <ol> | |
| <li><a href="#DebugMode">Building in Debug Mode</a></li> | |
| <li><a href="#ReleaseMode">Building in Release Mode</a> | |
| <ol> | |
| <li><a href="#ManualReleaseMode">Build unsigned</a></li> | |
| <li><a href="#AutoReleaseMode">Build signed and aligned</a></li> | |
| <li><a href="#OnceBuilt">Once built and signed in release mode</a></li> | |
| </ol> | |
| </li> | |
| <li><a href="#RunningOnEmulator">Running on the Emulator</a></li> | |
| <li><a href="#RunningOnDevice">Running on a Device</a></li> | |
| <li><a href="#Signing">Application Signing</a></li> | |
| <li><a href="#PluginReference">Plugin Language Reference</a></li> | |
| </ol> | |
| <h2>See also</h2> | |
| <ol> | |
| <li><a href="{@docRoot}sdk/installing/studio-build.html"> | |
| Build System</a></li> | |
| <li><a href="{@docRoot}tools/devices/managing-avds-cmdline.html"> | |
| Managing AVDs from the Command Line</a></li> | |
| <li><a href="{@docRoot}tools/devices/emulator.html"> | |
| Using the Android Emulator</a></li> | |
| <li><a href="{@docRoot}tools/publishing/app-signing.html"> | |
| Signing Your Applications</a></li> | |
| </ol> | |
| </div> | |
| </div> | |
| <p>By default, there are two build types to build your application using the gradle.build settings: | |
| one for debugging your application — <em>debug</em> — and one for building your | |
| final package for release — <em>release mode</em>. Regardless of which way you build type | |
| your modules use, the app must be signed before it can install on an emulator or device—with | |
| a debug key when building in debug mode and with your own private key when building in release mode.</p> | |
| <p>Whether you're building with the debug or release build type, you need to run | |
| and build your module. This will create the .apk file that you can install on an emulator or device. | |
| When you build using the debug build type, the .apk file is automatically signed by the SDK tools | |
| with a debug key based on the <code>debuggable true</code> setting in the module's gradle.build file, | |
| so it's instantly ready for installation onto an emulator or attached | |
| development device. You cannot distribute an application that is signed with a debug key. | |
| When you build using the release build type, the .apk file is <em>unsigned</em>, so you | |
| must manually sign it with your own private key, using Keytool and Jarsigner settings in the | |
| module's gradle.build file.</p> | |
| <p>It's important that you read and understand <a href= | |
| "{@docRoot}tools/publishing/app-signing.html">Signing Your Applications</a>, particularly once | |
| you're ready to release your application and share it with end-users. That document describes the | |
| procedure for generating a private key and then using it to sign your .apk file. If you're just | |
| getting started, however, you can quickly run your applications on an emulator or your own | |
| development device by building in debug mode.</p> | |
| <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 | |
| home page</a>. Install it and make sure it is in your executable PATH. Before calling Ant, you | |
| need to declare the JAVA_HOME environment variable to specify the path to where the JDK is | |
| installed.</p> | |
| <p class="note"><strong>Note:</strong> When installing JDK on Windows, the default is to install | |
| in the "Program Files" directory. This location will cause <code>ant</code> to fail, because of | |
| the space. To fix the problem, you can specify the JAVA_HOME variable like this: | |
| <pre>set JAVA_HOME=c:\Progra~1\Java\<jdkdir></pre> | |
| <p>The easiest solution, however, is to install JDK in a non-space directory, for example:</p> | |
| <pre>c:\java\jdk1.7</pre> | |
| <h2 id="DebugMode">Building in Debug Mode</h2> | |
| <p>For immediate application testing and debugging, you can build your application in debug mode | |
| and immediately install it on an emulator. In debug mode, the build tools automatically sign your | |
| application with a debug key and optimize the package with {@code zipalign}.</p> | |
| <p>To build in debug mode, open a command-line and navigate to the root of your project directory. | |
| Use Gradle to build your project in debug mode, invoke the <code>assembleDebug</code> build task | |
| using the Gradle wrapper script (<code>gradlew assembleRelease</code>). | |
| <p>This creates your debug <code>.apk</code> file inside the module <code>build/</code> | |
| directory, named <code><your_module_name>-debug.apk</code>. The file is already signed | |
| with the debug key and has been aligned with | |
| <a href="{@docRoot}tools/help/zipalign.html"><code>zipalign</code></a>. </p> | |
| <p>On Windows platforms, type this command:</p> | |
| <pre> | |
| > gradlew.bat assembleDebug | |
| </pre> | |
| <p>On Mac OS and Linux platforms, type these commands:</p> | |
| <pre> | |
| $ chmod +x gradlew | |
| $ ./gradlew assembleDebug | |
| </pre> | |
| <p>The first command (<code>chmod</code>) adds the execution permission to the Gradle wrapper | |
| script and is only necessary the first time you build this project from the command line.</p> | |
| <p>After you build the project, the output APK for the app module is located in | |
| <code>app/build/outputs/apk/</code>, and the output AAR for any lib modules is located in | |
| <code>lib/build/outputs/libs/</code>.</p> | |
| <p>To see a list of all available build tasks for your project, type this command:</p> | |
| <pre> | |
| $ ./gradlew tasks | |
| </pre> | |
| <p>Each time you change a source file or resource, you must run Gradle again in order to package up | |
| the latest version of the application.</p> | |
| <p>To install and run your application on an emulator, see the section about <a href= | |
| "{@docRoot}tools/building/building-studio.html">Running on the Emulator</a>.</p> | |
| <h2 id="ReleaseMode">Building in Release Mode</h2> | |
| <p>When you're ready to release and distribute your application to end-users, you must build your | |
| application in release mode. Once you have built in release mode, it's a good idea to perform | |
| additional testing and debugging with the final .apk.</p> | |
| <p>Before you start building your application in release mode, be aware that you must sign the | |
| resulting application package with your private key, and should then align it using the {@code | |
| zipalign} tool. There are two approaches to building in release mode: build an unsigned package | |
| in release mode and then manually sign and align the package, or allow the build script to sign | |
| and align the package for you.</p> | |
| <h3 id="ManualReleaseMode">Build unsigned</h3> | |
| <p>If you build your application <em>unsigned</em>, then you will need to manually sign and align | |
| the package.</p> | |
| <p>To build an <em>unsigned</em> .apk in release mode, open a command-line and navigate to the | |
| root of your module directory. Invoke the <code>assembleRelease</code> build task.</li> | |
| <p>On Windows platforms, type this command:</p> | |
| <pre> | |
| > gradlew.bat assembleRelease | |
| </pre> | |
| <p>On Mac OS and Linux platforms, type this command:</p> | |
| <pre> | |
| $ ./gradlew assembleRelease | |
| </pre> | |
| <p>This creates your Android application .apk file inside the project <code>bin/</code> | |
| directory, named <code><em><your_module_name></em>-unsigned.apk</code>.</p> | |
| <p class="note"><strong>Note:</strong> The .apk file is <em>unsigned</em> at this point and can't | |
| be installed until signed with your private key.</p> | |
| <p>Once you have created the unsigned .apk, your next step is to sign the .apk with your private | |
| key and then align it with {@code zipalign}. To complete this procedure, read <a href= | |
| "{@docRoot}tools/publishing/app-signing.html">Signing Your Applications</a>.</p> | |
| <p>When your <code>.apk</code> has been signed and aligned, it's ready to be distributed to end-users. | |
| You should test the final build on different devices or AVDs to ensure that it | |
| runs properly on different platforms.</p> | |
| <h3 id="AutoReleaseMode">Build signed and aligned</h3> | |
| <p>If you would like, you can configure the Android build script to automatically sign and align | |
| your application package. To do so, you must provide the path to your keystore and the name of | |
| your key alias in your modules's build.gradle file. With this information provided, | |
| the build will prompt you for your keystore and alias password when you build using the release | |
| build type and produce your final application package, which will be ready for distribution.</p> | |
| <p>To specify your keystore and alias, open the module gradle.build file (found in | |
| the root of the module directory) and add entries for {@code storeFile}, {@code storePassword}, | |
| {@code keyAlias} and {@code keyPassword}. | |
| For example:</p> | |
| <pre> | |
| storeFile file("myreleasekey.keystore") | |
| keyAlias "MyReleaseKey" | |
| </pre> | |
| <p>Save your changes. Now you can build a <em>signed</em> .apk in release mode:</p> | |
| <ol> | |
| <li>Open a command-line and navigate to the root of your module directory.</li> | |
| <li>Edit the gradle.build file to build your project in release mode: | |
| <p><pre> | |
| ... | |
| android { | |
| ... | |
| defaultConfig { ... } | |
| signingConfigs { | |
| release { | |
| storeFile file("myreleasekey.keystore") | |
| storePassword "password" | |
| keyAlias "MyReleaseKey" | |
| keyPassword "password" | |
| } | |
| } | |
| buildTypes { | |
| release { | |
| ... | |
| signingConfig signingConfigs.release | |
| } | |
| } | |
| } | |
| ... | |
| </pre></p> | |
| </li> | |
| <li>When prompted, enter you keystore and alias passwords. | |
| <p class="caution"><strong>Caution:</strong> As described above, your password will be | |
| visible on the screen.</p> | |
| </li> | |
| </ol> | |
| <p>This creates your Android application .apk file inside the module <code>build/</code> | |
| directory, named <code><em><your_module_name></em>-release.apk</code>. This .apk file has | |
| been signed with the private key specified in gradle.build file and aligned with {@code | |
| zipalign}. It's ready for installation and distribution.</p> | |
| <h3 id="OnceBuilt">Once built and signed in release mode</h3> | |
| <p>Once you have signed your application with a private key, you can install and run it on an | |
| <a href="#RunningOnEmulator">emulator</a> or <a href="#RunningOnDevice">device</a>. You can | |
| also try installing it onto a device from a web server. Simply upload the signed .apk to a web | |
| site, then load the .apk URL in your Android web browser to download the application and begin | |
| installation. (On your device, be sure you have enabled | |
| <em>Settings > Applications > Unknown sources</em>.)</p> | |
| <h2 id="RunningOnEmulator">Running on the Emulator</h2> | |
| <p>Before you can run your application on the Android Emulator, you must <a href= | |
| "{@docRoot}tools/devices/managing-avds.html">create an AVD</a>.</p> | |
| <p>To run your application:</p> | |
| <ol> | |
| <li> | |
| <strong>Open the AVD Manager and launch a virtual device</strong> | |
| <p>From your SDK's <code>platform-tools/</code> directory, execute the {@code android} tool | |
| with the <code>avd</code> options:</p> | |
| <pre> | |
| android avd | |
| </pre> | |
| <p>In the <em>Virtual Devices</em> view, select an AVD and click <strong>Start</strong>.</p> | |
| </li> | |
| <li> | |
| <strong>Install your application</strong> | |
| <p>From your SDK's <code>tools/</code> directory, install the {@code .apk} on the | |
| emulator:</p> | |
| <pre> | |
| adb install <em><path_to_your_bin></em>.apk | |
| </pre> | |
| <p>Your .apk file (signed with either a release or debug key) is in your module {@code build/} | |
| directory after you build your application.</p> | |
| <p>If there is more than one emulator running, you must specify the emulator upon which to | |
| install the application, by its serial number, with the <code>-s</code> option. For | |
| example:</p> | |
| <pre> | |
| adb -s emulator-5554 install <em>path/to/your/app</em>.apk | |
| </pre> | |
| <p>To see a list of available device serial numbers, execute {@code adb devices}.</p> | |
| </li> | |
| </ol> | |
| <p>If you don't see your application on the emulator, try closing the emulator and launching the | |
| virtual device again from the AVD Manager. Sometimes when you install an application for the | |
| first time, it won't show up in the application launcher or be accessible by other applications. | |
| This is because the package manager usually examines manifests completely only on emulator | |
| startup.</p> | |
| <p>Be certain to create multiple AVDs upon which to test your application. You should have one | |
| AVD for each platform and screen type with which your application is compatible. For instance, if | |
| your application compiles against the Android 4.0 (API Level 14) platform, you should create an | |
| AVD for each platform equal to and greater than 4.0 and an AVD for each <a href= | |
| "{@docRoot}guide/practices/screens_support.html">screen type</a> you support, then test your | |
| application on each one.</p> | |
| <p class="note"><strong>Tip:</strong> If you have <em>only one</em> emulator running, you can | |
| build your application and install it on the emulator in one simple step. Navigate to the root of | |
| your project directory and use Ant to compile the project with <em>install mode</em>: <code>ant | |
| install</code>. This will build your application, sign it with the debug key, and install it on | |
| the currently running emulator.</p> | |
| <h2 id="RunningOnDevice">Running on a Device</h2> | |
| <p>Before you can run your application on a device, you must perform some basic setup for your | |
| device:</p> | |
| <ul> | |
| <li>Enable <strong>USB debugging</strong> on your device. | |
| <ul> | |
| <li>On most devices running Android 3.2 or older, you can find the option under | |
| <strong>Settings > Applications > Development</strong>.</li> | |
| <li>On Android 4.0 and newer, it's in <strong>Settings > Developer options</strong>. | |
| <p class="note"><strong>Note:</strong> On Android 4.2 and newer, <strong>Developer | |
| options</strong> is hidden by default. To make it available, go | |
| to <strong>Settings > About phone</strong> and tap <strong>Build number</strong> | |
| seven times. Return to the previous screen to find <strong>Developer options</strong>.</p> | |
| </li> | |
| </ul> | |
| </li> | |
| <li>Ensure that your development computer can detect your device when connected via USB</li> | |
| </ul> | |
| <p>Read <a href="{@docRoot}tools/device.html#setting-up">Setting up a Device for | |
| Development</a> for more information.</p> | |
| <p>Once your device is set up and connected via USB, navigate to your SDK's <code>platform-tools/</code> | |
| directory and install the <code>.apk</code> on the device:</p> | |
| <pre> | |
| adb -d install <em>path/to/your/app</em>.apk | |
| </pre> | |
| <p>The {@code -d} flag specifies that you want to use the attached device (in case you also have | |
| an emulator running).</p> | |
| <p>For more information on the tools used above, please see the following documents:</p> | |
| <ul> | |
| <li><a href="{@docRoot}tools/help/android.html">android Tool</a></li> | |
| <li><a href="{@docRoot}tools/devices/emulator.html">Android Emulator</a></li> | |
| <li><a href="{@docRoot}tools/help/adb.html">Android Debug Bridge</a> (ADB)</li> | |
| </ul> | |
| <h2 id="Signing">Application Signing</h2> | |
| <p>As you begin developing Android applications, understand that all Android applications must be | |
| digitally signed before the system will install them on an emulator or device. There are two ways | |
| to do this: with a <em>debug key</em> (for immediate testing on an emulator or development | |
| device) or with a <em>private key</em> (for application distribution).</p> | |
| <p>The Android build tools help you get started by automatically signing your .apk files with a | |
| debug key at build time. This means that you can build your application and install it on the | |
| emulator without having to generate your own private key. However, please note that if you intend | |
| to publish your application, you <strong>must</strong> sign the application with your own private | |
| key, rather than the debug key generated by the SDK tools.</p> | |
| <p>Android Studio helps you get started quickly by signing your .apk files with a debug key, | |
| prior to installing them on an emulator or development device. This means that you can quickly | |
| run your application from Android Studio without having to generate your own private key. No | |
| specific action on your part is needed, provided ADT has access to Keytool. However, please note | |
| that if you intend to publish your application, you <strong>must</strong> sign the application | |
| with your own private key, rather than the debug key generated by the SDK tools.</p> | |
| <p>Please read <a href="{@docRoot}tools/publishing/app-signing.html">Signing Your | |
| Applications</a>, which provides a thorough guide to application signing on Android and what it | |
| means to you as an Android application developer. The document also includes a guide to publishing | |
| and signing your application.</p> | |
| <h2 id="PluginReference">Android Plugin for Gradle</h2> | |
| <p>The Android build system uses the Android plugin for Gradle to support the Gradle Domain | |
| Specific Language (DSL) and declarative language elements. See the | |
| <a href="{@docRoot}tools/building/plugin-for-gradle.html">Android Plug-in for Gradle</a> section for | |
| a description of the plugin and a link to the complete list of the supported Gradle DSL elements.</p> | |