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