| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 1 | page.title=Developing In Other IDEs |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | @jd:body |
| 3 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 4 | <div id="qv-wrapper"> |
| 5 | <div id="qv"> |
| 6 | <h2>In this document</h2> |
| 7 | <ol> |
| 8 | <li><a href="#CreatingAProject">Creating an Android Project</a></li> |
| 9 | <li><a href="#Signing">Preparing to Sign Your Application</a></li> |
| 10 | <li><a href="#Building">Building Your Application</a> |
| 11 | <ol> |
| 12 | <li><a href="#DebugMode">Building in debug mode</a></li> |
| 13 | <li><a href="#ReleaseMode">Building in release mode</a></li> |
| 14 | </ol> |
| 15 | </li> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 16 | <li><a href="#AVD">Creating an AVD</a></li> |
| 17 | <li><a href="#Running">Running Your Application</a> |
| 18 | <ol> |
| 19 | <li><a href="#RunningOnEmulator">Running on the emulator</a></li> |
| 20 | <li><a href="#RunningOnDevice">Running on a device</a></li> |
| 21 | </ol> |
| 22 | </li> |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 23 | <li><a href="#libraryProject">Working with Library Projects</a> |
| 24 | <ol> |
| 25 | <li><a href="#libraryReqts">Development requirements</a></li> |
| 26 | <li><a href="#librarySetup">Setting up a library project</a></li> |
| 27 | <li><a href="#libraryReference">Referencing a library project</a></li> |
| 28 | <li><a href="#depAppBuild">Building a dependent application project</a></li> |
| 29 | <li><a href="#considerations">Development considerations</a></li> |
| 30 | </ol> |
| 31 | </li> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 32 | <li><a href="#AttachingADebugger">Attaching a Debugger to Your Application</a></li> |
| 33 | </ol> |
| 34 | |
| 35 | <h2>See also</h2> |
| 36 | <ol> |
| Scott Main | 8a4c53a | 2009-04-24 13:41:44 -0700 | [diff] [blame] | 37 | <li><a href="{@docRoot}guide/developing/tools/othertools.html#android">android Tool</a></li> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 38 | <li><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></li> |
| 39 | <li><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a></li> |
| 40 | </ol> |
| 41 | </div> |
| 42 | </div> |
| 43 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | <p>The recommended way to develop an Android application is to use |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 45 | <a href="{@docRoot}guide/developing/eclipse-adt.html">Eclipse with the ADT plugin</a>. |
| 46 | The ADT plugin provides editing, building, debugging, and .apk packaging and signing functionality |
| 47 | integrated right into the IDE.</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 49 | <p>However, if you'd rather develop your application in another IDE, such as IntelliJ, |
| 50 | or in a basic editor, such as Emacs, you can do that instead. The SDK |
| 51 | includes all the tools you need to set up an Android project, build it, debug it and then |
| 52 | package it for distribution. This document is your guide to using these tools.</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 55 | <h2 id="EssentialTools">Essential Tools</h2> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 57 | <p>When developing in IDEs or editors other than Eclipse, you'll require |
| 58 | familiarity with the following Android SDK tools:</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 60 | <dl> |
| Scott Main | 8a4c53a | 2009-04-24 13:41:44 -0700 | [diff] [blame] | 61 | <dt><a href="{@docRoot}guide/developing/tools/othertools.html#android">android</a></dt> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 62 | <dd>To create/update Android projects and to create/move/delete AVDs.</dd> |
| 63 | <dt><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></dt> |
| 64 | <dd>To run your Android applications on an emulated Android platform.</dd> |
| 65 | <dt><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a></dt> |
| 66 | <dd>To interface with your emulator or connected device (install apps, |
| 67 | shell the device, issue commands, etc.). |
| 68 | </dd> |
| 69 | </dl> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 71 | <p>In addition to the above tools, included with the SDK, you'll use the following |
| 72 | open source and third-party tools:</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 74 | <dl> |
| 75 | <dt>Ant</dt> |
| 76 | <dd>To compile and build your Android project into an installable .apk file.</dd> |
| 77 | <dt>Keytool</dt> |
| 78 | <dd>To generate a keystore and private key, used to sign your .apk file.</dd> |
| 79 | <dt>Jarsigner (or similar signing tool)</dt> |
| 80 | <dd>To sign your .apk file with a private key generated by keytool.</dd> |
| 81 | </dl> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 83 | <p>In the topics that follow, you'll be introduced to each of these tools as necessary. |
| 84 | For more advanced operations, please read the respective documentation for each tool.</p> |
| 85 | |
| 86 | |
| 87 | <h2 id="CreatingAProject">Creating an Android Project</h2> |
| 88 | |
| 89 | <p>To create an Android project, you must use the <code>android</code> tool. When you create |
| 90 | a new project with <code>android</code>, it will generate a project directory |
| 91 | with some default application files, stub files, configuration files and a build file.</p> |
| 92 | |
| 93 | |
| 94 | <h3 id="CreatingANewProject">Creating a new Project</h3> |
| 95 | |
| 96 | <p>If you're starting a new project, use the <code>android create project</code> |
| 97 | command to generate all the necessary files and folders.</p> |
| 98 | |
| 99 | <p>To create a new Android project, open a command-line, |
| 100 | navigate to the <code>tools/</code> directory of your SDK and run:</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | <pre> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 102 | android create project \ |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 103 | --target <em><target_ID></em> \ |
| 104 | --name <em><your_project_name></em> \ |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 105 | --path <em>path/to/your/project</em> \ |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 106 | --activity <em><your_activity_name></em> \ |
| 107 | --package <em><your_package_namespace></em> |
| 108 | </pre> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | |
| 110 | <ul> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 111 | <li><code>target</code> is the "build target" for your application. It corresponds |
| 112 | to an Android platform library (including any add-ons, such as Google APIs) that you would like to |
| 113 | build your project against. To see a list of available targets and their corresponding IDs, |
| 114 | execute: <code>android list targets</code>.</li> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 115 | <li><code>name</code> is the name for your project. This is optional. If provided, this name will |
| 116 | be used |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 117 | for your .apk filename when you build your application.</li> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 118 | <li><code>path</code> is the location of your project directory. If the directory does not exist, |
| 119 | it will be created for you.</li> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 120 | <li><code>activity</code> is the name for your default {@link android.app.Activity} class. This |
| 121 | class file |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 122 | will be created for you inside |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 123 | |
| 124 | <code><em><path_to_your_project></em>/src/<em><your_package_namespace_path></em>/</code> |
| 125 | . |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 126 | This will also be used for your .apk filename unless you provide a the <code>name</code>.</li> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 127 | <li><code>package</code> is the package namespace for your project, following the same rules as |
| 128 | for |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 129 | packages in the Java programming language.</li> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | </ul> |
| 131 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 132 | <p>Here's an example:</p> |
| 133 | <pre> |
| 134 | android create project \ |
| 135 | --target 1 \ |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 136 | --name MyAndroidApp \ |
| 137 | --path ./MyAndroidAppProject \ |
| 138 | --activity MyAndroidAppActivity \ |
| 139 | --package com.example.myandroid |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 140 | </pre> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 142 | <p>The tool generates the following files and directories:</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 144 | <ul> |
| 145 | <li><code>AndroidManifest.xml</code> - The application manifest file, |
| 146 | synced to the specified Activity class for the project.</li> |
| 147 | <li><code>build.xml</code> - Build file for Ant.</li> |
| 148 | <li><code>default.properties</code> - Properties for the build system. <em>Do not modify |
| 149 | this file</em>.</li> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 150 | <li><code>build.properties</code> - Customizable properties for the build system. You can edit |
| 151 | this |
| 152 | file to override default build settings used by Ant and provide a pointer to your keystore and key |
| 153 | alias |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 154 | so that the build tools can sign your application when built in release mode.</li> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 155 | <li><code>src<em>/your/package/namespace/ActivityName</em>.java</code> - The Activity class |
| 156 | you specified during project creation.</li> |
| 157 | <li><code>bin/</code> - Output directory for the build script.</li> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 158 | <li><code>gen/</code> - Holds <code>Ant</code>-generated files, such as <code>R.java</code>. |
| 159 | </li> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 160 | <li><code>libs/</code> - Holds private libraries.</li> |
| 161 | <li><code>res/</code> - Holds project resources.</li> |
| 162 | <li><code>src/</code> - Holds source code.</li> |
| 163 | <li><code>tests/</code> - Holds a duplicate of all-of-the-above, for testing purposes.</li> |
| 164 | </ul> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 166 | <p>Once you've created your project, you're ready to begin development. |
| 167 | You can move your project folder wherever you want for development, but keep in mind |
| 168 | that you must use the <a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a> |
| Scott Main | 3165bbb | 2010-12-16 10:26:23 -0800 | [diff] [blame] | 169 | (adb) — located in the SDK <code>platform-tools/</code> directory — to send your |
| 170 | application |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 171 | to the emulator (discussed later). So you need access between your project solution and |
| Scott Main | 3165bbb | 2010-12-16 10:26:23 -0800 | [diff] [blame] | 172 | the <code>platform-tools/</code> folder.</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 174 | <p class="caution"><strong>Caution:</strong> You should refrain from moving the |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 175 | location of the SDK directory, because this will break the build scripts. (They |
| 176 | will need to be manually updated to reflect the new SDK location before they will |
| 177 | work again.)</p> |
| 178 | |
| 179 | |
| 180 | <h3 id="UpdatingAProject">Updating a project</h3> |
| 181 | |
| 182 | <p>If you're upgrading a project from an older version of the Android SDK or want to create |
| 183 | a new project from existing code, use the |
| 184 | <code>android update project</code> command to update the project to the new development |
| 185 | environment. You can also use this command to revise the build target of an existing project |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 186 | (with the <code>--target</code> option) and the project name (with the <code>--name</code> |
| 187 | option). The <code>android</code> tool will generate any files and |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 188 | folders (listed in the previous section) that are either missing or need to be updated, |
| 189 | as needed for the Android project.</p> |
| 190 | |
| 191 | <p>To update an existing Android project, open a command-line |
| 192 | and navigate to the <code>tools/</code> directory of your SDK. Now run:</p> |
| 193 | <pre> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 194 | android update project --name <em><project_name></em> --target <em><target_ID></em> |
| 195 | --path <em><path_to_your_project></em> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 196 | </pre> |
| 197 | |
| 198 | <ul> |
| 199 | <li><code>target</code> is the "build target" for your application. It corresponds to |
| 200 | an Android platform library (including any add-ons, such as Google APIs) that you would |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 201 | like to build your project against. To see a list of available targets and their corresponding |
| 202 | IDs, |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 203 | execute: <code>android list targets</code>.</li> |
| 204 | <li><code>path</code> is the location of your project directory.</li> |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 205 | <li><code>name</code> is the name for the project. This is optional—if you're not |
| 206 | changing the project name, you don't need this.</li> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 207 | </ul> |
| 208 | |
| 209 | <p>Here's an example:</p> |
| 210 | <pre> |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 211 | android update project --name MyApp --target 2 --path ./MyAppProject |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 212 | </pre> |
| 213 | |
| 214 | |
| 215 | <h2 id="Signing">Preparing to Sign Your Application</h2> |
| 216 | |
| 217 | <p>As you begin developing Android applications, understand that all |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | Android applications must be digitally signed before the system will install |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 219 | them on an emulator or device. There are two ways to do this: |
| 220 | with a <em>debug key</em> (for immediate testing on an emulator or development device) |
| 221 | or with a <em>private key</em> (for application distribution).</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 223 | <p>The Android build tools help you get started by automatically signing your .apk |
| 224 | files with a debug key at build time. This means |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | that you can compile your application and install it on the emulator without |
| 226 | having to generate your own private key. However, please note that if you intend |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 227 | to publish your application, you <strong>must</strong> sign the application with your |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | own private key, rather than the debug key generated by the SDK tools. </p> |
| 229 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 230 | <p>Please read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your |
| 231 | Applications</a>, which provides a thorough guide to application signing on Android |
| 232 | and what it means to you as an Android application developer.</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
| 234 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 235 | |
| 236 | <h2 id="Building">Building Your Application</h2> |
| 237 | |
| 238 | <p>There are two ways to build your application: one for testing/debugging your application |
| 239 | — <em>debug mode</em> — and one for building your final package for release — |
| 240 | <em>release mode</em>. As described in the previous |
| 241 | section, your application must be signed before it can be installed on an emulator |
| 242 | or device.</p> |
| 243 | |
| 244 | <p>Whether you're building in debug mode or release mode, you |
| 245 | need to use the Ant tool to compile and build your project. This will create the .apk file |
| 246 | that is installed onto the emulator or device. When you build in debug mode, the .apk |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 247 | file is automatically signed by the SDK tools with a debug key, so it's instantly ready for |
| 248 | installation |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 249 | (but only onto an emulator or attached development device). |
| 250 | When you build in release mode, the .apk file is <em>unsigned</em>, so you must manually |
| 251 | sign it with your own private key, using Keytool and Jarsigner.</p> |
| 252 | |
| 253 | <p>It's important that you read and understand |
| 254 | <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>, particularly |
| 255 | once you're ready to release your application and share it with end-users. That document describes |
| 256 | the procedure for generating a private key and then using it to sign your .apk file. |
| 257 | If you're just getting started, however, |
| 258 | you can quickly run your applications on an emulator or your own development device by building in |
| 259 | debug mode.</p> |
| 260 | |
| 261 | <p>If you don't have Ant, you can obtain it from the |
| 262 | <a href="http://ant.apache.org/">Apache Ant home page</a>. Install it and make |
| 263 | sure it is in your executable PATH. Before calling Ant, you need to declare the JAVA_HOME |
| 264 | environment variable to specify the path to where the JDK is installed.</p> |
| 265 | |
| 266 | <p class="note"><strong>Note:</strong> When installing JDK on Windows, the default is to install |
| 267 | in the "Program Files" directory. This location will cause <code>ant</code> to fail, because of |
| 268 | the space. To fix the problem, you can specify the JAVA_HOME variable like this: |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 269 | <code>set JAVA_HOME=c:\Progra~1\Java\<jdkdir></code>. The easiest solution, however, is to |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 270 | install JDK in a non-space directory, for example: <code>c:\java\jdk1.6.0_02</code>.</p> |
| 271 | |
| 272 | |
| 273 | <h3 id="DebugMode">Building in debug mode</h3> |
| 274 | |
| 275 | <p>For immediate application testing and debugging, you can build your application |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 276 | in debug mode and immediately install it on an emulator. In debug mode, the build tools |
| 277 | automatically sign your application with a debug key and optimize the package with |
| 278 | {@code zipalign}. However, you can (and should) also test your |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 279 | application in release mode. Debug mode simply allows you to run your application without |
| 280 | manually signing the application.</p> |
| 281 | |
| 282 | <p>To build in debug mode:</p> |
| 283 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | <ol> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 285 | <li>Open a command-line and navigate to the root of your project directory.</li> |
| 286 | <li>Use Ant to compile your project in debug mode: |
| 287 | <pre>ant debug</pre> |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 288 | <p>This creates your debug .apk file inside the project <code>bin/</code> |
| 289 | directory, named <code><em><your_project_name></em>-debug.apk</code>. The file |
| 290 | is already signed with the debug key and has been aligned with {@code zipalign}.</p> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 291 | </li> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | </ol> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 294 | <p>Each time you change a source file or resource, you must run Ant |
| 295 | again in order to package up the latest version of the application.</p> |
| 296 | |
| 297 | <p>To install and run your application on an emulator, see the following section |
| 298 | about <a href="#Running">Running Your Application</a>.</p> |
| 299 | |
| 300 | |
| 301 | <h3 id="ReleaseMode">Building in release mode</h3> |
| 302 | |
| 303 | <p>When you're ready to release and distribute your application to end-users, you must build |
| 304 | your application in release mode. Once you have built in release mode, it's a good idea to perform |
| 305 | additional testing and debugging with the final .apk.</p> |
| 306 | |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 307 | <p>Before you start building your application in release mode, be aware that you must sign |
| 308 | the resulting application package with your private key, and should then align it using the |
| 309 | {@code zipalign} tool. There are two approaches to building in release mode: |
| 310 | build an unsigned package in release mode and then manually sign and align |
| 311 | the package, or allow the build script |
| 312 | to sign and align the package for you.</p> |
| 313 | |
| 314 | <h4 id="ManualReleaseMode">Build unsigned</h4> |
| 315 | |
| 316 | <p>If you build your application <em>unsigned</em>, then you will need to |
| 317 | manually sign and align the package.</p> |
| 318 | |
| 319 | <p>To build an <em>unsigned</em> .apk in release mode:</p> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 320 | |
| 321 | <ol> |
| 322 | <li>Open a command-line and navigate to the root of your project directory.</li> |
| 323 | <li>Use Ant to compile your project in release mode: |
| 324 | <pre>ant release</pre> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 325 | </li> |
| 326 | </ol> |
| 327 | |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 328 | <p>This creates your Android application .apk file inside the project <code>bin/</code> |
| 329 | directory, named <code><em><your_project_name></em>-unsigned.apk</code>.</p> |
| 330 | |
| 331 | <p class="note"><strong>Note:</strong> The .apk file is <em>unsigned</em> at this point |
| 332 | and can't be installed until signed with your private key.</p> |
| 333 | |
| 334 | <p>Once you have created the unsigned .apk, your next step is to sign the .apk |
| 335 | with your private key and then align it with {@code zipalign}. To complete this procedure, |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 336 | read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>.</p> |
| 337 | |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 338 | <p>When your .apk has been signed and aligned, it's ready to be distributed to end-users.</p> |
| 339 | |
| 340 | <h4 id="AutoReleaseMode">Build signed and aligned</h4> |
| 341 | |
| 342 | <p>If you would like, you can configure the Android build script to automatically |
| 343 | sign and align your application package. To do so, you must provide the path to your keystore |
| 344 | and the name of your key alias in your project's {@code build.properties} file. With this |
| 345 | information provided, the build script will prompt you for your keystore and alias password |
| 346 | when you build in release mode and produce your final application package, which will be ready |
| 347 | for distribution.</p> |
| 348 | |
| 349 | <p class="caution"><strong>Caution:</strong> Due to the way Ant handles input, the password that |
| 350 | you enter during the build process <strong>will be visible</strong>. If you are |
| 351 | concerned about your keystore and alias password being visible on screen, then you |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 352 | may prefer to perform the application signing manually, via Jarsigner (or a similar tool). To |
| 353 | instead |
| 354 | perform the signing procedure manually, <a href="#ManualReleaseMode">build unsigned</a> and then |
| 355 | continue |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 356 | with <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>.</p> |
| 357 | |
| 358 | <p>To specify your keystore and alias, open the project {@code build.properties} file (found in the |
| 359 | root of the project directory) and add entries for {@code key.store} and {@code key.alias}. |
| 360 | For example:</p> |
| 361 | |
| 362 | <pre> |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 363 | key.store=path/to/my.keystore |
| Scott Main | 23110e4 | 2009-09-02 10:25:09 -0700 | [diff] [blame] | 364 | key.alias=mykeystore |
| 365 | </pre> |
| 366 | |
| 367 | <p>Save your changes. Now you can build a <em>signed</em> .apk in release mode:</p> |
| 368 | |
| 369 | <ol> |
| 370 | <li>Open a command-line and navigate to the root of your project directory.</li> |
| 371 | <li>Use Ant to compile your project in release mode: |
| 372 | <pre>ant release</pre> |
| 373 | </li> |
| 374 | <li>When prompted, enter you keystore and alias passwords. |
| 375 | <p class="caution"><strong>Caution:</strong> As described above, |
| 376 | your password will be visible on the screen.</p> |
| 377 | </li> |
| 378 | </ol> |
| 379 | |
| 380 | <p>This creates your Android application .apk file inside the project <code>bin/</code> |
| 381 | directory, named <code><em><your_project_name></em>-release.apk</code>. |
| 382 | This .apk file has been signed with the private key specified in |
| 383 | {@code build.properties} and aligned with {@code zipalign}. It's ready for |
| 384 | installation and distribution.</p> |
| 385 | |
| 386 | |
| 387 | <h4>Once built and signed in release mode</h4> |
| 388 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 389 | <p>Once you have signed your application with a private key, you can install it on an |
| 390 | emulator or device as discussed in the following section about |
| 391 | <a href="#Running">Running Your Application</a>. |
| 392 | You can also try installing it onto a device from a web server. |
| 393 | Simply upload the signed APK to a web site, then load the .apk URL in your Android web browser to |
| 394 | download the application and begin installation. |
| 395 | (On your device, be sure you have enabled <em>Settings > Applications > Unknown sources</em>.)</p> |
| 396 | |
| 397 | |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 398 | <h2 id="AVD">Creating an AVD</h2> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 399 | |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 400 | <p>An Android Virtual Device (AVD) is a device configuration for the emulator that |
| 401 | allows you to model real world devices. In order to run an instance of the emulator, you must create |
| 402 | an AVD.</p> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 403 | |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 404 | <p>To create an AVD using the SDK tools:</p> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 405 | |
| 406 | <ol> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 407 | <li>Navigate to your SDK's <code>tools/</code> directory and execute the {@code android} |
| 408 | tool with no arguments: |
| 409 | <pre>android</pre> |
| 410 | <p>This will launch the SDK and AVD Manager GUI.</p> |
| 411 | </li> |
| 412 | <li>In the <em>Virtual Devices</em> panel, you'll see a list of existing AVDs. Click |
| 413 | <strong>New</strong> |
| 414 | to create a new AVD.</li> |
| 415 | <li>Fill in the details for the AVD. |
| 416 | <p>Give it a name, a platform target, an SD card size, and |
| 417 | a skin (HVGA is default).</p> |
| 418 | <p class="note"><strong>Note:</strong> Be sure to define |
| 419 | a target for your AVD that satisfies your application's build target (the AVD |
| 420 | platform target must have an API Level equal to or greater than the API Level that your |
| 421 | application compiles against).</p> |
| 422 | </li> |
| 423 | <li>Click <strong>Create AVD</strong>.</li> |
| 424 | </ol> |
| 425 | |
| 426 | <p>Your AVD is now ready and you can either close the AVD Manager, create more AVDs, or |
| 427 | launch an emulator with the AVD by clicking <strong>Start</strong>.</p> |
| 428 | |
| 429 | <p>For more information about AVDs, read the |
| 430 | <a href="{@docRoot}guide/developing/tools/avd.html">Android Virtual Devices</a> |
| 431 | documentation.</p> |
| 432 | |
| 433 | |
| 434 | <h2 id="Running">Running Your Application</h2> |
| 435 | |
| 436 | <div class="sidebox-wrapper"> |
| 437 | <div class="sidebox"> |
| 438 | <h2>Use the Emulator to Test Different Configurations</h2> |
| 439 | <p>Create multiple AVDs that each define a different device configuration with which your |
| 440 | application is compatible, then launch each AVD into a new emulator from the SDK and AVD Manager. |
| 441 | Set the target mode in your app's run configuration to manual, so that when you run your |
| 442 | application, you can select from the available virtual devices.</p> |
| 443 | </div> |
| 444 | </div> |
| 445 | |
| 446 | <p>Running your application on a virtual or real device takes just a couple steps. Remember to |
| 447 | first <a href="#Building">build your application</a>.</p> |
| 448 | |
| 449 | <h3 id="RunningOnEmulator">Running on the emulator</h3> |
| 450 | |
| 451 | <p>Before you can run your application on the Android Emulator, |
| 452 | you must <a href="#AVD">create an AVD</a>.</p> |
| 453 | |
| 454 | <p>To run your application:</p> |
| 455 | <ol> |
| 456 | <li><strong>Open the SDK and AVD Manager and launch a virtual device</strong></li> |
| 457 | <p>From your SDK's <code>tools/</code> directory, execute the {@code android} tool with no |
| 458 | arguments: |
| 459 | <pre>android</pre> |
| 460 | <p>In the <em>Virtual Devices</em> view, select an AVD and click <strong>Start</strong>.</p> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 461 | </li> |
| 462 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 463 | <li><strong>Install your application</strong> |
| Scott Main | 3165bbb | 2010-12-16 10:26:23 -0800 | [diff] [blame] | 464 | <p>From your SDK's <code>platform-tools/</code> directory, install the {@code .apk} on the |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 465 | emulator: |
| 466 | <pre>adb install <em><path_to_your_bin></em>.apk</pre> |
| 467 | <p>Your APK file (signed with either a release or debug key) is in your project {@code bin/} |
| 468 | directory after you <a href="#Building">build your application</a>.</p> |
| 469 | <p>If there is more than one emulator running, you must specify the emulator upon which to |
| 470 | install the application, by its serial number, with the <code>-s</code> option. For example:</p> |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 471 | <pre>adb -s emulator-5554 install <em>path/to/your/app</em>.apk</pre> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 472 | <p>To see a list of available device serial numbers, execute {@code adb devices}.</p> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 473 | </li> |
| 474 | </ol> |
| 475 | |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 476 | <p>If you don't see your application on the emulator. Try closing the emulator and launching the |
| 477 | virtual device again from the SDK and AVD Manager. Sometimes when you install an Activity for the |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 478 | first time, it won't show up in the application launcher or be accessible by other |
| 479 | applications. This is because the package manager usually examines manifests |
| 480 | completely only on emulator startup.</p> |
| 481 | |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 482 | <p>Be certain to create multiple AVDs upon which to test your application. You should have one AVD |
| 483 | for each platform and screen type with which your application is compatible. For |
| 484 | instance, if your application compiles against the Android 1.5 (API Level 3) platform, you should |
| 485 | create an AVD for each platform equal to and greater than 1.5 and an AVD for each <a |
| 486 | href="{@docRoot}guide/practices/screens_support.html">screen type</a> you support, then test |
| 487 | your application on each one.</p> |
| 488 | |
| 489 | <p class="note"><strong>Tip:</strong> If you have <em>only one</em> emulator running, |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 490 | you can build your application and install it on the emulator in one simple step. |
| 491 | Navigate to the root of your project directory and use Ant to compile the project |
| 492 | with <em>install mode</em>: |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 493 | <code>ant install</code>. This will build your application, sign it with the debug key, |
| 494 | and install it on the currently running emulator.</p> |
| 495 | |
| 496 | |
| 497 | <h3 id="RunningOnDevice">Running on a device</h3> |
| 498 | |
| 499 | <p>Before you can run your application on a device, you must perform some basic setup for your |
| 500 | device:</p> |
| 501 | |
| 502 | <ul> |
| 503 | <li>Declare your application as debuggable in your manifest</li> |
| 504 | <li>Enable USB Debugging on your device</li> |
| 505 | <li>Ensure that your development computer can detect your device when connected via USB</li> |
| 506 | </ul> |
| 507 | <p>Read <a href="{@docRoot}guide/developing/device.html#setting-up">Setting up a Device for |
| 508 | Development</a> for more information.</p> |
| 509 | |
| 510 | <p>Once your device is set up and connected via USB, navigate to your |
| Scott Main | 3165bbb | 2010-12-16 10:26:23 -0800 | [diff] [blame] | 511 | SDK's <code>platform-tools/</code> directory and install the <code>.apk</code> on the device: |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 512 | <pre>adb -d install <em>path/to/your/app</em>.apk</pre> |
| Scott Main | 0197999 | 2010-04-05 17:42:17 -0700 | [diff] [blame] | 513 | <p>The {@code -d} flag specifies that you want to use the attached device (in case you also |
| 514 | have an emulator running).</p> |
| 515 | |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 516 | <p>For more information on the tools used above, please see the following documents:</p> |
| 517 | <ul> |
| Scott Main | 8a4c53a | 2009-04-24 13:41:44 -0700 | [diff] [blame] | 518 | <li><a href="{@docRoot}guide/developing/tools/othertools.html#android">android Tool</a></li> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 519 | <li><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></li> |
| 520 | <li><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a> (ADB)</li> |
| 521 | </ul> |
| 522 | |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 523 | <h2 id="libraryProject">Working with Library Projects</h2> |
| 524 | |
| 525 | <div class="sidebox-wrapper"> |
| 526 | <div class="sidebox"> |
| 527 | <h2>Library project example code</h2> |
| 528 | |
| 529 | <p>The SDK includes an example application called TicTacToeMain that shows how a |
| 530 | dependent application can use code and resources from an Android Library |
| 531 | project. The TicTacToeMain application uses code and resources from an example |
| 532 | library project called TicTacToeLib. |
| 533 | |
| 534 | <p style="margin-top:1em;">To download the sample applications and run them as |
| 535 | projects in your environment, use the <em>Android SDK and AVD Manager</em> to |
| 536 | download the "Samples for SDK API 8" component into your SDK. </p> |
| 537 | |
| 538 | <p style="margin-top:1em;">For more information and to browse the code of the |
| 539 | samples, see the <a |
| 540 | href="{@docRoot}resources/samples/TicTacToeMain/index.html">TicTacToeMain |
| 541 | application</a>.</p> |
| 542 | </div> |
| 543 | </div> |
| 544 | |
| 545 | <p>An Android <em>library project</em> is a development project that holds |
| 546 | shared Android source code and resources. Other Android application projects can |
| 547 | reference the library project and, at build time, include its compiled sources |
| 548 | in their <code>.apk</code> files. Multiple application projects can reference |
| 549 | the same library project and any single application project can reference |
| 550 | multiple library projects. </p> |
| 551 | |
| 552 | <p>If you have source code and resources that are common to multiple application |
| 553 | projects, you can move them to a library project so that it is easier to |
| 554 | maintain across applications and versions. Here are some common scenarios in |
| 555 | which you could make use of library projects: </p> |
| 556 | |
| 557 | <ul> |
| 558 | <li>If you are developing multiple related applications that use some of the |
| Dirk Dougherty | f1e134b | 2010-07-28 08:19:45 -0700 | [diff] [blame] | 559 | same components, you could move the redundant components out of their respective |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 560 | application projects and create a single, reuseable set of the same components |
| 561 | in a library project. </li> |
| 562 | <li>If you are creating an application that exists in both free and paid |
| Dirk Dougherty | f1e134b | 2010-07-28 08:19:45 -0700 | [diff] [blame] | 563 | versions, you could move the part of the application that is common to both versions |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 564 | into a library project. The two dependent projects, with their different package |
| 565 | names, will reference the library project and provide only the difference |
| 566 | between the two application versions.</li> |
| 567 | </ul> |
| 568 | |
| 569 | <p>Structurally, a library project is similar to a standard Android application |
| 570 | project. For example, it includes a manifest file at the project root, as well |
| 571 | as <code>src/</code>, <code>res/</code> and similar directories. The project can |
| 572 | contain the same types of source code and resources as a standard |
| 573 | Android project, stored in the same way. For example, source code in the library |
| 574 | project can access its own resources through its <code>R</code> class. </p> |
| 575 | |
| 576 | <p>However, a library project differs from an standard Android application |
| 577 | project in that you cannot compile it directly to its own <code>.apk</code> or |
| 578 | run it on the Android platform. Similarly, you cannot export the library project |
| 579 | to a self-contained JAR file, as you would do for a true library. Instead, you |
| 580 | must compile the library indirectly, by referencing the library from a dependent |
| 581 | application's build path, then building that application. </p> |
| 582 | |
| 583 | <p>When you build an application that depends on a library project, the SDK |
| 584 | tools compile the library and merge its sources with those in the main project, |
| 585 | then use the result to generate the <code>.apk</code>. In cases where a resource |
| 586 | ID is defined in both the application and the library, the tools ensure that the |
| 587 | resource declared in the application gets priority and that the resource in the |
| 588 | library project is not compiled into the application <code>.apk</code>. This |
| 589 | gives your application the flexibility to either use or redefine any resource |
| 590 | behaviors or values that are defined in any library.</p> |
| 591 | |
| 592 | <p>To organize your code further, your application can add references to |
| 593 | multiple library projects, then specify the relative priority of the resources |
| 594 | in each library. This lets you build up the resources actually used in your |
| 595 | application in a cumulative manner. When two libraries referenced from an |
| 596 | application define the same resource ID, the tools select the resource from the |
| 597 | library with higher priority and discard the other. |
| 598 | |
| 599 | <p>Once you've have added references, the tools let you set their relative |
| 600 | priority by editing the application project's build properties. At build time, |
| 601 | the tools merge the libraries with the application one at a time, starting from |
| 602 | the lowest priority to the highest. </p> |
| 603 | |
| 604 | <p>Note that a library project cannot itself reference another library project |
| 605 | and that, at build time, library projects are <em>not</em> merged with each |
| 606 | other before being merged with the application. However, note that a library can |
| 607 | import an external library (JAR) in the normal way.</p> |
| 608 | |
| 609 | <p>The sections below describe how to use ADT to set up and manage library your |
| 610 | projects. Once you've set up your library projects and moved code into them, you |
| 611 | can import library classes and resources to your application in the normal way. |
| 612 | </p> |
| 613 | |
| 614 | |
| 615 | <h3 id="libraryReqts">Development requirements</h3> |
| 616 | |
| 617 | <p>Android library projects are a build-time construct, so you can use them to |
| 618 | build a final application <code>.apk</code> that targets any API level and is |
| 619 | compiled against any version of the Android library. </p> |
| 620 | |
| 621 | <p>However, to use library projects, you need to update your development |
| 622 | environment to use the latest tools and platforms, since older releases of the |
| 623 | tools and platforms do not support building with library projects. Specifically, |
| 624 | you need to download and install the versions listed below:</p> |
| 625 | |
| 626 | <p class="table-caption"><strong>Table 1.</strong> Minimum versions of SDK tools |
| 627 | and plaforms on which you can develop library projects.</p> |
| 628 | |
| 629 | <table> |
| 630 | <tr> |
| 631 | <th>Component</th> |
| 632 | <th>Minimum Version</th> |
| 633 | </tr> |
| 634 | <tr> |
| 635 | <td>SDK Tools</td> |
| 636 | <td>r6 (or higher)</td> |
| 637 | </tr> |
| 638 | <tr><td>Android 2.2 platform</td><td>r1 (or higher)</td></tr> |
| 639 | <tr><td>Android 2.1 platform</td><td>r2 (or higher)</td></tr> |
| 640 | <tr><td style="color:gray">Android 2.0.1 platform</td><td style="color:gray"><em>not supported</em></td></tr> |
| 641 | <tr><td style="color:gray">Android 2.0 platform</td><td style="color:gray"><em>not supported</em></td></tr> |
| 642 | <tr><td>Android 1.6 platform</td><td>r3 (or higher)</td></tr> |
| 643 | <tr><td>Android 1.5 platform</td><td>r4 (or higher)</td></tr> |
| 644 | <tr><td>ADT Plugin</td><td>0.9.7 (or higher)</td></tr> |
| 645 | </table> |
| 646 | |
| 647 | <p>You can download the tools and platforms using the <em>Android SDK and AVD |
| 648 | Manager</em>, as described in <a href="{@docRoot}sdk/adding-components.html">Adding SDK |
| 649 | Components</a>.</p> |
| 650 | |
| 651 | |
| 652 | <h3 id="librarySetup">Setting up a new library project</h3> |
| 653 | |
| 654 | <p>A library project is a standard Android project, so you can create a new one in the |
| 655 | same way as you would a new application project. Specifically, you can use |
| 656 | the <code>android</code> tool to generate a new library project with all of the |
| 657 | necessary files and folders. </p> |
| 658 | |
| 659 | <h4>Creating a library project</h4> |
| 660 | |
| 661 | <p>To create a new library project, navigate to the <code><sdk>/tools/</code> directory |
| 662 | and use this command:</p> |
| 663 | |
| 664 | <pre class="no-pretty-print" style="color:black"> |
| 665 | android create lib-project --name <em><your_project_name></em> \ |
| 666 | --target <em><target_ID></em> \ |
| 667 | --path <em>path/to/your/project</em> \ |
| 668 | --package <em><your_library_package_namespace></em> |
| 669 | </pre> |
| 670 | |
| 671 | <p>The <code>create lib-project</code> command creates a standard project |
| 672 | structure that includes preset property that indicates to the build system that |
| 673 | the project is a library. It does this by adding this line to the project's |
| 674 | <code>default.properties</code> file: </p> |
| 675 | |
| 676 | <pre class="no-pretty-print" style="color:black">android.library=true</pre> |
| 677 | |
| 678 | <p>Once the command completes, the library project is created and you can begin moving |
| 679 | source code and resources into it, as described in the sections below.</p> |
| 680 | |
| 681 | <p>If you want to convert an existing application project to a library project, |
| 682 | so that other applications can use it, you can do so by adding a the |
| 683 | <code>android.library=true</code> property to the application's |
| 684 | <code>default.properties</code> file. </p> |
| 685 | |
| 686 | <h4>Creating the manifest file</h4> |
| 687 | |
| 688 | <p>A library project's manifest file must declare all of the shared components |
| 689 | that it includes, just as would a standard Android application. For more |
| 690 | information, see the documentation for <a |
| Scott Main | 1c8b6ca | 2010-07-02 11:11:34 -0700 | [diff] [blame] | 691 | href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p> |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 692 | |
| 693 | <p>For example, the <a |
| 694 | href="{@docRoot}resources/samples/TicTacToeLib/AndroidManifest.html">TicTacToeLib</a> |
| 695 | example library project declares the Activity <code>GameActivity</code>: </p> |
| 696 | |
| 697 | <pre><manifest> |
| 698 | ... |
| 699 | <application> |
| 700 | ... |
| 701 | <activity android:name="GameActivity" /> |
| 702 | ... |
| 703 | </application> |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 704 | </manifest></pre> |
| 705 | |
| 706 | <h4>Updating a library project</h4> |
| 707 | |
| 708 | <p>If you want to update the build properties (build target, location) of the |
| 709 | library project, use this command: </p> |
| 710 | |
| 711 | <pre> |
| 712 | android update lib-project \ |
| 713 | --target <em><target_ID></em> \ |
| 714 | --path <em>path/to/your/project</em> |
| 715 | </pre> |
| 716 | |
| 717 | |
| 718 | <h3 id="libraryReference">Referencing a library project from an application</h3> |
| 719 | |
| 720 | <p>If you are developing an application and want to include the shared code or |
| 721 | resources from a library project, you can do so easily by adding a reference to |
| 722 | the library project in the application project's build properties.</p> |
| 723 | |
| 724 | <p>To add a reference to a library project, navigate to the <code><sdk>/tools/</code> directory |
| 725 | and use this command:</p> |
| 726 | |
| 727 | <pre> |
| 728 | android update lib-project \ |
| 729 | --target <em><target_ID></em> \ |
| 730 | --path <em>path/to/your/project</em> |
| 731 | --library <em>path/to/library_projectA</em> |
| 732 | </pre> |
| 733 | |
| 734 | <p>This command updates the application project's build properties to include a |
| 735 | reference to the library project. Specifically, it adds an |
| 736 | <code>android.library.reference.<em>n</em></code> property to the project's |
| 737 | <code>default.properties</code> file. For example: </p> |
| 738 | |
| 739 | <pre class="no-pretty-print" style="color:black"> |
| 740 | android.library.reference.1=path/to/library_projectA |
| 741 | </pre> |
| 742 | |
| 743 | <p>If you are adding references to multiple libraries, note that you can set |
| 744 | their relative priority (and merge order) by manually editing the |
| 745 | <code>default.properties</code> file and adjusting the each reference's |
| 746 | <code>.<em>n</em></code> index as appropriate. For example, assume these |
| 747 | references: </p> |
| 748 | |
| 749 | <pre class="no-pretty-print" style="color:black"> |
| 750 | android.library.reference.1=path/to/library_projectA |
| 751 | android.library.reference.2=path/to/library_projectB |
| 752 | android.library.reference.3=path/to/library_projectC |
| 753 | </pre> |
| 754 | |
| 755 | <p>You can reorder the references to give highest priority to |
| 756 | <code>library_projectC</code> in this way:</p> |
| 757 | |
| 758 | <pre class="no-pretty-print" style="color:black"> |
| 759 | android.library.reference.2=path/to/library_projectA |
| 760 | android.library.reference.3=path/to/library_projectB |
| 761 | android.library.reference.1=path/to/library_projectC |
| 762 | </pre> |
| 763 | |
| 764 | <p>Note that the <code>.<em>n</em></code> index in the references |
| 765 | must begin at "1" and increase uniformly without "holes". References |
| 766 | appearing in the index after a hole are ignored. </p> |
| 767 | |
| 768 | <p>At build time, the libraries are merged with the application one at a time, |
| 769 | starting from the lowest priority to the highest. Note that a library cannot |
| 770 | itself reference another library and that, at build time, libraries are not |
| 771 | merged with each other before being merged with the application.</p> |
| 772 | |
| 773 | |
| 774 | <h4>Declaring library components in the the manifest file</h4> |
| 775 | |
| 776 | <p>In the manifest file of the application project, you must add declarations |
| 777 | of all components that the application will use that are imported from a library |
| 778 | project. For example, you must declare any <code><activity></code>, |
| 779 | <code><service></code>, <code><receiver></code>, |
| 780 | <code><provider></code>, and so on, as well as |
| 781 | <code><permission></code>, <code><uses-library></code>, and similar |
| 782 | elements.</p> |
| 783 | |
| 784 | <p>Declarations should reference the library components by their fully-qualified |
| 785 | package names, where appropriate. </p> |
| 786 | |
| 787 | <p>For example, the |
| 788 | <a href="{@docRoot}resources/samples/TicTacToeMain/AndroidManifest.html">TicTacToeMain</a> |
| 789 | example application declares the library Activity <code>GameActivity</code> |
| 790 | like this: </p> |
| 791 | |
| 792 | <pre><manifest> |
| 793 | ... |
| 794 | <application> |
| 795 | ... |
| 796 | <activity android:name="com.example.android.tictactoe.library.GameActivity" /> |
| 797 | ... |
| 798 | </application> |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 799 | </manifest></pre> |
| 800 | |
| Scott Main | 1c8b6ca | 2010-07-02 11:11:34 -0700 | [diff] [blame] | 801 | <p>For more information about the manifest file, see the documentation for <a |
| 802 | href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p> |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 803 | |
| 804 | <h3 id="depAppBuild">Building a dependent application</h3> |
| 805 | |
| 806 | <p>To build an application project that depends on one or more library projects, |
| 807 | you can use the standard Ant build commands and compile modes, as described in |
| Scott Main | 369c1c1 | 2010-12-07 11:17:00 -0800 | [diff] [blame] | 808 | <a href="#Building">Building Your Application</a>, earlier in this document. The |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 809 | tools compile and merge all libraries referenced by the application as part |
| 810 | of compiling the dependent application project. No additional commands or steps |
| 811 | are necessary. </p> |
| 812 | |
| 813 | <h3 id="considerations">Development considerations</h3> |
| 814 | |
| 815 | <p>As you develop your library project and dependent applications, keep the |
| 816 | points listed below in mind.</p> |
| 817 | |
| 818 | <p><strong>Resource conflicts</strong></p> |
| 819 | |
| 820 | <p>Since the tools merge the resources of a library project with those of a |
| 821 | dependent application project, a given resource ID might be defined in both |
| 822 | projects. In this case, the tools select the resource from the application, or |
| 823 | the library with highest priority, and discard the other resource. As you |
| 824 | develop your applications, be aware that common resource IDs are likely to be |
| 825 | defined in more than one project and will be merged, with the resource from the |
| 826 | application or highest-priority library taking precedence.</p> |
| 827 | |
| 828 | <p><strong>Using prefixes to avoid resource conflicts</strong></p> |
| 829 | |
| 830 | <p>To avoid resource conflicts for common resource IDs, consider using a prefix |
| 831 | or other consistent naming scheme that is unique to the project (or is unique |
| 832 | across all projects). </p> |
| 833 | |
| 834 | <p><strong>No export of library project to JAR</strong></p> |
| 835 | |
| 836 | <p>A library cannot be distributed as a binary file (such as a jar file). This |
| 837 | is because the library project is compiled by the main project to use the |
| 838 | correct resource IDs.</p> |
| 839 | |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 840 | <p><strong>A library project can include a JAR library</strong></p> |
| 841 | |
| 842 | <p>You can develop a library project that itself includes a JAR library. When |
| 843 | you build the dependent application project, the tools automatically locate and |
| 844 | include the library in the application <code>.apk</code>. </p> |
| 845 | |
| 846 | <p><strong>A library project can depend on an external JAR library</strong></p> |
| 847 | |
| 848 | <p>You can develop a library project that depends on an external library (for |
| 849 | example, the Maps external library). In this case, the dependent application |
| 850 | must build against a target that includes the external library (for example, the |
| 851 | Google APIs Add-On). Note also that both the library project and the dependent |
| 852 | application must declare the external library their manifest files, in a <a |
| 853 | href="{@docRoot}guide/topics/manifest/uses-library-element.html"><code><uses-library></code></a> |
| 854 | element. </p> |
| 855 | |
| Dirk Dougherty | a324564 | 2010-05-11 19:09:53 -0700 | [diff] [blame] | 856 | <p><strong>Library project cannot include raw assets</strong></p> |
| 857 | |
| 858 | <p>The tools do not support the use of raw asset files in a library project. |
| 859 | Any asset resources used by an application must be stored in the |
| 860 | <code>assets/</code> directory of the application project |
| 861 | itself.</p> |
| 862 | |
| 863 | <p><strong>Targeting different Android platform versions in library project and |
| 864 | application project</strong></p> |
| 865 | |
| 866 | <p>A library is compiled as part of the dependent application project, so the |
| 867 | API used in the library project must be compatible with the version of the |
| 868 | Android library used to compile the application project. In general, the library |
| 869 | project should use an <a href="{@docRoot}guide/appendix/api-levels.html">API level</a> |
| 870 | that is the same as — or lower than — that used by the application. |
| 871 | If the library project uses an API level that is higher than that of the |
| 872 | application, the application project will fail to compile. It is perfectly |
| 873 | acceptable to have a library that uses the Android 1.5 API (API level 3) and |
| 874 | that is used in an Android 1.6 (API level 4) or Android 2.1 (API level 7) |
| 875 | project, for instance.</p> |
| 876 | |
| 877 | <p><strong>No restriction on library package name</strong></p> |
| 878 | |
| 879 | <p>There is no requirement for the package name of a library to be the same as |
| 880 | that of applications that use it.</p> |
| 881 | |
| 882 | <p><strong>Multiple R classes in gen/ folder of application project</strong></p> |
| 883 | |
| 884 | <p>When you build the dependent application project, the code of any libraries |
| 885 | is compiled and merged to the application project. Each library has its own |
| 886 | <code>R</code> class, named according to the library's package name. The |
| 887 | <code>R</code> class generated from the resources of the main project and of the |
| 888 | library is created in all the packages that are needed including the main |
| 889 | project’s package and the libraries’ packages.</p> |
| 890 | |
| 891 | <p><strong>Testing a library project</strong></p> |
| 892 | |
| 893 | <p>There are two recommended ways of setting up testing on code and resources in |
| 894 | a library project: </p> |
| 895 | |
| 896 | <ul> |
| 897 | <li>You can set up a <a |
| 898 | href="{@docRoot}guide/developing/testing/testing_otheride.html">test project</a> |
| 899 | that instruments an application project that depends on the library project. You |
| 900 | can then add tests to the project for library-specific features.</li> |
| 901 | <li>You can set up a set up a standard application project that depends on the |
| 902 | library and put the instrumentation in that project. This lets you create a |
| 903 | self-contained project that contains both the tests/instrumentations and the |
| 904 | code to test.</li> |
| 905 | </ul> |
| 906 | |
| 907 | <p><strong>Library project storage location</strong></p> |
| 908 | |
| 909 | <p>There are no specific requirements on where you should store a library |
| 910 | project, relative to a dependent application project, as long as the application |
| 911 | project can reference the library project by a relative link. You can place the |
| 912 | library project What is important is that the main project can reference the |
| 913 | library project through a relative link.</p> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 914 | |
| 915 | <h2 id="AttachingADebugger">Attaching a Debugger to Your Application</h2> |
| 916 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | <p>This section describes how to display debug information on the screen (such |
| 918 | as CPU usage), as well as how to hook up your IDE to debug running applications |
| 919 | on the emulator. </p> |
| 920 | |
| 921 | <p>Attaching a debugger is automated using the Eclipse plugin, |
| 922 | but you can configure other IDEs to listen on a debugging port to receive debugging |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 923 | information:</p> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | <ol> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 925 | <li><strong>Start the <a href="{@docRoot}guide/developing/tools/ddms.html">Dalvik Debug Monitor |
| 926 | Server (DDMS)</a> tool, </strong> which |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | acts as a port forwarding service between your IDE and the emulator.</li> |
| 928 | <li><strong>Set |
| 929 | optional debugging configurations on |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 930 | your emulator</strong>, such as blocking application startup for an Activity |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | until a debugger is attached. Note that many of these debugging options |
| 932 | can be used without DDMS, such as displaying CPU usage or screen refresh |
| 933 | rate on the emulator.</li> |
| Scott Main | bd13c24 | 2009-04-21 19:09:24 -0700 | [diff] [blame] | 934 | <li><strong>Configure your IDE to attach to port 8700 for debugging.</strong> Read |
| 935 | about <a href="{@docRoot}guide/developing/debug-tasks.html#ide-debug-port"> |
| 936 | Configuring Your IDE to Attach to the Debugging Port</a>. </li> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | </ol> |