| Scott Main | 50e990c | 2012-06-21 17:14:39 -0700 | [diff] [blame] | 1 | page.title=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 | |
| 11 | <li><a href="#LibraryProjects">Library Projects</a> |
| 12 | <ol> |
| 13 | <li><a href="#considerations">Development considerations</a></li> |
| 14 | </ol> |
| 15 | </li> |
| 16 | |
| 17 | <li><a href="#TestProjects">Test Projects</a></li> |
| 18 | |
| 19 | <li><a href="#testing">Testing a Library Project</a></li> |
| 20 | </ol> |
| 21 | </div> |
| 22 | </div> |
| 23 | |
| 24 | <p>Projects act as containers for storing things such as code and resource files. The SDK tools |
| 25 | expect your projects to follow a specific structure so it can compile and package your |
| 26 | application correctly, so it is highly recommended that you create them with Eclipse and ADT or |
| 27 | with the <code>android</code> tool on the command line. There are three types of projects, and |
| 28 | they all share the same general structure but differ in function:</p> |
| 29 | |
| 30 | <dl> |
| 31 | <dt><strong>Android Projects</strong></dt> |
| 32 | |
| 33 | <dd>An Android project is the container for your application's source code, resource files, and |
| 34 | files such as the Ant build and Android Manifest file. An application project is the main type |
| 35 | of project and the contents are eventually built into an <code>.apk</code> file that you install on a |
| 36 | device.</dd> |
| 37 | |
| 38 | <dt><strong>Test Projects</strong></dt> |
| 39 | |
| 40 | <dd>These projects contain code to test your application projects and are built into |
| 41 | applications that run on a device.</dd> |
| 42 | |
| 43 | <dt><strong>Library Projects</strong></dt> |
| 44 | |
| 45 | <dd>These projects contain shareable Android source code and resources that you can reference |
| 46 | in Android projects. This is useful when you have common code that you want to reuse. |
| 47 | Library projects cannot be installed onto a device, however, they are |
| 48 | pulled into the <code>.apk</code> file at build time.</dd> |
| 49 | </dl> |
| 50 | |
| 51 | <p>When you use the Android development tools to create a new project, the essential files and |
| 52 | folders will be created for you. There are only a handful of files and folders generated for you, |
| 53 | and some of them depend on whether you use the Eclipse plugin or the {@code android} tool to |
| 54 | generate your project. As your application grows in complexity, you might require new kinds of |
| 55 | resources, directories, and files.</p> |
| 56 | |
| 57 | <h2 id="ApplicationProjects">Android Projects</h2> |
| 58 | |
| 59 | <p>Android projects are the projects that eventually get built into an <code>.apk</code> file that you install |
| 60 | onto a device. They contain things such as application source code and resource files. |
| 61 | Some are generated for you by default, while others should be created if |
| 62 | required. The following directories and files comprise an Android project:</p> |
| 63 | |
| 64 | <dl> |
| 65 | <dt><code>src/</code></dt> |
| 66 | |
| 67 | <dd>Contains your stub Activity file, which is stored at |
| 68 | <code>src<em>/your/package/namespace/ActivityName</em>.java</code>. All other source code |
| 69 | files (such as <code>.java</code> or <code>.aidl</code> files) go here as well.</dd> |
| 70 | |
| 71 | <dt><code>bin</code></dt> |
| 72 | |
| 73 | <dd>Output directory of the build. This is where you can find the final <code>.apk</code> file and other |
| 74 | compiled resources.</dd> |
| 75 | |
| 76 | <dt><code>jni</code></dt> |
| 77 | |
| 78 | <dd>Contains native code sources developed using the Android NDK. For more information, see the |
| 79 | <a href="{@docRoot}tools/sdk/ndk/index.html">Android NDK documentation</a>.</dd> |
| 80 | |
| 81 | <dt><code>gen/</code></dt> |
| 82 | |
| 83 | <dd>Contains the Java files generated by ADT, such as your <code>R.java</code> file and |
| 84 | interfaces created from AIDL files.</dd> |
| 85 | |
| 86 | <dt><code>assets/</code></dt> |
| 87 | |
| 88 | <dd>This is empty. You can use it to store raw asset files. Files that you save here are |
| 89 | compiled into an <code>.apk</code> file as-is, and the original filename is preserved. You can navigate this |
| 90 | directory in the same way as a typical file system using URIs and read files as a stream of |
| 91 | bytes using the the {@link android.content.res.AssetManager}. For example, this is a good |
| 92 | location for textures and game data.</dd> |
| 93 | |
| 94 | <dt><code>res/</code></dt> |
| 95 | |
| 96 | <dd> |
| 97 | Contains application resources, such as drawable files, layout files, and string values. See |
| 98 | <a href="{@docRoot}guide/topics/resources/index.html">Application Resources</a> for more |
| 99 | information. |
| 100 | |
| 101 | <dl> |
| 102 | <dt><code>anim/</code></dt> |
| 103 | |
| 104 | <dd>For XML files that are compiled into animation objects. See the <a href= |
| 105 | "{@docRoot}guide/topics/resources/animation-resource.html">Animation</a> resource |
| 106 | type.</dd> |
| 107 | |
| 108 | <dt><code>color/</code></dt> |
| 109 | |
| 110 | <dd>For XML files that describe colors. See the <a href= |
| 111 | "{@docRoot}guide/topics/resources/color-list-resource.html">Color Values</a> resource |
| 112 | type.</dd> |
| 113 | |
| 114 | <dt><code>drawable/</code></dt> |
| 115 | |
| 116 | <dd>For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe |
| 117 | Drawable shapes or a Drawable objects that contain multiple states (normal, pressed, or |
| 118 | focused). See the <a href= |
| 119 | "{@docRoot}guide/topics/resources/drawable-resource.html">Drawable</a> resource type.</dd> |
| 120 | |
| 121 | <dt><code>layout/</code></dt> |
| 122 | |
| 123 | <dd>XML files that are compiled into screen layouts (or part of a screen). See the <a href= |
| 124 | "{@docRoot}guide/topics/resources/layout-resource.html">Layout</a> resource type.</dd> |
| 125 | |
| 126 | <dt><code>menu/</code></dt> |
| 127 | |
| 128 | <dd>For XML files that define application menus. |
| 129 | See the <a href="{@docRoot}guide/topics/resources/menu-resource.html">Menus</a> |
| 130 | resource type.</dd> |
| 131 | |
| 132 | <dt><code>raw/</code></dt> |
| 133 | |
| 134 | <dd>For arbitrary raw asset files. Saving asset files here instead of in the |
| 135 | <code>assets/</code> directory only differs in the way that you access them. These files |
| 136 | are processed by aapt and must be referenced from the application using a resource |
| 137 | identifier in the {@code R} class. For example, this is a good place for media, such as MP3 |
| 138 | or Ogg files.</dd> |
| 139 | |
| 140 | <dt><code>values/</code></dt> |
| 141 | |
| 142 | <dd>For XML files that are compiled into many kinds of resource. Unlike other resources in |
| 143 | the <code>res/</code> directory, resources written to XML files in this folder are not |
| 144 | referenced by the file name. Instead, the XML element type controls how the resources is |
| 145 | defined within them are placed into the {@code R} class.</dd> |
| 146 | |
| 147 | <dt><code>xml/</code></dt> |
| 148 | |
| 149 | <dd>For miscellaneous XML files that configure application components. For example, an XML |
| 150 | file that defines a {@link android.preference.PreferenceScreen}, {@link |
| 151 | android.appwidget.AppWidgetProviderInfo}, or <a href= |
| 152 | "{@docRoot}reference/android/app/SearchManager.html#SearchabilityMetadata">Searchability |
| 153 | Metadata</a>. See <a href="{@docRoot}guide/topics/resources/index.html">Application Resources</a> |
| 154 | for more information about configuring these application components.</dd> |
| 155 | </dl> |
| 156 | </dd> |
| 157 | |
| 158 | <dt><code>libs/</code></dt> |
| 159 | |
| 160 | <dd>Contains private libraries.</dd> |
| 161 | |
| 162 | <dt><code>AndroidManifest.xml</code></dt> |
| 163 | |
| 164 | <dd>The control file that describes the nature of the application and each of its components. |
| 165 | For instance, it describes: certain qualities about the activities, services, intent receivers, |
| 166 | and content providers; what permissions are requested; what external libraries are needed; what |
| 167 | device features are required, what API Levels are supported or required; and others. See the |
| 168 | <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a> |
| 169 | documentation for more information</dd> |
| 170 | |
| 171 | <dt><code>project.properties</code></dt> |
| 172 | |
| 173 | <dd>This file contains project settings, such as the build target. This file is integral to |
| 174 | the project, so maintain it in a source revision control system. To edit project |
| 175 | properties in Eclipse, right-click the project folder and select |
| 176 | <strong>Properties</strong>.</dd> |
| 177 | |
| 178 | <dt><code>local.properties</code></dt> |
| 179 | |
| 180 | <dd>Customizable computer-specific properties for the build system. If you use Ant to build |
| 181 | the project, this contains the path to the SDK installation. Because the content of the file |
| 182 | is specific to the local installation of the SDK, the <code>local.properties</code> should not |
| 183 | be maintained in a source revision control system. If you use Eclipse, this file is not |
| 184 | used.</dd> |
| 185 | |
| 186 | <dt><code>ant.properties</code></dt> |
| 187 | |
| 188 | <dd>Customizable properties for the build system. You can edit this file to override default |
| 189 | build settings used by Ant and also provide the location of your keystore and key alias so that |
| 190 | the build tools can sign your application when building in release mode. This file is integral |
| 191 | to the project, so maintain it in a source revision control system. If you use Eclipse, this |
| 192 | file is not used.</dd> |
| 193 | |
| 194 | <dt><code>build.xml</code></dt> |
| 195 | |
| 196 | <dd>The Ant build file for your project. This is only applicable for projects that |
| 197 | you build with Ant.</dd> |
| 198 | |
| 199 | </dl> |
| 200 | |
| 201 | <h2 id="LibraryProjects">Library Projects</h2> |
| 202 | |
| 203 | <div class="sidebox-wrapper"> |
| 204 | <div class="sidebox"> |
| 205 | <h2>Library project example code</h2> |
| 206 | |
| 207 | <p>The SDK includes an example application called <code>TicTacToeMain</code> that shows how a dependent |
| 208 | application can use code and resources from an Android Library project. The TicTacToeMain |
| 209 | application uses code and resources from an example library project called TicTacToeLib.</p> |
| 210 | |
| 211 | <p>To download the sample applications and run them as projects in |
| 212 | your environment, use the <em>Android SDK Manager</em> to download the "Samples for |
| 213 | SDK API 8" (or later) package into your SDK.</p> |
| 214 | |
| 215 | <p>For more information and to browse the code of the samples, see |
| 216 | the <a href="{@docRoot}resources/samples/TicTacToeMain/index.html">TicTacToeMain |
| 217 | application</a>.</p> |
| 218 | </div> |
| 219 | </div> |
| 220 | |
| 221 | <p>An Android <em>library project</em> is a development project that holds shared Android |
| 222 | source code and resources. Other Android application projects can reference the library project |
| 223 | and, at build time, include its compiled sources in their <code>.apk</code> files. Multiple |
| 224 | application projects can reference the same library project and any single application project |
| 225 | can reference multiple library projects.</p> |
| 226 | |
| 227 | <p class="note"><strong>Note:</strong> You need SDK Tools r14 or newer to use the new library |
| 228 | project feature that generates each library project into its own JAR file. |
| 229 | You can download the tools and platforms using the |
| 230 | <em>Android SDK Manager</em>, as described in |
| 231 | <a href="{@docRoot}sdk/exploring.html">Exploring the SDK</a>.</p> |
| 232 | |
| 233 | <p>If you have source code and resources that are common to multiple Android projects, you |
| 234 | can move them to a library project so that it is easier to maintain across applications and |
| 235 | versions. Here are some common scenarios in which you could make use of library projects:</p> |
| 236 | |
| 237 | <ul> |
| 238 | <li>If you are developing multiple related applications that use some of the same components, |
| 239 | you move the redundant components out of their respective application projects and create a |
| 240 | single, reuseable set of the same components in a library project.</li> |
| 241 | |
| 242 | <li>If you are creating an application that exists in both free and paid versions. You move |
| 243 | the part of the application that is common to both versions into a library project. The two |
| 244 | dependent projects, with their different package names, will reference the library project |
| 245 | and provide only the difference between the two application versions.</li> |
| 246 | </ul> |
| 247 | |
| 248 | <p>Structurally, a library project is similar to a standard Android application project. For |
| 249 | example, it includes a manifest file at the project root, as well as <code>src/</code>, |
| 250 | <code>res/</code> and similar directories. The project can contain the same types of source |
| 251 | code and resources as a standard Android project, stored in the same way. For example, source |
| 252 | code in the library project can access its own resources through its <code>R</code> class.</p> |
| 253 | |
| 254 | <p>However, a library project differs from an standard Android application project in that you |
| 255 | cannot compile it directly to its own <code>.apk</code> and run it on an Android device. |
| 256 | Similarly, you cannot export the library project to a self-contained JAR file, as you would do |
| 257 | for a true library. Instead, you must compile the library indirectly, by referencing the |
| 258 | library in the dependent application and building that application.</p> |
| 259 | |
| 260 | <p>When you build an application that depends on a library project, the SDK tools compile the |
| 261 | library into a temporary JAR file and uses it in the main project, then uses the |
| 262 | result to generate the <code>.apk</code>. In cases where a resource ID is defined in both the |
| 263 | application and the library, the tools ensure that the resource declared in the application gets |
| 264 | priority and that the resource in the library project is not compiled into the application |
| 265 | <code>.apk</code>. This gives your application the flexibility to either use or redefine any |
| 266 | resource behaviors or values that are defined in any library.</p> |
| 267 | |
| 268 | <p>To organize your code further, your application can add references to multiple library |
| 269 | projects, then specify the relative priority of the resources in each library. This lets you |
| 270 | build up the resources actually used in your application in a cumulative manner. When two |
| 271 | libraries referenced from an application define the same resource ID, the tools select the |
| 272 | resource from the library with higher priority and discard the other.</p> |
| 273 | |
| 274 | <p>Once you have added references to library projects to your Android project, |
| 275 | you can set their relative priority. At build time, the |
| 276 | libraries are merged with the application one at a time, starting from the lowest priority to |
| 277 | the highest.</p> |
| 278 | |
| 279 | <p>Library projects can reference other library projects and can import an external library |
| 280 | (JAR) in the normal way.</p> |
| 281 | |
| 282 | <h3 id="considerations">Development considerations</h3> |
| 283 | |
| 284 | <p>As you develop your library project and dependent applications, keep the points listed below |
| 285 | in mind:</p> |
| 286 | |
| 287 | <ul> |
| 288 | <li><p><strong>Resource conflicts</strong></p> |
| 289 | <p>Since the tools merge the resources of a library project with those of a dependent application |
| 290 | project, a given resource ID might be defined in both projects. In this case, the tools select |
| 291 | the resource from the application, or the library with highest priority, and discard the other |
| 292 | resource. As you develop your applications, be aware that common resource IDs are likely to be |
| 293 | defined in more than one project and will be merged, with the resource from the application or |
| 294 | highest-priority library taking precedence.</p> |
| 295 | </li> |
| 296 | |
| 297 | <li><p><strong>Use prefixes to avoid resource conflicts</strong></p> |
| 298 | |
| 299 | <p>To avoid resource conflicts for common resource IDs, consider using a prefix or other |
| 300 | consistent naming scheme that is unique to the project (or is unique across all projects).</p></li> |
| 301 | |
| 302 | <li><p><strong>You cannot export a library project to a JAR file</strong></p> |
| 303 | |
| 304 | <p>A library cannot be distributed as a binary file (such as a JAR file). This will |
| 305 | be added in a future |
| 306 | version of the SDK Tools.</p></li> |
| 307 | |
| 308 | <li><p><strong>A library project can include a JAR library</strong></p> |
| 309 | |
| 310 | <p>You can develop a library project that itself includes a JAR library, however you need to |
| 311 | manually edit the dependent application project's build path and add a path to the JAR file.</p></li> |
| 312 | |
| 313 | <li><p><strong>A library project can depend on an external JAR library</strong></p> |
| 314 | |
| 315 | <p>You can develop a library project that depends on an external library (for example, the Maps |
| 316 | external library). In this case, the dependent application must build against a target that |
| 317 | includes the external library (for example, the Google APIs Add-On). Note also that both the |
| 318 | library project and the dependent application must declare the external library in their manifest |
| 319 | files, in a <a href= |
| 320 | "{@docRoot}guide/topics/manifest/uses-library-element.html"><code><uses-library></code></a> |
| 321 | element.</p></li> |
| 322 | |
| 323 | <li> <p><strong>Library projects cannot include raw assets</strong></p> |
| 324 | |
| 325 | <p>The tools do not support the use of raw asset files (saved in the <code>assets/</code> directory) |
| 326 | in a library project. Any asset resources |
| 327 | used by an application must be stored in the <code>assets/</code> directory of the application |
| 328 | project itself. However, resource files saved in the |
| 329 | <code>res/</code> directory are supported.</p></li> |
| 330 | |
| 331 | <li><p><strong>Platform version must be lower than or equal to the Android project</strong></p> |
| 332 | |
| 333 | <p>A library is compiled as part of the dependent application project, so the API used in the |
| 334 | library project must be compatible with the version of the Android library used to compile the |
| 335 | application project. In general, the library project should use an <a href= |
| 336 | "{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels">API level</a> that is the same as — or lower |
| 337 | than — that used by the application. If the library project uses an API level that is |
| 338 | higher than that of the application, the application project will not compile. It is |
| 339 | perfectly acceptable to have a library that uses the Android 1.5 API (API level 3) and that is |
| 340 | used in an Android 1.6 (API level 4) or Android 2.1 (API level 7) project, for instance.</p></li> |
| 341 | |
| 342 | <li> <p><strong>No restriction on library package names</strong></p> |
| 343 | |
| 344 | <p>There is no requirement for the package name of a library to be the same as that of |
| 345 | applications that use it.</p></li> |
| 346 | |
| 347 | <li><p><strong>Each library project creates its own R class </strong></p> |
| 348 | |
| 349 | <p>When you build the dependent application project, library projects are compiled and |
| 350 | merged with the application project. Each library has its own <code>R</code> class, named according |
| 351 | to the library's package name. The <code>R</code> class generated from main |
| 352 | project and the library project is created in all the packages that are needed including the main |
| 353 | project's package and the libraries' packages.</p></li> |
| 354 | |
| 355 | <li><p><strong>Library project storage location</strong></p> |
| 356 | |
| 357 | <p>There are no specific requirements on where you should store a library project, relative to a |
| 358 | dependent application project, as long as the application project can reference the library |
| 359 | project by a relative link. What is important is that the main |
| 360 | project can reference the library project through a relative link.</p></li> |
| 361 | </ul> |
| 362 | |
| 363 | <h2 id="TestProjects">Test Projects</h2> |
| 364 | |
| 365 | <p>Test projects contain Android applications that you write using the |
| 366 | <a href="{@docRoot}tools/testing/index.html">Testing and |
| 367 | Instrumentation framework</a>. The framework is an extension of the JUnit test framework and adds |
| 368 | access to Android system objects. The file structure of a test project is the same as an |
| 369 | Android project.</p> |
| 370 | |
| 371 | <dl> |
| 372 | <dt><code>src/</code></dt> |
| 373 | |
| 374 | <dd>Includes your test source files. Test projects do not require an Activity <code>.java</code> |
| 375 | file, but can include one.</dd> |
| 376 | |
| 377 | <dt><code>gen/</code></dt> |
| 378 | |
| 379 | <dd>This contains the Java files generated by ADT, such as your <code>R.java</code> file and |
| 380 | interfaces created from AIDL files.</dd> |
| 381 | |
| 382 | <dt><code>assets/</code></dt> |
| 383 | |
| 384 | <dd>This is empty. You can use it to store raw asset files.</dd> |
| 385 | |
| 386 | <dt><code>res/</code></dt> |
| 387 | |
| 388 | <dd>A folder for your application resources, such as drawable files, layout files, string |
| 389 | values, etc. See <a href="{@docRoot}guide/topics/resources/index.html">Application |
| 390 | Resources</a>.</dd> |
| 391 | |
| 392 | <dt><code>AndroidManifest.xml</code></dt> |
| 393 | |
| 394 | <dd>The Android Manifest for your project. See <a href= |
| 395 | "{@docRoot}guide/topics/manifest/manifest-intro.html">The AndroidManifest.xml File</a>. Test |
| 396 | Projects have a special <a href= |
| 397 | "{@docRoot}guide/topics/manifest/instrumentation-element.html"> |
| 398 | <code><instrumentation></code></a> |
| 399 | element that connects the test project with the application project.</dd> |
| 400 | |
| 401 | <dt><code>project.properties</code></dt> |
| 402 | |
| 403 | <dd>This file contains project settings, such as the build target and links to the project being |
| 404 | tested. This file is integral to the project, so maintain it in a source |
| 405 | revision control system. To edit project properties in Eclipse, right-click the project folder |
| 406 | and select <strong>Properties</strong>.</dd> |
| 407 | |
| 408 | <dt><code>local.properties</code></dt> |
| 409 | |
| 410 | <dd>Customizable computer-specific properties for the build system. If you use Ant to build |
| 411 | the project, this contains the path to the SDK installation. Because the content of the file |
| 412 | is specific to the local installation of the SDK, it should not be maintained in a Source |
| 413 | Revision Control system. If you use Eclipse, this file is not used.</dd> |
| 414 | |
| 415 | <dt><code>ant.properties</code></dt> |
| 416 | |
| 417 | <dd>Customizable properties for the build system. You can edit this file to override default |
| 418 | build settings used by Ant and provide the location to your keystore and key alias, so that the |
| 419 | build tools can sign your application when building in release mode. This file is integral to |
| 420 | the project, so maintain it in a source revision control system. |
| 421 | If you use Eclipse, this file is not used.</dd> |
| 422 | |
| 423 | <dt><code>build.xml</code></dt> |
| 424 | |
| 425 | <dd>The Ant build file for your project. This is only applicable for projects that |
| 426 | you build with Ant.</dd> |
| 427 | </dl> |
| 428 | |
| 429 | <p>For more information, see the <a href= |
| 430 | "{@docRoot}tools/testing/index.html">Testing</a> section.</p> |
| 431 | |
| 432 | |
| 433 | <h2 id="testing">Testing a Library Project</h2> |
| 434 | |
| 435 | <p>There are two recommended ways of setting up testing on code and resources in a library |
| 436 | project:</p> |
| 437 | |
| 438 | <ul> |
| 439 | <li>You can set up a <a href="{@docRoot}tools/testing/testing_otheride.html">test |
| 440 | project</a> that instruments an application project that depends on the library project. You |
| 441 | can then add tests to the project for library-specific features.</li> |
| 442 | |
| 443 | <li>You can set up a standard application project that depends on the library and put |
| 444 | the instrumentation in that project. This lets you create a self-contained project that |
| 445 | contains both the tests/instrumentations and the code to test.</li> |
| 446 | </ul> |