Home | History | Annotate | Download | only in projects
      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}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, maintained it in a source
    183     revision control system. If you use Eclipse, this file is not used.</dd>
    184 
    185     <dt><code>ant.properties</code></dt>
    186 
    187     <dd>Customizable properties for the build system. You can edit this file to override default
    188     build settings used by Ant and also provide the location of your keystore and key alias so that
    189     the build tools can sign your application when building in release mode. This file is integral
    190     to the project, so maintain it in a source revision control system. If you use Eclipse, this
    191     file is not used.</dd>
    192 
    193     <dt><code>build.xml</code></dt>
    194 
    195     <dd>The Ant build file for your project. This is only applicable for projects that
    196     you build with Ant.</dd>
    197 
    198   </dl>
    199 
    200   <h2 id="LibraryProjects">Library Projects</h2>
    201 
    202   <div class="sidebox-wrapper">
    203     <div class="sidebox">
    204       <h2>Library project example code</h2>
    205 
    206       <p>The SDK includes an example application called <code>TicTacToeMain</code> that shows how a dependent
    207       application can use code and resources from an Android Library project. The TicTacToeMain
    208       application uses code and resources from an example library project called TicTacToeLib.</p>
    209 
    210       <p>To download the sample applications and run them as projects in
    211       your environment, use the <em>Android SDK and AVD Manager</em> to download the "Samples for
    212       SDK API 8" (or later) component into your SDK.</p>
    213 
    214       <p>For more information and to browse the code of the samples, see
    215       the <a href="{@docRoot}resources/samples/TicTacToeMain/index.html">TicTacToeMain
    216       application</a>.</p>
    217     </div>
    218   </div>
    219 
    220     <p>An Android <em>library project</em> is a development project that holds shared Android
    221     source code and resources. Other Android application projects can reference the library project
    222     and, at build time, include its compiled sources in their <code>.apk</code> files. Multiple
    223     application projects can reference the same library project and any single application project
    224     can reference multiple library projects.</p>
    225 
    226     <p class="note"><strong>Note:</strong> You need SDK Tools r14 or newer to use the new library
    227     project feature that generates each library project into its own JAR file.
    228     You can download the tools and platforms using the
    229     <em>Android SDK and AVD Manager</em>, as described in
    230     <a href="{@docRoot}sdk/adding-components.html">Adding SDK Components</a>.</p>
    231 
    232     <p>If you have source code and resources that are common to multiple Android projects, you
    233     can move them to a library project so that it is easier to maintain across applications and
    234     versions. Here are some common scenarios in which you could make use of library projects:</p>
    235 
    236     <ul>
    237       <li>If you are developing multiple related applications that use some of the same components,
    238       you move the redundant components out of their respective application projects and create a
    239       single, reuseable set of the same components in a library project.</li>
    240 
    241       <li>If you are creating an application that exists in both free and paid versions. You move
    242       the part of the application that is common to both versions into a library project. The two
    243       dependent projects, with their different package names, will reference the library project
    244       and provide only the difference between the two application versions.</li>
    245     </ul>
    246 
    247     <p>Structurally, a library project is similar to a standard Android application project. For
    248     example, it includes a manifest file at the project root, as well as <code>src/</code>,
    249     <code>res/</code> and similar directories. The project can contain the same types of source
    250     code and resources as a standard Android project, stored in the same way. For example, source
    251     code in the library project can access its own resources through its <code>R</code> class.</p>
    252 
    253     <p>However, a library project differs from an standard Android application project in that you
    254     cannot compile it directly to its own <code>.apk</code> and run it on an Android device.
    255     Similarly, you cannot export the library project to a self-contained JAR file, as you would do
    256     for a true library. Instead, you must compile the library indirectly, by referencing the
    257     library in the dependent application and building that application.</p>
    258 
    259     <p>When you build an application that depends on a library project, the SDK tools compile the
    260     library into a temporary JAR file and uses it in the main project, then uses the
    261     result to generate the <code>.apk</code>. In cases where a resource ID is defined in both the
    262     application and the library, the tools ensure that the resource declared in the application gets
    263     priority and that the resource in the library project is not compiled into the application
    264     <code>.apk</code>. This gives your application the flexibility to either use or redefine any
    265     resource behaviors or values that are defined in any library.</p>
    266 
    267     <p>To organize your code further, your application can add references to multiple library
    268     projects, then specify the relative priority of the resources in each library. This lets you
    269     build up the resources actually used in your application in a cumulative manner. When two
    270     libraries referenced from an application define the same resource ID, the tools select the
    271     resource from the library with higher priority and discard the other.</p>
    272 
    273     <p>Once you have added references to library projects to your Android project,
    274     you can set their relative priority. At build time, the
    275     libraries are merged with the application one at a time, starting from the lowest priority to
    276     the highest.</p>
    277 
    278     <p>Library projects can reference other library projects and can import an external library
    279     (JAR) in the  normal way.</p>
    280 
    281   <h3 id="considerations">Development considerations</h3>
    282 
    283   <p>As you develop your library project and dependent applications, keep the points listed below
    284   in mind:</p>
    285 
    286   <ul>
    287   <li><p><strong>Resource conflicts</strong></p>
    288   <p>Since the tools merge the resources of a library project with those of a dependent application
    289   project, a given resource ID might be defined in both projects. In this case, the tools select
    290   the resource from the application, or the library with highest priority, and discard the other
    291   resource. As you develop your applications, be aware that common resource IDs are likely to be
    292   defined in more than one project and will be merged, with the resource from the application or
    293   highest-priority library taking precedence.</p>
    294   </li>
    295 
    296   <li><p><strong>Use prefixes to avoid resource conflicts</strong></p>
    297 
    298   <p>To avoid resource conflicts for common resource IDs, consider using a prefix or other
    299   consistent naming scheme that is unique to the project (or is unique across all projects).</p></li>
    300 
    301   <li><p><strong>You cannot export a library project to a JAR file</strong></p>
    302 
    303   <p>A library cannot be distributed as a binary file (such as a JAR file). This will
    304 be added in a future
    305   version of the SDK Tools.</p></li>
    306 
    307   <li><p><strong>A library project can include a JAR library</strong></p>
    308 
    309   <p>You can develop a library project that itself includes a JAR library, however you need to
    310   manually edit the dependent application project's build path and add a path to the JAR file.</p></li>
    311 
    312   <li><p><strong>A library project can depend on an external JAR library</strong></p>
    313 
    314   <p>You can develop a library project that depends on an external library (for example, the Maps
    315   external library). In this case, the dependent application must build against a target that
    316   includes the external library (for example, the Google APIs Add-On). Note also that both the
    317   library project and the dependent application must declare the external library in their manifest
    318   files, in a <a href=
    319   "{@docRoot}guide/topics/manifest/uses-library-element.html"><code>&lt;uses-library&gt;</code></a>
    320   element.</p></li>
    321 
    322   <li> <p><strong>Library projects cannot include raw assets</strong></p>
    323 
    324   <p>The tools do not support the use of raw asset files (saved in the <code>assets/</code> directory)
    325   in a library project. Any asset resources
    326   used by an application must be stored in the <code>assets/</code> directory of the application
    327   project itself. However, resource files saved in the
    328   <code>res/</code> directory are supported.</p></li>
    329 
    330   <li><p><strong>Platform version must be lower than or equal to the Android project</strong></p>
    331 
    332   <p>A library is compiled as part of the dependent application project, so the API used in the
    333   library project must be compatible with the version of the Android library used to compile the
    334   application project. In general, the library project should use an <a href=
    335   "{@docRoot}guide/appendix/api-levels.html">API level</a> that is the same as &mdash; or lower
    336   than &mdash; that used by the application. If the library project uses an API level that is
    337   higher than that of the application, the application project will not compile. It is
    338   perfectly acceptable to have a library that uses the Android 1.5 API (API level 3) and that is
    339   used in an Android 1.6 (API level 4) or Android 2.1 (API level 7) project, for instance.</p></li>
    340 
    341   <li> <p><strong>No restriction on library package names</strong></p>
    342 
    343   <p>There is no requirement for the package name of a library to be the same as that of
    344   applications that use it.</p></li>
    345 
    346   <li><p><strong>Each library project creates its own R class </strong></p>
    347 
    348   <p>When you build the dependent application project, library projects are compiled and
    349   merged with the application project. Each library has its own <code>R</code> class, named according
    350   to the library's package name. The <code>R</code> class generated from main
    351   project and the library project is created in all the packages that are needed including the main
    352   project's package and the libraries' packages.</p></li>
    353 
    354   <li><p><strong>Library project storage location</strong></p>
    355 
    356   <p>There are no specific requirements on where you should store a library project, relative to a
    357   dependent application project, as long as the application project can reference the library
    358   project by a relative link. What is important is that the main
    359   project can reference the library project through a relative link.</p></li>
    360   </ul>
    361 
    362   <h2 id="TestProjects">Test Projects</h2>
    363 
    364   <p>Test projects contain Android applications that you write using the
    365   <a href="{@docRoot}guide/topics/testing/index.html">Testing and
    366   Instrumentation framework</a>. The framework is an extension of the JUnit test framework and adds
    367   access to Android system objects. The file structure of a test project is the same as an
    368   Android project.</p>
    369 
    370   <dl>
    371     <dt><code>src/</code></dt>
    372 
    373     <dd>Includes your test source files. Test projects do not require an Activity <code>.java</code>
    374     file, but can include one.</dd>
    375 
    376     <dt><code>gen/</code></dt>
    377 
    378     <dd>This contains the Java files generated by ADT, such as your <code>R.java</code> file and
    379     interfaces created from AIDL files.</dd>
    380 
    381     <dt><code>assets/</code></dt>
    382 
    383     <dd>This is empty. You can use it to store raw asset files.</dd>
    384 
    385     <dt><code>res/</code></dt>
    386 
    387     <dd>A folder for your application resources, such as drawable files, layout files, string
    388     values, etc. See <a href="{@docRoot}guide/topics/resources/index.html">Application
    389     Resources</a>.</dd>
    390 
    391     <dt><code>AndroidManifest.xml</code></dt>
    392 
    393     <dd>The Android Manifest for your project. See <a href=
    394     "{@docRoot}guide/topics/manifest/manifest-intro.html">The AndroidManifest.xml File</a>. Test
    395     Projects have a special <a href=
    396     "{@docRoot}guide/topics/manifest/instrumentation-element.html">
    397     <code>&lt;instrumentation&gt;</code></a>
    398     element that connects the test project with the application project.</dd>
    399 
    400     <dt><code>project.properties</code></dt>
    401 
    402     <dd>This file contains project settings, such as the build target and links to the project being
    403 tested. This file is integral to the project, so maintain it in a source
    404 revision control system. To edit project properties in Eclipse, right-click the project folder
    405 and select <strong>Properties</strong>.</dd>
    406 
    407     <dt><code>local.properties</code></dt>
    408 
    409     <dd>Customizable computer-specific properties for the build system. If you use Ant to build
    410     the project, this contains the path to the SDK installation. Because the content of the file
    411     is specific to the local installation of the SDK, it should not be maintained in a Source
    412     Revision Control system. If you use Eclipse, this file is not used.</dd>
    413 
    414     <dt><code>ant.properties</code></dt>
    415 
    416     <dd>Customizable properties for the build system. You can edit this file to override default
    417     build settings used by Ant and provide the location to your keystore and key alias, so that the
    418     build tools can sign your application when building in release mode. This file is integral to
    419     the project, so maintain it in a source revision control system.
    420     If you use Eclipse, this file is not used.</dd>
    421 
    422     <dt><code>build.xml</code></dt>
    423 
    424     <dd>The Ant build file for your project. This is only applicable for projects that
    425     you build with Ant.</dd>
    426   </dl>
    427 
    428   <p>For more information, see the <a href=
    429   "{@docRoot}guide/developing/testing/index.html">Testing</a> section.</p>
    430 
    431 
    432   <h2 id="testing">Testing a Library Project</h2>
    433 
    434   <p>There are two recommended ways of setting up testing on code and resources in a library
    435   project:</p>
    436 
    437   <ul>
    438     <li>You can set up a <a href="{@docRoot}guide/developing/testing/testing_otheride.html">test
    439     project</a> that instruments an application project that depends on the library project. You
    440     can then add tests to the project for library-specific features.</li>
    441 
    442     <li>You can set up a set up a standard application project that depends on the library and put
    443     the instrumentation in that project. This lets you create a self-contained project that
    444     contains both the tests/instrumentations and the code to test.</li>
    445   </ul>