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