Home | History | Annotate | Download | only in projects
      1 page.title=Managing Projects Overview
      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="#ProjectFiles">Android Project Files</a></li>
     10         <li><a href="#ApplicationModules">Android Application Modules</a></li>
     11 
     12         <li><a href="#LibraryModules">Library Modules</a>
     13           <ol>
     14             <li><a href="#considerations">Development considerations</a></li>
     15           </ol>
     16         </li>
     17 
     18         <li><a href="#TestModules">Test Modules</a></li>
     19 
     20         <li><a href="#testing">Testing a Library Module</a></li>
     21       </ol>
     22     </div>
     23   </div>
     24 
     25   <p>An Android <em>project</em> contains everything that defines your Android app, from app
     26   source code to build configurations and test code. The SDK tools require that your projects
     27   follow a specific structure so it can compile and package your application correctly.
     28   If you're using Android Studio, it takes care of all this for you.</p>
     29 
     30   <p>A <em>module</em> is the first level of containment within a project that encapsulates
     31   specific types of source code files and resources. There are several types of modules
     32   with a project:</p>
     33 
     34   <dl>
     35 
     36     <dt><strong>Android Application Modules</strong></dt>
     37 
     38     <dd>An Android Application Module is the container for your application's source code, resource
     39     files, and application level settings, such as the module-level build file, resource  files, and
     40     Android Manifest file. The application module contents are eventually
     41     built into the <code>.apk</code> file that gets installed on a device.</dd>
     42 
     43     <dt><strong>Test Modules</strong></dt>
     44 
     45     <dd>These modules contain code to test your application projects and are built into
     46     test applications that run on a device. By default, Android Studio creates the
     47     <em>androidTest</em> module for inserting JUnit tests. </dd>
     48 
     49     <dt><strong>Library Modules</strong></dt>
     50 
     51     <dd>These modules contain shareable Android source code and resources that you can reference
     52     in Android projects. This is useful when you have common code that you want to reuse.
     53     Library modules cannot be installed onto a device, however, they are
     54     pulled into the <code>.apk</code> file at build time.</dd>
     55 
     56 
     57     <dt><strong>App Engine Modules</strong></dt>
     58 
     59     <dd>Android Studio lets you easily add a cloud backend to your application. A backend allows you
     60     to implement functionality such as backing up user data to the cloud, serving content to client
     61     apps, real-time interactions, sending push notifications through Google Cloud Messaging for
     62     Android (GCM), and more. App Engine modules are App Engine java Servlet Module for backend
     63     development, App Engine java Endpoints Module to convert server-side Java code annotations into
     64     RESTful backend APIs, and App Engine Backend with Google Cloud Messaging to send push notifications
     65     from your server to your Android devices. </dd>
     66 
     67   </dl>
     68 
     69   <p>When you use the Android development tools to create a new project and the module, the essential files
     70   and folders will be created for you. There are only a handful of files and folders generated for you,
     71   and some of them depend on whether you use Android Studio or the {@code android} tool to
     72   generate your module. As your application grows in complexity, you might require new kinds of
     73   resources, directories, and files.</p>
     74 
     75 <p class="note"><strong>Note:</strong> Project folders and files apply across the entire Android
     76 project and override similar module file settings.</p>
     77 
     78 
     79 
     80 
     81   <h2 id="ProjectFiles">Android Project Files</h2>
     82 
     83   <p>Android Studio project files and settings provide project-wide settings that apply across all
     84   modules in the project.  </p>
     85 
     86 	<dl>
     87    <dt><code>.idea</code></dt>
     88 
     89    <dd>Directory for IntelliJ IDEA settings.</dd>
     90 
     91 
     92    <dt><code>app</code></dt>
     93 
     94    <dd>Application module directories and files. </dd>
     95 
     96 
     97    <dt><code>build</code></dt>
     98 
     99    <dd>This directory stories the build output for all project modules.</dd>
    100 
    101 
    102     <dt><code>gradle</code></dt>
    103 
    104     <dd>Contains the gradler-wrapper files. </dd>
    105 
    106 
    107     <dt><code>.gitignore</code></dt>
    108 
    109     <dd>Specifies the untracked files that Git should ignore.</dd>
    110 
    111 
    112     <dt><code>build.gradle</code></dt>
    113 
    114     <dd>Customizable properties for the build system. You can edit this file to specify the default
    115     build settings used by the application modules and also set the location of your keystore and key alias
    116     so that the build tools can sign your application when building in release mode. This file is
    117     integral to the project, so maintain it in a source revision control system. </dd>
    118 
    119     <dt><code>gradle.properties</code></dt>
    120 
    121     <dd>Project-wide Gradle settings.</dd>
    122 
    123 
    124     <dt><code>gradlew</code></dt>
    125 
    126     <dd>Gradle startup script for Unix.</dd>
    127 
    128 
    129     <dt><code>gradlew.bat</code></dt>
    130 
    131     <dd>Gradle startup script for Windows. </dd>
    132 
    133    <dt><code>local.properties</code></dt>
    134 
    135    <dd>Customizable computer-specific properties for the build system, such as the path to the SDK
    136    installation. Because the content of the file is specific to the local installation of the SDK,
    137    the <code>local.properties</code> should not be maintained in a source revision control system. </dd>
    138 
    139 
    140     <dt><code><project>.iml</code></dt>
    141 
    142     <dd>Module file created by the IntelliJ IDEA to store module information.</dd>
    143 
    144     <dt><code>settings.gradle</code></dt>
    145 
    146     <dd>Specifies the sub-projects to build.</dd>
    147 
    148   </dl>
    149 
    150 
    151   <h2 id="ApplicationModules">Android Application Modules</h2>
    152 
    153   <p>Android Application Modules are the modules that eventually get built into the <code>.apk</code>
    154   files based on your build settings. They contain things such as application source code and resource
    155   files. Most code and resource files are generated for you by default, while others should be created if
    156   required. The following directories and files comprise an Android application module:</p>
    157 
    158   <dl>
    159 
    160     <dt><code>build/</code></dt>
    161 
    162     <dd>Contains build folders for the specified build variants. Stored in the main application module.</dd>
    163 
    164 
    165     <dt><code>libs/</code></dt>
    166 
    167     <dd>Contains private libraries. Stored in the main application module.</dd>
    168 
    169 
    170 
    171 
    172     <dt><code>src/</code></dt>
    173 
    174     <dd>Contains your stub Activity file, which is stored at
    175     <code>src<em>/main/java/<namespace.applicationname>/ActivityName></em>.java</code>. All other source
    176     code files (such as <code>.java</code> or <code>.aidl</code> files) go here as well.</dd>
    177 
    178      <dl>
    179        <dt><code>androidTest/</code></dt>
    180 
    181        <dd>Contains the instrumentation tests. For more information, see the
    182        <a href="{@docRoot}tools/testing/index.html">Android Test documentation</a>.</dd>
    183 
    184        <dt><code>main/java/com.&gt;project&lt;.&gt;app&lt;</code></dt>
    185 
    186        <dd>Contains Java code source for the app activities.</dd>
    187 
    188        <dt><code>main/jni/</code></dt>
    189 
    190        <dd>Contains native code using the Java Native Interface (JNI). For more information, see the
    191        <a href="{@docRoot}tools/sdk/ndk/index.html">Android NDK documentation</a>.</dd>
    192 
    193        <dt><code>main/gen/</code></dt>
    194 
    195        <dd>Contains the Java files generated by Android Studio, such as your <code>R.java</code> file and
    196        interfaces created from AIDL files.</dd>
    197 
    198        <dt><code>main/assets/</code></dt>
    199 
    200        <dd>This is empty. You can use it to store raw asset files. Files that you save here are
    201        compiled into an <code>.apk</code> file as-is, and the original filename is preserved. You can
    202        navigate this directory in the same way as a typical file system using URIs and read files as a
    203        stream of bytes using the {@link android.content.res.AssetManager}. For example, this is a good
    204        location for textures and game data.</dd>
    205 
    206        <dt><code>main/res/</code></dt>
    207 
    208        <dd>Contains application resources, such as drawable files, layout files, and string values
    209        in the following directories. See
    210        <a href="{@docRoot}guide/topics/resources/index.html">Application Resources</a> for more
    211        information.
    212 
    213        <dl>
    214            <dt><code>anim/</code></dt>
    215 
    216            <dd>For XML files that are compiled into animation objects. See the <a href=
    217            "{@docRoot}guide/topics/resources/animation-resource.html">Animation</a> resource
    218            type.</dd>
    219 
    220            <dt><code>color/</code></dt>
    221 
    222            <dd>For XML files that describe colors. See the <a href=
    223         "  {@docRoot}guide/topics/resources/color-list-resource.html">Color Values</a> resource
    224            type.</dd>
    225 
    226            <dt><code>drawable/</code></dt>
    227 
    228            <dd>For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe
    229            Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or
    230            focused). See the <a href=
    231            "{@docRoot}guide/topics/resources/drawable-resource.html">Drawable</a> resource type.</dd>
    232 
    233            <dt><code>layout/</code></dt>
    234 
    235            <dd>XML files that are compiled into screen layouts (or part of a screen). See the <a href=
    236            "{@docRoot}guide/topics/resources/layout-resource.html">Layout</a> resource type.</dd>
    237 
    238            <dt><code>menu/</code></dt>
    239 
    240            <dd>For XML files that define application menus.
    241            See the <a href="{@docRoot}guide/topics/resources/menu-resource.html">Menus</a>
    242            resource type.</dd>
    243 
    244            <dt><code>raw/</code></dt>
    245 
    246            <dd>For arbitrary raw asset files. Saving asset files here is essentially the same as
    247            saving them in the <code>assets/</code> directory. The only difference is how you
    248            access them. These files
    249            are processed by aapt and must be referenced from the application using a resource
    250            identifier in the {@code R} class. For example, this is a good place for media, such as MP3
    251            or Ogg files.</dd>
    252 
    253            <dt><code>values/</code></dt>
    254 
    255            <dd>For XML files that define resources by XML element type. Unlike other resources in
    256            the <code>res/</code> directory, resources written to XML files in this folder are not
    257            referenced by the file name. Instead, the XML element type controls how the resources
    258            defined within the XML files are placed into the {@code R} class.</dd>
    259 
    260            <dt><code>xml/</code></dt>
    261 
    262            <dd>For miscellaneous XML files that configure application components. For example, an XML
    263            file that defines a {@link android.preference.PreferenceScreen}, {@link
    264            android.appwidget.AppWidgetProviderInfo}, or
    265            <a href="{@docRoot}reference/android/app/SearchManager.html#SearchabilityMetadata">
    266            Searchability Metadata</a>. See
    267            <a href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>
    268            for more information about configuring these application components.</dd>
    269 
    270          </dl>
    271 
    272       <dt><code>AndroidManifest.xml</code></dt>
    273 
    274       <dd>The control file that describes the nature of the application and each of its components.
    275       For instance, it describes: certain qualities about the activities, services, intent receivers,
    276       and content providers; what permissions are requested; what external libraries are needed; what
    277       device features are required, what API Levels are supported or required; and others. See the
    278       <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>
    279       documentation for more information</dd>
    280 
    281    </dl>
    282 
    283    <dt><code>.gitignore/</code></dt>
    284 
    285    <dd>Specifies the untracked files ignored by git.</dd>
    286 
    287    <dt><code>app.iml/</code></dt>
    288 
    289    <dd>IntelliJ IDEA module</dd>
    290 
    291    <dt><code>build.gradle</code></dt>
    292 
    293    <dd>Customizable properties for the build system. You can edit this file to override default
    294     build settings used by the manifest file and also set the location of your keystore and key alias
    295     so that the build tools can sign your application when building in release mode. This file is
    296     integral to the project, so maintain it in a source revision control system. </dd>
    297 
    298     <dt><code>proguard-rules.pro</code></dt>
    299 
    300     <dd>ProGuard settings file. </dd>
    301 
    302 
    303   </dl>
    304 
    305 
    306 
    307   <h2 id="LibraryModules">Library Module</h2>
    308 
    309   <div class="sidebox-wrapper">
    310     <div class="sidebox">
    311       <h2>Library module example code</h2>
    312 
    313       <p>The SDK includes an example application called <code>TicTacToeMain</code> that shows how a
    314       dependent application can use code and resources from an Android Library module. The TicTacToeMain
    315       application uses code and resources from an example library module called TicTacToeLib.</p>
    316 
    317       <p>To download the sample applications and run them as modules in
    318       your environment, use the <em>Android SDK Manager</em> to download the "Samples for
    319       SDK API 8" (or later) module into your SDK.</p>
    320 
    321       <p>For more information and to browse the code of the samples, see
    322       the <a href="{@docRoot}resources/samples/TicTacToeMain/index.html">TicTacToeMain
    323       application</a>.</p>
    324     </div>
    325   </div>
    326 
    327     <p>An Android <em>library module</em> is a development module that holds shared Android
    328     source code and resources. Other Android application modules can reference the library module
    329     and, at build time, include its compiled sources in their <code>.apk</code> files. Multiple
    330     application modules can reference the same library module and any single application module
    331     can reference multiple library modules.</p>
    332 
    333     <p class="note"><strong>Note:</strong> You need SDK Tools r14 or newer to use the new library
    334     module feature that generates each library module into its own JAR file.
    335     You can download the tools and platforms using the
    336     <em>Android SDK Manager</em>, as described in
    337     <a href="{@docRoot}tools/help/sdk-manager.html">SDK tools help</a>.</p>
    338 
    339     <p>If you have source code and resources that are common to multiple Android projects, you
    340     can move them to a library module so that it is easier to maintain across applications and
    341     versions. Here are some common scenarios in which you could make use of library modules:</p>
    342 
    343     <ul>
    344       <li>If you are developing multiple related applications that use some of the same components,
    345       you move the redundant components out of their respective application module and create a
    346       single, reusable set of the same components in a library module.</li>
    347 
    348       <li>If you are creating an application that exists in both free and paid versions. You move
    349       the part of the application that is common to both versions into a library module. The two
    350       dependent modules, with their different package names, will reference the library module
    351       and provide only the difference between the two application versions.</li>
    352     </ul>
    353 
    354     <p>Structurally, a library module is similar to a standard Android application module. For
    355     example, it includes a manifest file at the module root, as well as <code>src/</code>,
    356     <code>res/</code> and similar directories. The module can contain the same types of source
    357     code and resources as a standard Android module, stored in the same way. For example, source
    358     code in the library module can access its own resources through its <code>R</code> class.</p>
    359 
    360     <p>However, a library module differs from a standard Android application module in that you
    361     cannot compile it directly to its own <code>.apk</code> and run it on an Android device.
    362     Similarly, you cannot export the library module to a self-contained JAR file, as you would do
    363     for a true library. Instead, you must compile the library indirectly, by referencing the
    364     library in the dependent application and building that application.</p>
    365 
    366     <p>When you build an application that depends on a library module, the SDK tools compile the
    367     library into a temporary JAR file and use it in the main module, then uses the
    368     result to generate the <code>.apk</code>. In cases where a resource ID is defined in both the
    369     application and the library, the tools ensure that the resource declared in the application gets
    370     priority and that the resource in the library module is not compiled into the application
    371     <code>.apk</code>. This gives your application the flexibility to either use or redefine any
    372     resource behaviors or values that are defined in any library.</p>
    373 
    374     <p>To organize your code further, your application can add references to multiple library
    375     modules, then specify the relative priority of the resources in each library. This lets you
    376     build up the resources actually used in your application in a cumulative manner. When two
    377     libraries referenced from an application define the same resource ID, the tools select the
    378     resource from the library with higher priority and discard the other.</p>
    379 
    380     <p>Once you have added references to library modules to your Android application module,
    381     you can set their relative priority. At build time, the
    382     libraries are merged with the application one at a time, starting from the lowest priority to
    383     the highest.</p>
    384 
    385     <p>Library modules can reference other library modules and can import an external library
    386     (JAR) in the normal way.</p>
    387 
    388   <h3 id="considerations">Development considerations</h3>
    389 
    390   <p>As you develop your library modules and dependent applications, keep the points listed below
    391   in mind:</p>
    392 
    393   <ul>
    394   <li><p><strong>Resource conflicts</strong></p>
    395   <p>Since the tools merge the resources of a library module with those of a dependent application
    396   module, a given resource ID might be defined in both modules. In this case, the tools select
    397   the resource from the application, or the library with highest priority, and discard the other
    398   resource. As you develop your applications, be aware that common resource IDs are likely to be
    399   defined in more than one project and will be merged, with the resource from the application or
    400   highest-priority library taking precedence.</p>
    401   </li>
    402 
    403   <li><p><strong>Use prefixes to avoid resource conflicts</strong></p>
    404 
    405   <p>To avoid resource conflicts for common resource IDs, consider using a prefix or other
    406   consistent naming scheme that is unique to the module (or is unique across all project modules).</p></li>
    407 
    408   <li><p><strong>You cannot export a library module to a JAR file</strong></p>
    409 
    410   <p>A library cannot be distributed as a binary file (such as a JAR file). This will be added in a
    411   future version of the SDK Tools.</p></li>
    412 
    413   <li><p><strong>A library module can include a JAR library</strong></p>
    414 
    415   <p>You can develop a library module that itself includes a JAR library; however you need to
    416   manually edit the dependent application modules's build path and add a path to the JAR file.</p></li>
    417 
    418   <li><p><strong>A library module can depend on an external JAR library</strong></p>
    419 
    420   <p>You can develop a library module that depends on an external library (for example, the Maps
    421   external library). In this case, the dependent application must build against a target that
    422   includes the external library (for example, the Google APIs Add-On). Note also that both the
    423   library module and the dependent application must declare the external library in their manifest
    424   files, in a <a href=
    425   "{@docRoot}guide/topics/manifest/uses-library-element.html"><code>&lt;uses-library&gt;</code></a>
    426   element.</p></li>
    427 
    428   <li> <p><strong>Library modules cannot include raw assets</strong></p>
    429 
    430   <p>The tools do not support the use of raw asset files (saved in the <code>assets/</code> directory)
    431   in a library module. Any asset resources
    432   used by an application must be stored in the <code>assets/</code> directory of the application
    433   module itself. However, resource files saved in the <code>res/</code> directory are supported.</p></li>
    434 
    435   <li><p><strong>Platform version must be lower than or equal to the Android module</strong></p>
    436 
    437   <p>A library is compiled as part of the dependent application module, so the API used in the
    438   library module must be compatible with the version of the Android library used to compile the
    439   application module. In general, the library module should use an <a href=
    440   "{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels">API level</a> that is the same as &mdash; or lower
    441   than &mdash; that used by the application. If the library module uses an API level that is
    442   higher than that of the application, the application module will not compile. It is
    443   perfectly acceptable to have a library that uses the Android 1.5 API (API level 3) and that is
    444   used in an Android 1.6 (API level 4) or Android 2.1 (API level 7) module, for instance.</p></li>
    445 
    446   <li> <p><strong>No restriction on library module names</strong></p>
    447 
    448   <p>There is no requirement for the package name of a library to be the same as that of
    449   applications that use it.</p></li>
    450 
    451   <li><p><strong>Each library module creates its own R class </strong></p>
    452 
    453   <p>When you build the dependent application modules, library modules are compiled and
    454   merged with the application module. Each library has its own <code>R</code> class, named according
    455   to the library's package name. The <code>R</code> class generated from main
    456   module and the library module is created in all the packages that are needed including the main
    457   module's package and the libraries' packages.</p></li>
    458 
    459   <li><p><strong>Library module storage location</strong></p>
    460 
    461   <p>There are no specific requirements on where you should store a library module, relative to a
    462   dependent application module, as long as the application module can reference the library
    463   module by a relative link. What is important is that the main
    464   module can reference the library module through a relative link.</p></li>
    465   </ul>
    466 
    467   <h2 id="TestProjects">Test Projects</h2>
    468 
    469   <p>Test projects contain Android applications that you write using the
    470   <a href="{@docRoot}tools/testing/index.html">Testing and
    471   Instrumentation framework</a>. The framework is an extension of the JUnit test framework and adds
    472   access to Android system objects. </p>
    473 
    474   <p>The test projects are now automatically part of the app source folder. When a new application
    475   module is created, Android Studio creates the <code>src/androidTest</code> source set. This
    476   source set contains tests for the default configuration and is combined with the <em>debug</em>
    477   build type to generate a test application. </p>
    478 
    479   <img src="{@docRoot}images/tools/studio-androidtest-folder.png">
    480   <p class="img-caption"><strong>Figure 1.</strong> androidTest Folder.</p>
    481 
    482   <p class="note"><strong>Note:</strong> The <code>src/androidTest</code> source set may not be
    483   created for every type of available module template. If this source set is not created, you
    484   can just create it for that module.</p>
    485 
    486   <p>For each product flavor, create a test folder specific to that product flavor.  </p>
    487 
    488   <dl>
    489     <dt><code>src/main/</code></dt>
    490     <dd><code>src/androidTest/</code></dt>
    491 
    492     <dt><code>src/productFlavor1/</code></dt>
    493     <dd><code>src/testproductFlavor1/</code></dd>
    494 
    495     <dt><code>src/productFlavor2/</code></dt>
    496     <dd><code>src/testproductFlavor2/</code></dd>
    497 
    498   </dl>
    499 
    500   <p>The test manifests are always generated so a manifest in a test source set is optional.</p>
    501 
    502   <p>The test applications run against the <em>debug</em> build type.  This can be configured
    503   using the <code>testBuildType</code> property in the build file.</p>
    504 
    505 
    506   <p>For more information, see the
    507   <a href="{@docRoot}tools/testing/index.html">Testing</a> section.</p>
    508 
    509 
    510   <h2 id="testing">Testing a Library Module</h2>
    511 
    512   <p>There are two recommended ways of setting up testing on code and resources in a library
    513   module:</p>
    514 
    515   <ul>
    516     <li>You can set up a <a href="{@docRoot}tools/testing/testing_otheride.html">test
    517     module</a> that instruments an application module that depends on the library module. You
    518     can then add tests to the module for library-specific features.</li>
    519 
    520     <li>You can set up a standard application module that depends on the library and put
    521     the instrumentation in that module. This lets you create a self-contained module that
    522     contains both the tests/instrumentations and the code to test.</li>
    523   </ul>
    524