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