Home | History | Annotate | Download | only in developing
      1 page.title=Developing In Eclipse, with ADT
      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="#AVD">Creating an AVD</a></li>
     10     <li><a href="#Running">Running Your Application</a>
     11       <ol>
     12         <li><a href="#RunningOnEmulator">Running on the emulator</a></li>
     13         <li><a href="#RunningOnDevice">Running on a device</a></li>
     14       </ol>
     15     </li>
     16     <li><a href="#RunConfig">Creating a Custom Run Configuration</a></li>
     17     <li><a href="#Signing">Setting Up Application Signing</a></li>
     18     <li><a href="#libraryProject">Working with Library Projects</a>
     19       <ol>
     20         <li><a href="#libraryReqts">Development requirements</a></li>
     21         <li><a href="#librarySetup">Setting up a library project</a></li>
     22         <li><a href="#libraryReference">Referencing a library project</a></li>
     23         <li><a href="#considerations">Development considerations</a></li>
     24         <li><a href="#libraryMigrating">Migrating library projects to ADT 0.9.8</a></li>
     25       </ol>
     26     </li>
     27     <li><a href="#Tips">Eclipse Tips</a></li>
     28   </div>
     29 </div>
     30 
     31 
     32 <p>The Android Development Tools (ADT) plugin for Eclipse adds powerful extensions to the Eclipse 
     33 integrated development environment. It allows you to create and debug Android applications easier 
     34 and faster. If you use Eclipse, the ADT plugin gives you an incredible boost in developing Android 
     35 applications:</p>
     36 
     37 <ul>
     38   <li>It gives you access to other Android development tools from inside the Eclipse IDE. For 
     39 example, ADT lets you access the many capabilities of the DDMS tool: take screenshots, manage 
     40 port-forwarding, set breakpoints, and view thread and process information directly from
     41 Eclipse.</li>
     42   <li>It provides a New Project Wizard, which helps you quickly create and set up all of the 
     43 basic files you'll need for a new Android application.</li>
     44   <li>It automates and simplifies the process of building your Android application.</li>
     45   <li>It provides an Android code editor that helps you write valid XML for your Android 
     46 manifest and resource files.</li>
     47   <li>It will even export your project into a signed APK, which can be distributed to users.</li>
     48 </ul>
     49 
     50 <p>To begin developing Android applications in the Eclipse IDE with ADT, you first need to 
     51 download the Eclipse IDE and then download and install the ADT plugin. To do so, follow the 
     52 steps given in <a href="{@docRoot}sdk/eclipse-adt.html#installing">Installing 
     53 the ADT Plugin</a>.</p>
     54 
     55 <p>If you are already developing applications using a version of ADT earlier than 0.9, make
     56 sure to upgrade to the latest version before continuing. See the guide to
     57 <a href="{@docRoot}sdk/eclipse-adt.html#updating">Updating Your ADT Plugin</a>.</p>
     58 
     59 <p class="note"><strong>Note:</strong> This guide assumes you are using the latest version of
     60 the ADT plugin. While most of the information covered also applies to previous 
     61 versions, if you are using an older version, you may want to consult this document from
     62 the set of documentation included in your SDK package (instead of the online version).</p>
     63 
     64 
     65 <h2  id="CreatingAProject">Creating an Android Project</h2>
     66 
     67 <p>The ADT plugin provides a New Project Wizard that you can use to quickly create a new
     68 Android project (or a project from existing code). To create a new project:</p>
     69 
     70 <ol>
     71   <li>Select <strong>File</strong> &gt; <strong>New</strong> &gt; <strong>Project</strong>.</li>
     72   <li>Select <strong>Android</strong> &gt; <strong>Android Project</strong>, and click
     73     <strong>Next</strong>.</li>
     74   <li>Select the contents for the project:
     75     <ul>
     76       <li>Enter a <em>Project Name</em>. This will be the name of the folder where your
     77         project is created.</li>
     78       <li>Under Contents, select <strong>Create new project in workspace</strong>.
     79         Select your project workspace location.</li>
     80       <li>Under Target, select an Android target to be used as the project's Build Target. 
     81         The Build Target
     82         specifies which Android platform you'd like your application built against.
     83         <p>Unless you know that you'll be using new APIs introduced in the latest SDK, you should
     84         select a target with the lowest platform version possible.</p>
     85         <p class="note"><strong>Note:</strong> You can change your the Build Target for your 
     86         project at any time: Right-click the project in the Package Explorer, select
     87         <strong>Properties</strong>, select <strong>Android</strong> and then check 
     88         the desired Project Target.</p>
     89       </li>
     90       <li>Under Properties, fill in all necessary fields.
     91         <ul>
     92           <li>Enter an <em>Application name</em>. This is the human-readable title for your 
     93             application &mdash; the name that will appear on the Android device.</li>
     94           <li>Enter a <em>Package name</em>. This is the package namespace (following the same rules
     95             as for packages in the Java programming language) where all your source code
     96             will reside.</li>
     97           <li>Select <em>Create Activity</em> (optional, of course, but common) and enter a name
     98             for your main Activity class.</li>
     99           <li>Enter a <em>Min SDK Version</em>. This is an integer that indicates
    100             the minimum API Level required to properly run your application. 
    101             Entering this here automatically sets the <code>minSdkVersion</code> attribute in the 
    102             <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt;</a>
    103             of your Android Manifest file. If you're unsure of the appropriate <a
    104 href="{@docRoot}guide/appendix/api-levels.html">API Level</a> to use,
    105             copy the API Level listed for the Build Target you selected in the Target tab.</li>
    106         </ul>
    107       </li>
    108     </ul>
    109   </li>
    110   <li>Click <strong>Finish</strong>.</li>
    111 </ol>
    112 
    113 <p class="note"><strong>Tip:</strong>
    114 You can also start the New Project Wizard from the <em>New</em> icon in the toolbar.</p>
    115 
    116 <p>Once you complete the New Project Wizard, ADT creates the following 
    117 folders and files in your new project:</p>
    118   <dl>
    119     <dt><code>src/</code></dt>
    120       <dd>Includes your stub Activity Java file. All other Java files for your application
    121       go here.</dd>
    122     <dt><code><em>&lt;Android Version&gt;</em>/</code> (e.g., <code>Android 1.1/</code>)</dt>
    123       <dd>Includes the <code>android.jar</code> file that your application will build against. 
    124       This is determined by the build target that you have chosen in the <em>New Project
    125       Wizard</em>.</dd>
    126     <dt><code>gen/</code></dt>
    127       <dd>This contains the Java files generated by ADT, such as your <code>R.java</code> file
    128       and interfaces created from AIDL files.</dd>
    129     <dt><code>assets/</code></dt>
    130       <dd>This is empty. You can use it to store raw asset files. </dd>
    131     <dt><code>res/</code></dt>
    132       <dd>A folder for your application resources, such as drawable files, layout files, string
    133       values, etc. See 
    134       <a href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>.</dd>
    135     <dt><code>AndroidManifest.xml</code></dt>
    136       <dd>The Android Manifest for your project. See 
    137       <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">The AndroidManifest.xml 
    138       File</a>.</dd>
    139     <dt><code>default.properties</code></dt>
    140       <dd>This file contains project settings, such as the build target. This files is integral
    141       to the project, as such, it should be maintained in a Source Revision Control system. 
    142       It should never be edited manually &mdash; to edit project properties, 
    143       right-click the project folder and select "Properties".</dd>
    144   </dl>
    145 
    146 
    147 <h2 id="AVD">Creating an AVD</h2>
    148 
    149 <p>An Android Virtual Device (AVD) is a device configuration for the emulator that
    150 allows you to model real world devices. In order to run an instance of the emulator, you must create
    151 an AVD.</p>
    152 
    153 <p>To create an AVD from Eclipse:</p>
    154 
    155 <ol>
    156   <li>Select <strong>Window > Android SDK and AVD Manager</strong>, or click the Android SDK and
    157 AVD Manager icon in the Eclipse toolbar.</p>
    158   </li>
    159   <li>In the <em>Virtual Devices</em> panel, you'll see a list of existing AVDs. Click
    160 <strong>New</strong> to create a new AVD.</li>
    161   <li>Fill in the details for the AVD.
    162     <p>Give it a name, a platform target, an SD card size, and
    163     a skin (HVGA is default).</p>
    164     <p class="note"><strong>Note:</strong> Be sure to define
    165     a target for your AVD that satisfies your application's Build Target (the AVD
    166     platform target must have an API Level equal to or greater than the API Level that your
    167 application compiles against).</p>
    168   </li>
    169   <li>Click <strong>Create AVD</strong>.</li>
    170 </ol>
    171 
    172 <p>Your AVD is now ready and you can either close the SDK and AVD Manager, create more AVDs, or
    173 launch an emulator with the AVD by selecting a device and clicking <strong>Start</strong>.</p>
    174 
    175 <p>For more information about AVDs, read the
    176 <a href="{@docRoot}guide/developing/tools/avd.html">Android Virtual Devices</a>
    177 documentation.</p>
    178 
    179 
    180 <h2 id="Running">Running Your Application</h2>
    181 
    182 <div class="sidebox-wrapper">
    183 <div class="sidebox">
    184 <h2>Use the Emulator to Test Different Configurations</h2>
    185 <p>Create multiple AVDs that each define a different device configuration with which your
    186 application is compatible, then launch each AVD into a new emulator from the SDK and AVD Manager.
    187 Set the target mode in your app's run configuration to manual, so that when you run your
    188 application, you can select from the available virtual devices.</p>
    189 </div>
    190 </div>
    191 
    192 <p>Running your application from Eclipse will usually require just a couple clicks, whether you're
    193 running it on the emulator or on an attached device. The information below describes how to get
    194 set up and run your application from Eclipse.</p>
    195 
    196 <h3 id="RunningOnEmulator">Running on the emulator</h3>
    197 
    198 <p>Before you can run your application on the Android Emulator,
    199 you <strong>must</strong> <a href="#AVD">create an AVD</a>.</p>
    200 
    201 <p>To run (or debug) your application, select <strong>Run</strong> &gt; <strong>Run</strong> (or
    202 <strong>Run</strong> &gt; <strong>Debug</strong>) from the Eclipse menu bar. The ADT plugin
    203 will automatically create a default launch configuration for the project. Eclipse will then perform
    204 the following:</p>
    205 
    206 <ol>
    207   <li>Compile the project (if there have been changes since the last build).</li>
    208   <li>Create a default launch configuration (if one does not already exist for the
    209 project).</li>
    210   <li>Install and start the application on an emulator (or device), based on the Deployment
    211 Target
    212     defined by the run configuration.
    213     <p>By default, Android run configurations use an "automatic target" mode for
    214     selecting a device target. For information on how automatic target mode selects a
    215     deployment target, see <a href="#AutoAndManualTargetModes">Automatic and manual
    216     target modes</a> below.</p>
    217   </li>
    218 </ol>
    219 
    220 <p>If debugging, the application will start in the "Waiting For Debugger" mode. Once the
    221 debugger is attached, Eclipse will open the Debug perspective.</p>
    222 
    223 <p>To set or change the launch configuration used for your project, use the launch configuration
    224 manager.
    225 See <a href="#launchconfig">Creating a Launch Configuration</a> for information.</p>
    226 
    227 <p>Be certain to create multiple AVDs upon which to test your application. You should have one AVD
    228 for each platform and screen type with which your application is compatible. For
    229 instance, if your application compiles against the Android 1.5 (API Level 3) platform, you should
    230 create an AVD for each platform equal to and greater than 1.5 and an AVD for each <a
    231 href="{@docRoot}guide/practices/screens_support.html">screen type</a> you support, then test
    232 your application on each one.</p>
    233 
    234 
    235 <h3 id="RunningOnDevice">Running on a device</h3>
    236 
    237 <p>Before you can run your application on a device, you must perform some basic setup for your
    238 device:</p>
    239 
    240 <ul>
    241   <li>Declare your application as debuggable in your manifest</li>
    242   <li>Enable USB Debugging on your device</li>
    243   <li>Ensure that your development computer can detect your device when connected via USB</li>
    244 </ul>
    245 <p>Read <a href="{@docRoot}guide/developing/device.html#setting-up">Setting up a Device for
    246 Development</a> for more information.</p>
    247 
    248 <p>Once set up and your device is connected via USB, install your application on the device by
    249 selecting <strong>Run</strong> &gt; <strong>Run</strong> (or
    250 <strong>Run</strong> &gt; <strong>Debug</strong>) from the Eclipse menu bar.</p>
    251 
    252 
    253 
    254 <h2 id="RunConfig">Creating a Run Configuration</h2>
    255 
    256 <p>The run configuration specifies the project to run, the Activity 
    257 to start, the emulator or connected device to use, and so on. When you first run a project
    258 as an <em>Android Application</em>, ADT will automatically create a run configuration.
    259 The default run configuration will
    260 launch the default project Activity and use automatic target mode for device selection 
    261 (with no preferred AVD). If the default settings don't suit your project, you can 
    262 customize the launch configuration or even create a new.</p>
    263 
    264 <p>To create or modify a launch configuration, follow these steps as appropriate 
    265 for your Eclipse version:</p>
    266 
    267 <ol>
    268   <li>Open the run configuration manager.
    269         <ul>
    270           <li>In Eclipse 3.3 (Europa), select <strong>Run</strong> &gt; 
    271             <strong>Open Run Dialog</strong> (or <strong>Open Debug Dialog</strong>)
    272           </li>
    273           <li>In Eclipse 3.4 (Ganymede), select <strong>Run </strong>&gt; 
    274             <strong>Run Configurations</strong> (or
    275             <strong>Debug Configurations</strong>)
    276           </li>
    277       </ul>
    278   </li>
    279   <li>Expand the <strong>Android Application</strong> item and create a new
    280   configuration or open an existing one.
    281     <ul>
    282       <li>To create a new configuration:
    283         <ol>
    284           <li>Select <strong>Android Application</strong> and click the <em>New launch
    285 configuration</em>
    286           icon above the list (or, right-click  <strong>Android Application</strong> and click 
    287           <strong>New</strong>).</li>
    288           <li>Enter a Name for your configuration.</li>
    289           <li>In the Android tab, browse and select the project you'd like to run with the
    290           configuration.</li>
    291         </ol>
    292       <li>To open an existing configuration, select the configuration name from the list
    293       nested below <strong>Android Application</strong>.</li>
    294     </ul>
    295   </li>
    296   <li>Adjust your desired launch configuration settings.
    297     <p>In the Target tab, consider whether you'd like to use Manual or Automatic mode
    298     when selecting an AVD to run your application.
    299     See the following section on <a href=#AutoAndManualModes">Automatic and manual target 
    300     modes</a>).</p>
    301     <p>You can specify any emulator options to the Additional Emulator Command
    302     Line Options field. For example, you could add <code>-scale 96dpi</code> to
    303     scale the AVD's screen to an accurate size, based on the dpi of your
    304     computer monitor. For a full list of emulator options, see the <a
    305     href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a>
    306 document.</p>
    307   </li>
    308 </ol>
    309 
    310 
    311 <h3 id="AutoAndManualTargetModes">Automatic and manual target modes</h3>
    312 
    313 <p>By default, a run configuration uses the <strong>automatic</strong> target mode in order to
    314 select an AVD. In this mode, ADT will select an AVD for the application in the following manner:</p>
    315 
    316 <ol>
    317   <li>If there's a device or emulator already running and its AVD configuration
    318   meets the requirements of the application's build target, the application is installed
    319   and run upon it.</li>
    320   <li>If there's more than one device or emulator running, each of which meets the requirements
    321   of the build target, a "device chooser" is shown to let you select which device to use.</li>
    322   <li>If there are no devices or emulators running that meet the requirements of the build target, 
    323   ADT looks at the available AVDs. If one meets the requirements of the build target,
    324   the AVD is used to launch a new emulator, upon which the application is installed and run.</li>
    325   <li>If all else fails, the application will not be run and you will see a console error warning
    326   you that there is no existing AVD that meets the build target requirements.</li>
    327 </ol>
    328 
    329 <p>However, if a "preferred AVD" is selected in the run configuration, then the application 
    330 will <em>always</em> be deployed to that AVD. If it's not already running, then a new emulator
    331 will be launched.</p>
    332 
    333 <p>If your run configuration uses <strong>manual</strong> mode, then the "device chooser" 
    334 is presented every time that your application is run, so that you can select which AVD to use.</p>
    335 
    336 
    337 <h2 id="Signing">Signing your Applications</h2>
    338 
    339 <p>As you begin developing Android applications, understand that all
    340 Android applications must be digitally signed before the system will install
    341 them on an emulator or an actual device. There are two ways to do this:
    342 with a debug key (for immediate testing on an emulator or development device)
    343 or with a private key (for application distribution).</p>
    344 
    345 <p>The ADT plugin helps you get started quickly by signing your .apk files with
    346 a debug key, prior to installing them on an emulator or development device. This means that you can
    347 quickly run your application from Eclipse without having to
    348 generate your own private key. No specific action on your part is needed, 
    349 provided ADT has access to Keytool.However, please note that if you intend
    350 to publish your application, you <strong>must</strong> sign the application with your
    351 own private key, rather than the debug key generated by the SDK tools.</p>
    352 
    353 <p>Please read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your
    354 Applications</a>, which provides a thorough guide to application signing on Android
    355 and what it means to you as an Android application developer. The document also includes
    356 a guide to exporting and signing your application with the ADT's Export Wizard.</p>
    357 
    358 
    359 <h2 id="libraryProject">Working with Library Projects</h2>
    360 
    361 <div class="sidebox-wrapper">
    362 <div class="sidebox">
    363 <h2>Library project example code</h2>
    364 
    365 <p>The SDK includes an example application called TicTacToeMain that shows how a
    366 dependent application can use code and resources from an Android Library
    367 project. The TicTacToeMain application uses code and resources from an example
    368 library project called TicTacToeLib. 
    369 
    370 <p style="margin-top:1em;">To download the sample applications and run them as
    371 projects in your environment, use the <em>Android SDK and AVD Manager</em> to
    372 download the "Samples for SDK API 8" component into your SDK. </p>
    373 
    374 <p style="margin-top:1em;">For more information and to browse the code of the
    375 samples, see the <a
    376 href="{@docRoot}resources/samples/TicTacToeMain/index.html">TicTacToeMain
    377 application</a>.</p>
    378 </div>
    379 </div>
    380 
    381 <p>An Android <em>library project</em> is a development project that holds
    382 shared Android source code and resources. Other Android application projects can
    383 reference the library project and, at build time, include its compiled sources
    384 in their <code>.apk</code> files. Multiple application projects can reference
    385 the same library project and any single application project can reference
    386 multiple library projects. </p>
    387 
    388 <p>If you have source code and resources that are common to multiple application
    389 projects, you can move them to a library project so that it is easier to
    390 maintain across applications and versions. Here are some common scenarios in
    391 which you could make use of library projects: </p>
    392 
    393 <ul>
    394 <li>If you are developing multiple related applications that use some of the
    395 same components, you could move the redundant components out of their respective
    396 application projects and create a single, reuseable set of the same components
    397 in a library project. </li>
    398 <li>If you are creating an application that exists in both free and paid
    399 versions, you could move the part of the application that is common to both versions
    400 into a library project. The two dependent projects, with their different package
    401 names, will reference the library project and provide only the difference
    402 between the two application versions.</li>
    403 </ul>
    404 
    405 <p>Structurally, a library project is similar to a standard Android application
    406 project. For example, it includes a manifest file at the project root, as well
    407 as <code>src/</code>, <code>res/</code> and similar directories. The project can
    408 contain the same types of source code and resources as a standard
    409 Android project, stored in the same way. For example, source code in the library
    410 project can access its own resources through its <code>R</code> class. </p>
    411 
    412 <p>However, a library project differs from an standard Android application
    413 project in that you cannot compile it directly to its own <code>.apk</code> or
    414 run it on the Android platform. Similarly, you cannot export the library project
    415 to a self-contained JAR file, as you would do for a true library. Instead, you
    416 must compile the library indirectly, by referencing the library from a dependent
    417 application's build path, then building that application. </p>
    418 
    419 <p>When you build an application that depends on a library project, the SDK
    420 tools compile the library and merge its sources with those in the main project,
    421 then use the result to generate the <code>.apk</code>. In cases where a resource
    422 ID is defined in both the application and the library, the tools ensure that the
    423 resource declared in the application gets priority and that the resource in the
    424 library project is not compiled into the application <code>.apk</code>. This
    425 gives your application the flexibility to either use or redefine any resource
    426 behaviors or values that are defined in any library.</p>
    427 
    428 <p>To organize your code further, your application can add references to
    429 multiple library projects, then specify the relative priority of the resources
    430 in each library. This lets you build up the resources actually used in your
    431 application in a cumulative manner. When two libraries referenced from an
    432 application define the same resource ID, the tools select the resource from the
    433 library with higher priority and discard the other. </p>
    434 
    435 <p>ADT lets you add references to library projects and set their relative
    436 priority from the application project's Properties. As shown in Figure 2,
    437 below, once you've added a reference to a library project, you can use the
    438 <strong>Up</strong> and <strong>Down</strong> controls to change the ordering,
    439 with the library listed at the top getting the higher priority. At build time,
    440 the libraries are merged with the application one at a time, starting from the
    441 lowest priority to the highest. </p>
    442 
    443 <p>Note that a library project cannot itself reference another library project
    444 and that, at build time, library projects are <em>not</em> merged with each
    445 other before being merged with the application. However, note that a library can
    446 import an external library (JAR) in the normal way.</p>
    447 
    448 <p>The sections below describe how to use ADT to set up and manage library your
    449 projects. Once you've set up your library projects and moved code into them, you
    450 can import library classes and resources to your application in the normal way.
    451 </p>
    452 
    453 
    454 <h3 id="libraryReqts">Development requirements</h3>
    455 
    456 <p>Android library projects are a build-time construct, so you can use them to
    457 build a final application <code>.apk</code> that targets any API level and is
    458 compiled against any version of the Android library. </p>
    459 
    460 <p>However, to use library projects, you need to update your development
    461 environment to use the latest tools and platforms, since older releases of the
    462 tools and platforms do not support building with library projects. Specifically,
    463 you need to download and install the versions listed below:</p>
    464 
    465 <p class="table-caption"><strong>Table 1.</strong> Minimum versions of SDK tools
    466 and plaforms on which you can develop library projects.</p>
    467 
    468 <table>
    469 <tr>
    470 <th>Component</th>
    471 <th>Minimum Version</th>
    472 </tr>
    473 <tr>
    474 <td>SDK Tools</td>
    475 <td>r6 (or higher)</td>
    476 </tr>
    477 <tr><td>Android 2.2 platform</td><td>r1 (or higher)</td></tr>
    478 <tr><td>Android 2.1 platform</td><td>r2 (or higher)</td></tr>
    479 <tr><td style="color:gray">Android 2.0.1 platform</td><td style="color:gray"><em>not supported</em></td></tr>
    480 <tr><td style="color:gray">Android 2.0 platform</td><td style="color:gray"><em>not supported</em></td></tr>
    481 <tr><td>Android 1.6 platform</td><td>r3 (or higher)</td></tr>
    482 <tr><td>Android 1.5 platform</td><td>r4 (or higher)</td></tr>
    483 <tr><td>ADT Plugin</td><td>0.9.7 (or higher)</td></tr>
    484 </table>
    485 
    486 <p>You can download the tools and platforms using the <em>Android SDK and AVD
    487 Manager</em>, as described in <a href="{@docRoot}sdk/adding-components.html">Adding SDK
    488 Components</a>. To install or update ADT, use the Eclipse Updater as described
    489 in <a href="{@docRoot}sdk/eclipse-adt.html">ADT Plugin for Eclipse</a>.</p>
    490 
    491 
    492 <h3 id="librarySetup">Setting up a library project</h3>
    493 
    494 <p>A library project is a standard Android project, so you can create a new one in the
    495 same way as you would a new application project. Specifically, you can use
    496 the New Project Wizard, as described in <a href="#CreatingAProject">Creating an
    497 Android Project</a>, above. </p>
    498 
    499 <p>When you are creating the library project, you can select any application
    500 name, package, and set other fields as needed, as shown in the diagram below.
    501 Click Finish to create the project in the workspace.</p>
    502 
    503 <p>Next, set the project's Properties to indicate that it is a library project:</p>
    504 
    505 <ol>
    506 <li>In the <strong>Package Explorer</strong>, right-click the library project
    507 and select <strong>Properties</strong>.</li>
    508 <li>In the <strong>Properties</strong> window, select the "Android" properties
    509 group at left and locate the <strong>Library</strong> properties at right. </li>
    510 <li>Select the "is Library" checkbox and click <strong>Apply</strong>.</li>
    511 <li>Click <strong>OK</strong> to close the <strong>Properties</strong> window.</li>
    512 </ol>
    513 
    514 <p>The new project is now marked as a library project. You can begin moving
    515 source code and resources into it, as described in the sections below. </p>
    516 
    517 <p>You can also convert an existing application project into a library. To do
    518 so, simply open the Properties for the project and select the "is Library"
    519 checkbox. Other application projects can now reference the existing project as a
    520 library project.</p>
    521 
    522 <img src="{@docRoot}images/developing/adt-props-isLib.png" style="margin:0;padding:0;" />
    523 <p class="img-caption" style="margin-left:3em;margin-bottom:2em;"><strong>Figure 1.</strong>
    524 Marking a project as an Android library project. </p>
    525 
    526 <h4>Creating the manifest file</h4>
    527 
    528 <p>A library project's manifest file must declare all of the shared components
    529 that it includes, just as would a standard Android application. For more
    530 information, see the documentation for <a
    531 href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p>
    532 
    533 <p>For example, the <a
    534 href="{@docRoot}resources/samples/TicTacToeLib/AndroidManifest.html">TicTacToeLib</a>
    535 example library project declares the Activity <code>GameActivity</code>: </p>
    536 
    537 <pre>&lt;manifest&gt;
    538   ...
    539   &lt;application&gt;
    540     ...
    541     &lt;activity android:name="GameActivity" /&gt;
    542     ...
    543   &lt;/application&gt;
    544   ...
    545 &lt;/manifest&gt;</pre>
    546 
    547 
    548 <h3 id="libraryReference">Referencing a library project from an application</h3>
    549 
    550 <p>If you are developing an application and want to include the shared code or
    551 resources from a library project, you can do so easily by adding a reference to
    552 the library project in the application project's Properties.</p>
    553 
    554 <p>To add a reference to a library project, follow these steps: </p>
    555 
    556 <ol>
    557 <li>In the <strong>Package Explorer</strong>, right-click the dependent project
    558 and select <strong>Properties</strong>.</li>
    559 <li>In the <strong>Properties</strong> window, select the "Android" properties group
    560 at left and locate the <strong>Library</strong> properties at right.</li>
    561 <li>Click <strong>Add</strong> to open the <strong>Project Selection</strong>
    562 dialog. </li>
    563 <li>From the list of available library projects, select a project and click
    564 <strong>OK</strong>.</li>
    565 <li>When the dialog closes, click <strong>Apply</strong> in the
    566 <strong>Properties</strong> window.</li>
    567 <li>Click <strong>OK</strong> to close the <strong>Properties</strong> window.</li>
    568 </ol>
    569 
    570 <p>As soon as the Properties dialog closes, Eclipse rebuilds the project,
    571 including the contents of the library project. </p>
    572 
    573 <p>The figure below shows the Properties dialog that lets you add library
    574 references and move them up and down in priority. </p>
    575 
    576 <img src="{@docRoot}images/developing/adt-props-libRef.png" style="margin:0;padding:0;" />
    577 <p class="img-caption" style="margin-left:3em;margin-bottom:2em;"><strong>Figure 2.</strong>
    578 Adding a reference to a library project in the properties of an application project. </p>
    579 
    580 <p>If you are adding references to multiple libraries, note that you can set
    581 their relative priority (and merge order) by selecting a library and using the
    582 <strong>Up</strong> and <strong>Down</strong> controls. The tools merge the
    583 referenced libraries with your application starting from lowest priority (bottom
    584 of the list) to highest (top of the list). If more than one library defines the
    585 same resource ID, the tools select the resource from the library with higher
    586 priority. The application itself has highest priority and its resources are
    587 always used in preference to identical resource IDs defined in libraries.</p>
    588 
    589 <h4>Declaring library components in the the manifest file</h4>
    590 
    591 <p>In the manifest file of the application project, you must add declarations
    592 of all components that the application will use that are imported from a library
    593 project. For example, you must declare any <code>&lt;activity&gt;</code>,
    594 <code>&lt;service&gt;</code>, <code>&lt;receiver&gt;</code>,
    595 <code>&lt;provider&gt;</code>, and so on, as well as
    596 <code>&lt;permission&gt;</code>, <code>&lt;uses-library&gt;</code>, and similar
    597 elements.</p>
    598 
    599 <p>Declarations should reference the library components by their fully-qualified
    600 package names, where appropriate. </p>
    601 
    602 <p>For example, the <a 
    603 href="{@docRoot}resources/samples/TicTacToeMain/AndroidManifest.html">TicTacToeMain</a>
    604 example application declares the library Activity <code>GameActivity</code>
    605 like this: </p>
    606 
    607 <pre>&lt;manifest&gt;
    608   ...
    609   &lt;application&gt;
    610     ...
    611     &lt;activity android:name="com.example.android.tictactoe.library.GameActivity" /&gt;
    612     ...
    613   &lt;/application&gt;
    614   ...
    615 &lt;/manifest&gt;</pre>
    616 
    617 <p>For more information about the manifest file, see the documentation for <a
    618 href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p>
    619 
    620 
    621 <h3 id="considerations">Development considerations</h3>
    622 
    623 <p>As you develop your library project and dependent applications, keep the 
    624 points listed below in mind.</p>
    625 
    626 <p><strong>Resource conflicts</strong></p>
    627 
    628 <p>Since the tools merge the resources of a library project with those of a
    629 dependent application project, a given resource ID might be defined in both
    630 projects. In this case, the tools select the resource from the application, or
    631 the library with highest priority, and discard the other resource. As you
    632 develop your applications, be aware that common resource IDs are likely to be
    633 defined in more than one project and will be merged, with the resource from the
    634 application or highest-priority library taking precedence.</p>
    635 
    636 <p><strong>Using prefixes to avoid resource conflicts</strong></p>
    637 
    638 <p>To avoid resource conflicts for common resource IDs, consider using a prefix
    639 or other consistent naming scheme that is unique to the project (or is  unique
    640 across all projects). </p>
    641 
    642 <p><strong>No export of library project to JAR</strong></p>
    643 
    644 <p>A library cannot be distributed as a binary file (such as a jar file). This
    645 is because the library project is compiled by the main project to use the
    646 correct resource IDs.</p>
    647 
    648 <p><strong>A library project can include a JAR library</strong></p>
    649 
    650 <p>You can develop a library project that itself includes a JAR library, however
    651 you need to manually edit the dependent application project's build path and add
    652 a path to the JAR file. </p>
    653 
    654 <p><strong>A library project can depend on an external JAR library</strong></p>
    655 
    656 <p>You can develop a library project that depends on an external library (for
    657 example, the Maps external library). In this case, the dependent application
    658 must build against a target that includes the external library (for example, the
    659 Google APIs Add-On). Note also that both the library project and the dependent
    660 application must declare the external library their manifest files, in a <a
    661 href="{@docRoot}guide/topics/manifest/uses-library-element.html"><code>&lt;uses-library&gt;</code></a>
    662 element. </p>
    663 
    664 <p><strong>Library project can not include raw assets</strong></p>
    665 
    666 <p>The tools do not support the use of raw asset files in a library project.
    667 Any asset resources used by an application must be stored in the 
    668 <code>assets/</code> directory of the application project
    669 itself.</p>
    670 
    671 <p><strong>Targeting different Android platform versions in library project and
    672 application project</strong></p>
    673 
    674 <p>A library is compiled as part of the dependent application project, so the
    675 API used in the library project must be compatible with the version of the
    676 Android library used to compile the application project. In general, the library
    677 project should use an <a href="{@docRoot}guide/appendix/api-levels.html">API level</a>
    678 that is the same as &mdash; or lower than &mdash; that used by the application.
    679 If the library project uses an API level that is higher than that of the
    680 application, the application project will fail to compile. It is perfectly
    681 acceptable to have a library that uses the Android 1.5 API (API level 3) and
    682 that is used in an Android 1.6 (API level 4) or Android 2.1 (API level 7)
    683 project, for instance.</p>
    684 
    685 <p><strong>No restriction on library package name</strong></p>
    686 
    687 <p>There is no requirement for the package name of a library to be the same as
    688 that of applications that use it.</p>
    689 
    690 <p><strong>Multiple R classes in gen/ folder of application project</strong></p>
    691 
    692 <p>When you build the dependent application project, the code of any libraries
    693 is compiled and merged to the application project. Each library has its own
    694 <code>R</code> class, named according to the library's package name. The
    695 <code>R</code> class generated from the resources of the main project and of the
    696 library is created in all the packages that are needed including the main
    697 projects package and the libraries packages.</p>
    698 
    699 <p><strong>Testing a library project</strong></p>
    700 
    701 <p>There are two recommended ways of setting up testing on code and resources in
    702 a library project: </p>
    703 
    704 <ul>
    705 <li>You can set up a <a
    706 href="{@docRoot}guide/developing/testing/testing_otheride.html">test project</a>
    707 that instruments an application project that depends on the library project. You
    708 can then add tests to the project for library-specific features.</li>
    709 <li>You can set up a set up a standard application project that depends on the
    710 library and put the instrumentation in that project. This lets you create a
    711 self-contained project that contains both the tests/instrumentations and the
    712 code to test.</li>
    713 </ul>
    714 
    715 <p><strong>Library project storage location</strong></p>
    716 
    717 <p>There are no specific requirements on where you should store a library
    718 project, relative to a dependent application project, as long as the application
    719 project can reference the library project by a relative link. You can place the
    720 library project What is important is that the main project can reference the
    721 library project through a relative link.</p>
    722 
    723 <h3 id="libraryMigrating">Migrating library projects to ADT 0.9.8</h3>
    724 
    725 <p>This section provides information about how to migrate a library project
    726 created with ADT 0.9.7 to ADT 0.9.8 (or higher). The migration is needed only if
    727 you are developing in Eclipse with ADT and assumes that you have also upgraded
    728 to SDK Tools r7 (or higher). </p>
    729 
    730 <p>The way that ADT handles library projects has changed between
    731 ADT 0.9.7 and ADT 0.9.8. Specifically, in ADT 0.9.7, the <code>src/</code>
    732 source folder of the library was linked into the dependent application project
    733 as a folder that had the same name as the library project. This worked because
    734 of two restrictions on the library projects:</p>
    735 
    736 <ul>
    737 <li>The library was only able to contain a single source folder (excluding the
    738 special <code>gen/</code> source folder), and</li>
    739 <li>The source folder was required to have the name <code>src/</code> and be
    740 stored at the root of the project.</li>
    741 </ul>
    742 
    743 <p>In ADT 0.9.8, both of those restrictions were removed. A library project can
    744 have as many source folders as needed and each can have any name. Additionally,
    745 a library project can store source folders in any location of the project. For
    746 example, you could store sources in a <code>src/java/</code> directory. In order
    747 to support this, the name of the linked source folders in the main project are
    748 now called &lt;<em>library-name</em>&gt;_&lt;<em>folder-name</em>&gt; For
    749 example: <code>MyLibrary_src/</code> or <code>MyLibrary_src_java/</code>.</p>
    750 
    751 <p>Additionally, the linking process now flags those folders in order for ADT to
    752 recognize that it created them. This will allow ADT to automatically migrate the
    753 project to new versions of ADT, should they contain changes to the handling of
    754 library projects. ADT 0.9.7 did not flag the linked source folders, so ADT 0.9.8
    755 cannot be sure whether the old linked folders can be removed safely. After
    756 upgrading ADT to 0.9.8, you will need to remove the old linked folders manually
    757 in a simple two-step process, as described below.</p>
    758 
    759 <p>Before you begin, make sure to create a backup copy of your application or
    760 save the latest version to your code version control system. This ensures that
    761 you will be able to easily revert the migration changes in case there is a
    762 problem in your environment.</p>
    763 
    764 <p>When you first upgrade to ADT 0.9.8, your main project will look as shown
    765 below, with two linked folders (in this example, <code>MyLibrary</code> and
    766 <code>MyLibrary_src</code> &mdash; both of which link to
    767 <code>MyLibrary/src</code>. Eclipse shows an error on one of them because they
    768 are duplicate links to a single class.</p>
    769 
    770 <img src="{@docRoot}images/developing/lib-migration-0.png" alt="">
    771 
    772 <p>To fix the error, remove the linked folder that <em>does not</em> contain the
    773 <code>_src</code> suffix. </p>
    774 
    775 <ol>
    776 <li>Right click the folder that you want to remove (in this case, the
    777 <code>MyLibrary</code> folder) and choose <strong>Build Path</strong> &gt;
    778 <strong>Remove from Build Path</strong>, as shown below.</li>
    779 
    780 <img src="{@docRoot}images/developing/lib-migration-1.png" style="height:600px"
    781 alt="">
    782 
    783 <li>Next, When asked about unlinking the folder from the project, select
    784 <strong>Yes</strong>, as shown below.</li>
    785 
    786 <img src="{@docRoot}images/developing/lib-migration-2.png" alt="">
    787 </ol>
    788 
    789 <p>This should resolve the error and migrate your library project to the new
    790 ADT environment. </p>
    791 
    792 <h2 id="Tips">Eclipse Tips</h2>
    793 
    794 <h3 id="arbitraryexpressions">Executing arbitrary Java expressions in Eclipse</h3>
    795 
    796 <p>You can execute arbitrary code when paused at a breakpoint in Eclipse. For example,
    797     when in a function with a String argument called &quot;zip&quot;, you can get
    798     information about packages and call class methods. You can also invoke arbitrary
    799     static methods: for example, entering <code>android.os.Debug.startMethodTracing()</code> will
    800     start dmTrace. </p>
    801 <p>Open a code execution window, select <strong>Window</strong> &gt; <strong>Show
    802         View</strong> &gt; <strong>Display</strong> from the main menu to open the
    803         Display window, a simple text editor. Type your expression, highlight the
    804         text, and click the 'J' icon (or CTRL + SHIFT + D) to run your
    805         code. The code runs in the context of the selected thread, which must be
    806         stopped at a breakpoint or single-step point. (If you suspend the thread
    807         manually, you have to single-step once; this doesn't work if the thread is
    808         in Object.wait().)</p>
    809 <p>If you are currently paused on a breakpoint, you can simply highlight and execute
    810     a piece of source code by pressing CTRL + SHIFT + D. </p>
    811 <p>You can highlight a block of text within the same scope by pressing ALT +SHIFT
    812     + UP ARROW to select larger and larger enclosing blocks, or DOWN ARROW to select
    813     smaller blocks. </p>
    814 <p>Here are a few sample inputs and responses in Eclipse using the Display window.</p>
    815 
    816 <table width="100%" border="1">
    817     <tr>
    818         <th scope="col">Input</th>
    819         <th scope="col">Response</th>
    820     </tr>
    821     <tr>
    822         <td><code>zip</code></td>
    823         <td><code>(java.lang.String) 
    824         /work/device/out/linux-x86-debug/android/app/android_sdk.zip</code></td>
    825     </tr>
    826     <tr>
    827         <td><code>zip.endsWith(&quot;.zip&quot;)</code></td>
    828         <td><code>(boolean) true</code></td>
    829     </tr>
    830     <tr>
    831         <td><code>zip.endsWith(&quot;.jar&quot;)</code></td>
    832         <td><code>(boolean) false</code></td>
    833     </tr>
    834 </table>
    835 <p>You can also execute arbitrary code when not debugging by using a scrapbook page.
    836     Search the Eclipse documentation for &quot;scrapbook&quot;.</p>
    837 
    838 
    839 <h3>Running DDMS Manually</h3>
    840 
    841 <p>Although the recommended way to debug is to use the ADT plugin, you can manually run 
    842 DDMS and configure Eclipse to debug on port 8700. (<strong>Note: </strong>Be sure that you 
    843 have first started <a href="{@docRoot}guide/developing/tools/ddms.html">DDMS</a>). </p>
    844 
    845 
    846 
    847 
    848 <!-- TODO: clean this up and expand it to cover more wizards and features
    849 <h3>ADT Wizards</h3>
    850 
    851 <p>Notice that the "New Android Project" wizard has been expanded to use the multi-platform
    852 capabilities of the new SDK.</p>
    853 
    854 <p>There is now a "New XML File" wizard that lets you create skeleton XML resource
    855 files for your Android projects. This makes it easier to create a new layout, a new menu, a
    856 new strings file, etc.</p>
    857 
    858 <p>Both wizards are available via <strong>File > New</strong> and new icons in the main
    859 Eclipse toolbar (located to the left of the Debug and Run icons). 
    860 If you do not see the new icons, you may need to select <strong>Window > Reset
    861 Perspective</strong> from the Java perspective.</p>
    862 -->
    863