blob: 45fd5a1c90d3ed60d75ec66881495638a4251428 [file] [log] [blame]
Robert Lybf6c3382010-12-22 11:05:10 -08001page.title=Creating and Managing Projects
2@jd:body
3
4 <div id="qv-wrapper">
5 <div id="qv">
6 <h2>In this document</h2>
7
8 <ol>
9 <li><a href="#ApplicationProjects">Android Projects</a></li>
10
Robert Lyc74a69a82011-01-04 22:48:02 -080011 <li><a href="#LibraryProjects">Library Projects</a>
12 <ol>
13 <li><a href="#libraryMigrating">Migrating library projects to ADT 0.9.8 or higher</a></li>
14 </ol>
15 </li>
Robert Lybf6c3382010-12-22 11:05:10 -080016
17 <li><a href="#TestProjects">Test Projects</a></li>
18 </ol>
19 </div>
20 </div>
21
22 <p>Projects act as containers for storing things such as code and resource files. The SDK tools
23 expect your projects to follow a specific structure so it can compile and package your
24 application correctly, so it is highly recommended that you create them with Eclipse and ADT or
25 with the <code>android</code> tool on the command line. There are three types of projects, and
26 they all share the same general structure but differ in function:</p>
27
28 <dl>
29 <dt><strong>Android Projects</strong></dt>
30
31 <dd>An Android project is the container for your application's source code, resource files, and
32 files such as the Ant build and Android Manifest file. An application project is the main type
33 of project and the contents are eventually built into an <code>.apk</code> file that you install on a
34 device.</dd>
35
36 <dt><strong>Test Projects</strong></dt>
37
38 <dd>These projects contain code to test your application projects and are built into
39 applications that run on a device.</dd>
40
41 <dt><strong>Library Projects</strong></dt>
42
43 <dd>These projects contain shareable Android source code and resources that you can reference
44 in Android projects. This is useful when you have common code that you want to reuse.
45 Library projects cannot be installed onto a device, however, they are
46 pulled into the <code>.apk</code> file at build time.</dd>
47 </dl>
48
49 <p>When you use the Android development tools to create a new project, the essential files and
50 folders will be created for you. There are only a handful of files and folders generated for you,
51 and some of them depend on whether you use the Eclipse plugin or the {@code android} tool to
52 generate your project. As your application grows in complexity, you might require new kinds of
53 resources, directories, and files.</p>
54
55 <h2 id="ApplicationProjects">Android Projects</h2>
56
57 <p>Android projects are the projects that eventually get built into an <code>.apk</code> file that you install
58 onto a device. They contain things such as application source code and resource files.
59 Some are generated for you by default, while others should be created if
60 required. The following directories and files comprise an Android project:</p>
61
62 <dl>
63 <dt><code>src/</code></dt>
64
65 <dd>Contains your stub Activity file, which is stored at
66 <code>src<em>/your/package/namespace/ActivityName</em>.java</code>. All other source code
67 files (such as <code>.java</code> or <code>.aidl</code> files) go here as well.</dd>
68
69 <dt><code>bin</code></dt>
70
71 <dd>Output directory of the build. This is where you can find the final <code>.apk</code> file and other
72 compiled resources.</dd>
73
74 <dt><code>jni</code></dt>
75
76 <dd>Contains native code sources developed using the Android NDK. For more information, see the
77 <a href="{@docRoot}sdk/ndk/index.html">Android NDK documentation</a>.</dd>
78
79 <dt><code>gen/</code></dt>
80
81 <dd>Contains the Java files generated by ADT, such as your <code>R.java</code> file and
82 interfaces created from AIDL files.</dd>
83
84 <dt><code>assets/</code></dt>
85
86 <dd>This is empty. You can use it to store raw asset files. Files that you save here are
87 compiled into an <code>.apk</code> file as-is, and the original filename is preserved. You can navigate this
88 directory in the same way as a typical file system using URIs and read files as a stream of
89 bytes using the the {@link android.content.res.AssetManager}. For example, this is a good
90 location for textures and game data.</dd>
91
92 <dt><code>res/</code></dt>
93
94 <dd>
95 Contains application resources, such as drawable files, layout files, and string values. See
96 <a href="{@docRoot}guide/topics/resources/index.html">Application Resources</a> for more
97 information.
98
99 <dl>
100 <dt><code>anim/</code></dt>
101
102 <dd>For XML files that are compiled into animation objects. See the <a href=
103 "{@docRoot}guide/topics/resources/animation-resource.html">Animation</a> resource
104 type.</dd>
105
106 <dt><code>color/</code></dt>
107
108 <dd>For XML files that describe colors. See the <a href=
109 "{@docRoot}guide/topics/resources/color-list-resource.html">Color Values</a> resource
110 type.</dd>
111
112 <dt><code>drawable/</code></dt>
113
114 <dd>For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe
115 Drawable shapes or a Drawable objects that contain multiple states (normal, pressed, or
116 focused). See the <a href=
117 "{@docRoot}guide/topics/resources/drawable-resource.html">Drawable</a> resource type.</dd>
118
119 <dt><code>layout/</code></dt>
120
121 <dd>XML files that are compiled into screen layouts (or part of a screen). See the <a href=
122 "{@docRoot}guide/topics/resources/layout-resource.html">Layout</a> resource type.</dd>
123
124 <dt><code>menu/</code></dt>
125
126 <dd>For XML files that define application menus.
127 See the <a href="{@docRoot}guide/topics/resources/menu-resource.html">Menus</a>
128 resource type.</dd>
129
130 <dt><code>raw/</code></dt>
131
132 <dd>For arbitrary raw asset files. Saving asset files here instead of in the
133 <code>assets/</code> directory only differs in the way that you access them. These files
134 are processed by aapt and must be referenced from the application using a resource
135 identifier in the {@code R} class. For example, this is a good place for media, such as MP3
136 or Ogg files.</dd>
137
138 <dt><code>values/</code></dt>
139
140 <dd>For XML files that are compiled into many kinds of resource. Unlike other resources in
141 the <code>res/</code> directory, resources written to XML files in this folder are not
142 referenced by the file name. Instead, the XML element type controls how the resources is
143 defined within them are placed into the {@code R} class.</dd>
144
145 <dt><code>xml/</code></dt>
146
147 <dd>For miscellaneous XML files that configure application components. For example, an XML
148 file that defines a {@link android.preference.PreferenceScreen}, {@link
149 android.appwidget.AppWidgetProviderInfo}, or <a href=
150 "{@docRoot}reference/android/app/SearchManager.html#SearchabilityMetadata">Searchability
151 Metadata</a>. See <a href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>
152 for more information about configuring these application components.</dd>
153 </dl>
154 </dd>
155
156 <dt><code>libs/</code></dt>
157
158 <dd>Contains private libraries.</dd>
159
160 <dt><code>AndroidManifest.xml</code></dt>
161
162 <dd>The control file that describes the nature of the application and each of its components.
163 For instance, it describes: certain qualities about the activities, services, intent receivers,
164 and content providers; what permissions are requested; what external libraries are needed; what
165 device features are required, what API Levels are supported or required; and others. See the
166 <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>
167 documentation for more information</dd>
168
169 <dt><code>build.properties</code></dt>
170
171 <dd>Customizable properties for the build system. You can edit this file to override default
172 build settings used by Ant and provide a pointer to your keystore and key alias so that the
173 build tools can sign your application when built in release mode. If you use Eclipse, this file
174 is not used.</dd>
175
176 <dt><code>build.xml</code></dt>
177
178 <dd>The Ant build file for your project. This is only applicable for projects that
179 you create on the command line.</dd>
180
181 <dt><code>default.properties</code></dt>
182
183 <dd>This file contains project settings, such as the build target. This files is integral to
184 the project, as such, it should be maintained in a Source Revision Control system. Do not edit
185 the file manually.</dd>
186 </dl>
187
188 <h2 id="LibraryProjects">Library Projects</h2>
189
190 <div class="sidebox-wrapper">
191 <div class="sidebox">
192 <h2>Library project example code</h2>
193
194 <p>The SDK includes an example application called <code>TicTacToeMain</code> that shows how a dependent
195 application can use code and resources from an Android Library project. The TicTacToeMain
196 application uses code and resources from an example library project called TicTacToeLib.</p>
197
198 <p>To download the sample applications and run them as projects in
199 your environment, use the <em>Android SDK and AVD Manager</em> to download the "Samples for
200 SDK API 8" component into your SDK.</p>
201
202 <p>For more information and to browse the code of the samples, see
203 the <a href="{@docRoot}resources/samples/TicTacToeMain/index.html">TicTacToeMain
204 application</a>.</p>
205 </div>
206 </div>
207
208 <p>An Android <em>library project</em> is a development project that holds shared Android
209 source code and resources. Other Android application projects can reference the library project
210 and, at build time, include its compiled sources in their <code>.apk</code> files. Multiple
211 application projects can reference the same library project and any single application project
212 can reference multiple library projects.</p>
213
214 <p>If you have source code and resources that are common to multiple Android projects, you
215 can move them to a library project so that it is easier to maintain across applications and
216 versions. Here are some common scenarios in which you could make use of library projects:</p>
217
218 <ul>
219 <li>If you are developing multiple related applications that use some of the same components,
220 you move the redundant components out of their respective application projects and create a
221 single, reuseable set of the same components in a library project.</li>
222
223 <li>If you are creating an application that exists in both free and paid versions. You move
224 the part of the application that is common to both versions into a library project. The two
225 dependent projects, with their different package names, will reference the library project
226 and provide only the difference between the two application versions.</li>
227 </ul>
228
229 <p>Structurally, a library project is similar to a standard Android application project. For
230 example, it includes a manifest file at the project root, as well as <code>src/</code>,
231 <code>res/</code> and similar directories. The project can contain the same types of source
232 code and resources as a standard Android project, stored in the same way. For example, source
233 code in the library project can access its own resources through its <code>R</code> class.</p>
234
235 <p>However, a library project differs from an standard Android application project in that you
236 cannot compile it directly to its own <code>.apk</code> and run it on an Android device.
237 Similarly, you cannot export the library project to a self-contained JAR file, as you would do
238 for a true library. Instead, you must compile the library indirectly, by referencing the
239 library in the dependent application and building that application.</p>
240
241 <p>When you build an application that depends on a library project, the SDK tools compile the
242 library and merge its sources with those in the main project, then use the result to generate
243 the <code>.apk</code>. In cases where a resource ID is defined in both the application and the
244 library, the tools ensure that the resource declared in the application gets priority and that
245 the resource in the library project is not compiled into the application <code>.apk</code>.
246 This gives your application the flexibility to either use or redefine any resource behaviors or
247 values that are defined in any library.</p>
248
249 <p>To organize your code further, your application can add references to multiple library
250 projects, then specify the relative priority of the resources in each library. This lets you
251 build up the resources actually used in your application in a cumulative manner. When two
252 libraries referenced from an application define the same resource ID, the tools select the
253 resource from the library with higher priority and discard the other.</p>
254
255 <p>Once you have added references to library projects to your Android project,
256 you can set their relative priority. At build time, the
257 libraries are merged with the application one at a time, starting from the lowest priority to
258 the highest.</p>
259
260 <p>Note that a library project cannot itself reference another library project and that, at
261 build time, library projects are <em>not</em> merged with each other before being merged with
262 the application. However, note that a library can import an external library (JAR) in the
263 normal way.</p>
264
265 <h3 id="libraryReqts">Development requirements</h3>
266
267 <p>Android library projects are a build-time construct, so you can use them to build a final
268 application <code>.apk</code> that targets any API level and is compiled against any version of
269 the Android library.</p>
270
271 <p>However, to use library projects, you need to update your development environment to use the
272 latest tools and platforms, since older releases of the tools and platforms do not support
273 building with library projects. Specifically, you need to download and install the versions
274 listed below:</p>
275
276 <p class="table-caption"><strong>Table 1.</strong> Minimum versions of SDK tools and platforms on
277 which you can develop library projects.</p>
278
279 <table>
280 <tr>
281 <th>Component</th>
282
283 <th>Minimum Version</th>
284 </tr>
285
286 <tr>
287 <td>SDK Tools</td>
288
289 <td>r6 (or higher)</td>
290 </tr>
291
292 <tr>
293 <td>Android 2.2 platform</td>
294
295 <td>r1 (or higher)</td>
296 </tr>
297
298 <tr>
299 <td>Android 2.1 platform</td>
300
301 <td>r2 (or higher)</td>
302 </tr>
303
304 <tr>
305 <td style="color:gray">Android 2.0.1 platform</td>
306
307 <td style="color:gray"><em>not supported</em></td>
308 </tr>
309
310 <tr>
311 <td style="color:gray">Android 2.0 platform</td>
312
313 <td style="color:gray"><em>not supported</em></td>
314 </tr>
315
316 <tr>
317 <td>Android 1.6 platform</td>
318
319 <td>r3 (or higher)</td>
320 </tr>
321
322 <tr>
323 <td>Android 1.5 platform</td>
324
325 <td>r4 (or higher)</td>
326 </tr>
327
328 <tr>
329 <td>ADT Plugin</td>
330
331 <td>0.9.7 (or higher)</td>
332 </tr>
333 </table>
334
335 <p>You can download the tools and platforms using the <em>Android SDK and AVD Manager</em>, as
336 described in <a href="{@docRoot}sdk/adding-components.html">Adding SDK Components</a>.</p>
337
338 <h3 id="considerations">Development considerations</h3>
339
340 <p>As you develop your library project and dependent applications, keep the points listed below
341 in mind:</p>
342
343 <ul>
344 <li><p><strong>Resource conflicts</strong></p>
345 <p>Since the tools merge the resources of a library project with those of a dependent application
346 project, a given resource ID might be defined in both projects. In this case, the tools select
347 the resource from the application, or the library with highest priority, and discard the other
348 resource. As you develop your applications, be aware that common resource IDs are likely to be
349 defined in more than one project and will be merged, with the resource from the application or
350 highest-priority library taking precedence.</p>
351 </li>
352
353 <li><p><strong>Use prefixes to avoid resource conflicts</strong></p>
354
355 <p>To avoid resource conflicts for common resource IDs, consider using a prefix or other
356 consistent naming scheme that is unique to the project (or is unique across all projects).</p></li>
357
358 <li><p><strong>You cannot export a library project to a JAR file</strong></p>
359
360 <p>A library cannot be distributed as a binary file (such as a jar file). This is because the
361 library project is compiled by the main project to use the correct resource IDs.</p></li>
362 <li><p><strong>One library project cannot reference another</strong></p>
363
364 <p>A library cannot depend on another library</p></li>
365
366 <li><p><strong>A library project can include a JAR library</strong></p>
367
368 <p>You can develop a library project that itself includes a JAR library, however you need to
369 manually edit the dependent application project's build path and add a path to the JAR file.</p></li>
370
371 <li><p><strong>A library project can depend on an external JAR library</strong></p>
372
373 <p>You can develop a library project that depends on an external library (for example, the Maps
374 external library). In this case, the dependent application must build against a target that
375 includes the external library (for example, the Google APIs Add-On). Note also that both the
376 library project and the dependent application must declare the external library in their manifest
377 files, in a <a href=
378 "{@docRoot}guide/topics/manifest/uses-library-element.html"><code>&lt;uses-library&gt;</code></a>
379 element.</p></li>
380 <li><p><strong>Library project cannot include AIDL files</strong></p>
381
382 <p>The tools do not support the use of AIDL files in a library project. Any AIDL files used by an
383 application must be stored in the application project itself.</p></li>
384
385 <li> <p><strong>Library projects cannot include raw assets</strong></p>
386
387 <p>The tools do not support the use of raw asset files (saved in the <code>assets/</code> directory)
388 in a library project. Any asset resources
389 used by an application must be stored in the <code>assets/</code> directory of the application
390 project itself. However, resource files saved in the
391 <code>res/</code> directory are supported.</p></li>
392
393 <li><p><strong>Platform version must be lower than or equal to the Android project</strong></p>
394
395 <p>A library is compiled as part of the dependent application project, so the API used in the
396 library project must be compatible with the version of the Android library used to compile the
397 application project. In general, the library project should use an <a href=
398 "{@docRoot}guide/appendix/api-levels.html">API level</a> that is the same as &mdash; or lower
399 than &mdash; that used by the application. If the library project uses an API level that is
400 higher than that of the application, the application project will not compile. It is
401 perfectly acceptable to have a library that uses the Android 1.5 API (API level 3) and that is
402 used in an Android 1.6 (API level 4) or Android 2.1 (API level 7) project, for instance.</p></li>
403
404 <li> <p><strong>No restriction on library package names</strong></p>
405
406 <p>There is no requirement for the package name of a library to be the same as that of
407 applications that use it.</p></li>
408
409 <li><p><strong>Each library project creates its own R class </strong></p>
410
411 <p>When you build the dependent application project, library projects are compiled and
412 merged with the application project. Each library has its own <code>R</code> class, named according
413 to the library's package name. The <code>R</code> class generated from main
414 project and the library project is created in all the packages that are needed including the main
415 project's package and the libraries' packages.</p></li>
416
417 <li><p><strong>Library project storage location</strong></p>
418
419 <p>There are no specific requirements on where you should store a library project, relative to a
420 dependent application project, as long as the application project can reference the library
421 project by a relative link. What is important is that the main
422 project can reference the library project through a relative link.</p></li>
423 </ul>
424
Robert Lyc74a69a82011-01-04 22:48:02 -0800425
426
427 <h3 id="libraryMigrating">Migrating library projects to ADT 0.9.8 or higher</h3>
428
429<p>This section provides information about how to migrate a library project
430created with ADT 0.9.7 to ADT 0.9.8 or higher. The migration is needed only if
431you are developing in Eclipse with ADT and assumes that you have also upgraded
432to SDK Tools r7 (or higher). </p>
433
434<p>The way that ADT handles library projects has changed between
435ADT 0.9.7 and ADT 0.9.8. Specifically, in ADT 0.9.7, the <code>src/</code>
436source folder of the library was linked into the dependent application project
437as a folder that had the same name as the library project. This worked because
438of two restrictions on the library projects:</p>
439
440<ul>
441<li>The library was only able to contain a single source folder (excluding the
442special <code>gen/</code> source folder), and</li>
443<li>The source folder was required to have the name <code>src/</code> and be
444stored at the root of the project.</li>
445</ul>
446
447<p>In ADT 0.9.8, both of those restrictions were removed. A library project can
448have as many source folders as needed and each can have any name. Additionally,
449a library project can store source folders in any location of the project. For
450example, you could store sources in a <code>src/java/</code> directory. In order
451to support this, the name of the linked source folders in the main project are
452now called &lt;library-name&gt;_&lt;folder-name&gt; For
453example: <code>MyLibrary_src/</code> or <code>MyLibrary_src_java/</code>.</p>
454
455<p>Additionally, the linking process now flags those folders in order for ADT to
456recognize that it created them. This will allow ADT to automatically migrate the
457project to new versions of ADT, should they contain changes to the handling of
458library projects. ADT 0.9.7 did not flag the linked source folders, so ADT 0.9.8
459cannot be sure whether the old linked folders can be removed safely. After
460upgrading ADT to 0.9.8, you will need to remove the old linked folders manually
461in a simple two-step process, as described below.</p>
462
463<p>Before you begin, make sure to create a backup copy of your application or
464save the latest version to your code version control system. This ensures that
465you will be able to easily revert the migration changes in case there is a
466problem in your environment.</p>
467
468<p>When you first upgrade to ADT 0.9.8, your main project will look as shown
469in figure 1, with two linked folders (in this example, <code>MyLibrary</code> and
470<code>MyLibrary_src</code> &mdash; both of which link to
471<code>MyLibrary/src</code>. Eclipse shows an error on one of them because they
472are duplicate links to a single class.</p>
473
474<img src="/images/developing/lib-migration-0.png" alt="">
475<p class="img-caption"><strong>Figure 1.</strong> Library project migration error</p>
476<p>To fix the error, remove the linked folder that <em>does not</em> contain the
477<code>_src</code> suffix. </p>
478
479<ol>
480<li>Right click the folder that you want to remove (in this case, the
481<code>MyLibrary</code> folder) and choose <strong>Build Path</strong> &gt;
482<strong>Remove from Build Path</strong>, as shown in figure 2.</li>
483
484<img src="/images/developing/lib-migration-1.png" style="height:600px"
485alt="">
486<p class="img-caption"><strong>Figure 2.</strong> Remove from Build Path menu item</p>
487
488<li>Next, when asked about unlinking the folder from the project, select
489<strong>Yes</strong>, as shown in figure 3.</li>
490
491<img src="/images/developing/lib-migration-2.png" alt="">
492
493<p class="img-caption"><strong>Figure 3.</strong> Unlink folder confirmation window</p>
494</ol>
495
496<p>This should resolve the error and migrate your library project to the new
497ADT environment. </p>
498
499
Robert Lybf6c3382010-12-22 11:05:10 -0800500
501 <h2 id="TestProjects">Test Projects</h2>
502
503 <p>Test projects contain Android applications that you write using the
504 <a href="{@docRoot}guide/topics/testing/index.html">Testing and
505 Instrumentation framework</a>. The framework is an extension of the JUnit test framework and adds
506 access to Android system objects. The file structure of a test project is the same as an
507 Android project.</p>
508
509 <dl>
510 <dt><code>src/</code></dt>
511
512 <dd>Includes your test source files. Test projects do not require an Activity <code>.java</code>
513 file, but can include one.</dd>
514
515 <dt><code>gen/</code></dt>
516
517 <dd>This contains the Java files generated by ADT, such as your <code>R.java</code> file and
518 interfaces created from AIDL files.</dd>
519
520 <dt><code>assets/</code></dt>
521
522 <dd>This is empty. You can use it to store raw asset files.</dd>
523
524 <dt><code>res/</code></dt>
525
526 <dd>A folder for your application resources, such as drawable files, layout files, string
527 values, etc. See <a href="{@docRoot}guide/topics/resources/index.html">Application
528 Resources</a>.</dd>
529
530 <dt><code>AndroidManifest.xml</code></dt>
531
532 <dd>The Android Manifest for your project. See <a href=
533 "{@docRoot}guide/topics/manifest/manifest-intro.html">The AndroidManifest.xml File</a>. Test
534 Projects have a special <a href=
535 "{@docRoot}guide/topics/manifest/instrumentation-element.html">
536 <code>&lt;instrumentation&gt;</code></a>
537 element that connects the test project with the application project.</dd>
538
539 <dt><code>build.properties</code></dt>
540
541 <dd>Customizable properties for the build system. You can edit this file to override default
542 build settings used by Ant and provide a pointer to your keystore and key alias so that the
543 build tools can sign your application when built in release mode.</dd>
544
545 <dt><code>build.xml</code></dt>
546
547 <dd>The Ant build file for your project.</dd>
548
549 <dt><code>default.properties</code></dt>
550
551 <dd>This file contains project settings, such as the build target. This files is integral to
552 the project, as such, it should be maintained in a Source Revision Control system. It should
553 never be edited manually &mdash; to edit project properties, right-click the project folder and
554 select "Properties".</dd>
555 </dl>For more information, see the <a href=
556 "{@docRoot}guide/developing/testing/index.html">Testing</a> section.
557
558
559 <h2>Testing a library project</h2>
560
561 <p>There are two recommended ways of setting up testing on code and resources in a library
562 project:</p>
563
564 <ul>
565 <li>You can set up a <a href="{@docRoot}guide/developing/testing/testing_otheride.html">test
566 project</a> that instruments an application project that depends on the library project. You
567 can then add tests to the project for library-specific features.</li>
568
569 <li>You can set up a set up a standard application project that depends on the library and put
570 the instrumentation in that project. This lets you create a self-contained project that
571 contains both the tests/instrumentations and the code to test.</li>
572 </ul>