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