1page.title=Managing Projects from Android Studio 2 3@jd:body 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 <h2>In this document</h2> 8 9 <ol> 10 <li><a href="#CreatingAProject">Creating an Android Project</a></li> 11 <ol> 12 <li><a href="#Step1CreatingAProject">Create a New Project</a> </li> 13 <li><a href="#Step2SelectFormFactor">Select Form Factors and API Level</a> </li> 14 <li><a href="#Step3AddActivity">Add an Activity</a> </li> 15 <li><a href="#Step4ConfigureActivity">Configure Your App</a> </li> 16 <li><a href="#Step5DevelopYourApp">Develop Your App</a> </li> 17 </ol> 18 19 <li><a href="#CreatingAModule">Creating an Android Module</a></li> 20 21 <li><a href="#SettingUpLibraryModule">Setting up a Library Module</a></li> 22 23 <li><a href="#ReferencingLibraryModule">Referencing a Library Module</a></li> 24 25 <li><a href="#ReferencingAppEngModule">Setting up an App Eng Module</a></li> 26 27 <li><a href="#ProjectView">Using the Android Project View</a></li> 28 29 </ol> 30 31 </div> 32 </div> 33 34 35<p>Android Studio provides graphical tools for creating and managing Android projects, which 36contain everything that define your Android apps, from app source code to build configurations and 37test code. Each project contains one or more different types of modules, such as 38 application modules, library modules, and test modules.</p> 39 40<p>This guide explains how to create Android projects and different modules using 41<a href="{@docRoot}tools/studio/index.html">Android Studio</a>. 42For more information about the Android project structure and module types, read <a href= 43"{@docRoot}tools/projects/index.html">Managing Projects Overview</a>.</p> 44 45 46 47<h2 id="CreatingAProject">Creating an Android Project</h2> 48 49<p>Android Studio makes it easy to create Android apps for several form factors, such as phone, 50tablet, TV, Wear, and Google Glass. The <em>New Project</em> wizard lets you choose the form factors 51for your app and populates the project structure with everything you need to get started.</p> 52 53<p>Follow the steps in this section to create a project in Android Studio.</p> 54 55<h3 id="Step1CreatingAProject">Step 1: Create a New Project</h2> 56 57<p>If you didn't have a project opened, Android Studio shows the Welcome screen. 58To create a new project, click <strong>New Project</strong>.</p> 59 60<p>If you had a project opened, Android Studio shows the development environment. 61To create a new project, click <strong>File</strong> > <strong>New Project</strong>.</p> 62 63<p>The next window lets you configure the name of your app, the package name, and the location 64of your project.</p> 65 66<img src="{@docRoot}images/tools/wizard2.png" alt="" width="500" height="381"> 67<p class="img-caption"><strong>Figure 1.</strong> Choose a name for your project.</p> 68 69<p>Enter the values for your project then click <strong>Next</strong>.</p> 70 71<h3 id="Step2SelectFormFactor">Step 2: Select Form Factors and API Level</h2> 72 73<p>The next window lets you select the form factors supported by your app, such as phone, tablet, 74TV, Wear, and Google Glass. The selected form factors become the application modules witin the 75project. For each form factor, you can also select the API Level for that app. To get more information, 76click <strong>Help me choose</strong>.</p> 77 78<img src="{@docRoot}images/tools/wizard4.png" alt="" width="750" height="510"> 79<p class="img-caption"><strong>Figure 2.</strong> Select the API Level.</p> 80 81<p>The API Level window shows the distribution of mobile devices running each version of Android, 82as shown in figure 3. Click on an API level to see a list of features introduced in the corresponding 83version of Android. This helps you choose the minimum API Level that has all the features that 84your apps needs, so you can reach as many devices as possible. Then click <strong>OK</strong>.</p> 85 86<img src="{@docRoot}images/tools/wizard3.png" alt="" width="500" height="480"> 87<p class="img-caption"><strong>Figure 3.</strong> Choose form factors for your app.</p> 88 89<p>Then, on the Form Factors Window, click <strong>Next</strong>.</p> 90 91 92<h3 id="Step3AddActivity">Step 3: Add an Activity</h2> 93 94<p>The next screen lets you select an activity type to add to your app, as shown in figure 4. 95This screen displays a different set of activities for each of the form factors you selected earlier.</p> 96 97<img src="{@docRoot}images/tools/wizard5.png" alt="" width="720" height="504"> 98<p class="img-caption"><strong>Figure 4.</strong> Add an activity to your app.</p> 99 100<p>Choose an activity type then click <strong>Next</strong>.</p> 101 102 <p class="note"><strong>Note:</strong> If you choose "Add No Activity", click <strong>Finish</strong> 103 to create the project.</p> 104 105 106<h3 id="Step4ConfigureActivity">Step 4: Configure Your Activity</h2> 107 108<p>The next screen lets you configure the activity to add to your app, as shown in figure 5.</p> 109 110<img src="{@docRoot}images/tools/wizard6.png" alt="" width="450" height="385"> 111<p class="img-caption"><strong>Figure 5.</strong> Choose a name for your activity.</p> 112 113<p>Enter the activity name, the layout name, and the activity title. Then click 114<strong>Finish</strong>.</p> 115 116 117<h3 id="Step5DevelopYourApp">Step 5: Develop Your App</h2> 118 119<p>Android Studio creates the default structure for your project and opens the development 120environment. If your app supports more than one form factor, Android Studio creates a module folder 121with complete source files for each of them as shown in figure 6.</p> 122 123<img src="{@docRoot}images/tools/wizard7.png" alt="" width="750" height="509"> 124<p class="img-caption"><strong>Figure 6.</strong> The default project structure for a mobile app.</p> 125 126<p>Now you are ready to develop your app. For more information, see the following links:</p> 127 128<ul> 129<li><a href="{@docRoot}training/">Training Lessons</a></li> 130<li><a href="{@docRoot}training/building-wearables.html">Building Apps for Wearables</a></li> 131<li><a href="{@docRoot}tv/">Android TV</a></li> 132<li><a href="https://developers.google.com/glass/">Google Glass</a></li> 133</ul> 134 135 136 <h2 id="CreatingAModule">Creating an Android Module</h2> 137 138 <p>Android application modules contain the <code>src/main/</code>, <code>AndroidManifest.xml</code>, 139 <code>build.gradle</code>, build output and other files you need to generate your app's APK files. 140 Android Studio provides a <em>New Module Wizard</em> that you can use to quickly create a new 141 Android module (or a module from existing code) based on selected application settings, such as 142 minimum SDK level and activity template.</p> 143 144 <p>To create a new module, select <strong>File</strong> > <strong>New</strong> > 145 <strong>Module</strong>. Select the desire module type then click Next to enter the basic module 146 settings:</p> 147 148 <ul> 149 <li>Enter an <strong>Application Name</strong>. This name is used as the title of your 150 application launcher icon when it is installed on a device.</li> 151 152 <li>Enter a <strong>Module Name</strong>. This text is used as the name of the folder where 153 your Java-based activity files are stored.</li> 154 155 <li>Enter a <strong>Package Name</strong> and <strong>Package Location</strong>. This class 156 package namespace creates the initial 157 package structure for your applications code files and is added as the 158 <a href="{@docRoot}guide/topics/manifest/manifest-element.html#package">{@code package}</a> 159 attribute in your application's 160 <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">Android manifest file</a>. 161 This manifest value serves as the unique identifier for your application app when you 162 distribute it to users. The package name must follow the same rules as packages in the Java 163 programming language.</li> 164 165 <li>Select the <strong>Minimum required SDK</strong>. 166 This setting indicates the lowest version of the Android platform that your application 167 supports for the selected form factor. This value sets the 168 <code>minSdkVersion</code> attribute in the build.gradle file.</li> 169 170 <p class="note"><strong>Note:</strong> You can manually change the minimum and target SDK 171 for your module at any time: Double-click the module's build.gradle in the Project Explorer, 172 set the <strong>targetSdkVersion</strong> and <em>targetSdkVersion</em> in the 173 <em>defaultConfig</em> section.</p> 174 175 176 <li>Select a <strong>Target SDK</strong>. This setting indicates the highest version of 177 Android with which you have tested with your application and sets the 178 <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code 179 targetSdkVersion}</a> attribute in your application's' build.gradle file. 180 181 182 <li>Select a <strong>Compile With</strong> API version. This setting specifies what version 183 of the SDK to compile your project against. We strongly recommend using the most recent 184 version of the API.</li> 185 186 <li>Select a <strong>Language Level</strong> API version. This setting specifies what version 187 of the SDK to compile your project against. We strongly recommend using the most recent 188 version of the API.</li> 189 190 <li>Select a <strong>Theme</strong>. This setting specifies which standard Android 191 <a href="{@docRoot}design/style/themes.html">visual style</a> is applied to your 192 application. Select activity template. For more information about Android code templates, see 193 <a href="{@docRoot}tools/projects/templates.html">Using Code Templates</a>Leave the <strong> 194 Create activity</strong> option checked so you can start your 195 application with some essential components. </li> 196 197 <li>Click the check box for the required Support Libraries then click <strong>Next</strong>.</li> 198 199 <li>In the <strong>Configure Launcher Icon</strong> page, create an icon and options, then click 200 <strong>Next</strong>.</li> 201 202 <li>In the <strong>Create Activity</strong> page, select activity template then click 203 <strong>Next</strong>. For more information about Android code templates, see 204 <a href="{@docRoot}tools/projects/templates.html">Using Code Templates</a>. 205 </li> 206 207 <li>Review the new module settings then click <strong>Finish</strong>.</li> 208 209 </ul> 210 211 <p>The wizard creates a new Android application module according to the options you have chosen.</p> 212 213 214 215 <h2 id="SettingUpLibraryModule">Setting up a Library Module</h2> 216 217 <p>A library module is a standard Android module, so you can create a new one in the same way 218 as you would a new application module, using the New Module wizard and selecting <em>Android 219 Library</em> as the module type. The created library module will appear in your project view 220 along with the other modules. </p> 221 222 <p> You can easily change an existing application module to a library module by changing the 223 plugin assignment in the <strong>build.gradle</strong> file to <em>com.android.libary</em>.</p> 224 225<pre> 226apply plugin: 'com.android.application' 227 228android {...} 229</pre> 230 231<pre> 232apply plugin: 'com.android.library' 233 234android {...} 235</pre> 236 237 238 <h3>Adding a dependency on a library module</h3> 239 240 <p>The library dependency can be declared in the module's manifest file or in the 241 <strong<build.gradle</strong> file. </p> 242 243 <p>A library modules's manifest file must declare all of the shared components that it includes, 244 just as would a standard Android application. For more information, see the documentation for 245 <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p> 246 247 <p>For example, the <a href= 248 "{@docRoot}resources/samples/TicTacToeLib/AndroidManifest.html">TicTacToeLib</a> example library 249 project declares the activity <code>GameActivity</code>:</p> 250 <pre> 251<manifest> 252 ... 253 <application> 254 ... 255 <activity android:name="GameActivity" /> 256 ... 257 </application> 258</manifest> 259</pre> 260 261 262<p>To add the dependency declaration to the build file, edit the build file for the <code>app</code> 263module (<code>app/build.gradle</code>) and add a dependency on the <code>lib</code> module:</p> 264 265<pre> 266... 267dependencies { 268 ... 269 compile project(":lib") 270} 271</pre> 272 273<p>In this example, the <code>lib</code> module can still be built and tested independently, and 274the build system creates an AAR package for it that you could reuse in other projects.</p> 275 276<p class="note"><strong>Note:</strong> The library settings in the <code>app/build.gradle</code> 277file will override any shared library resources declared in the manifest file.</p> 278 279 280 <h2 id="ReferencingLibraryModule">Referencing a library module</h2> 281 282 <p>If you are developing an application and want to include the shared code or resources from a 283 library module, you can also do so easily by adding a reference to the library module in the 284 module's dependency page.</p> 285 286 <p>To add a reference to a library module, follow these steps:</p> 287 288 <ol> 289 <li>Make sure that both the module library and the application module that depends on it are 290 in your proejct. If one of the modules is missing, import it into your project.</li> 291 292 <li>In the project view, right-click the dependent module and select 293 <strong>Open</strong> > <strong>Module Settings</strong>.</li> 294 295 <li>Right-click the plus icon to add a new dependencies. 296 <p>If you are adding references to multiple libraries, you can set their relative 297 priority (and merge order) by selecting a library and using the <strong>Up</strong> and 298 <strong>Down</strong> controls. The tools merge the referenced libraries with your application 299 starting from lowest priority (bottom of the list) to highest (top of the list). If more than one 300 library defines the same resource ID, the tools select the resource from the library with higher 301 priority. The application itself has highest priority and its resources are always used in 302 preference to identical resource IDs defined in libraries.</p> 303 </li> 304 305 <li>Use the <strong>Scope</strong> drop-down to select how the dependency will be applied.</li> 306 307 <li>Click <strong>Apply</strong> to create the dependency and <strong>OK</strong> to close the 308 <strong>Project Structure</strong> window.</li> 309 </ol> 310 311 <p>Android Studio rebuilds the module, including the contents of the library module the next time 312 the project or module is built.</p> 313 314 315 316 <h3>Declaring library components in the manifest file</h3> 317 318 <p>In the manifest file of the application module, you must add declarations of all components 319 that the application will use that are imported from a library module. For example, you must 320 declare any <code><activity></code>, <code><service></code>, 321 <code><receiver></code>, <code><provider></code>, and so on, as well as 322 <code><permission></code>, <code><uses-library></code>, and similar elements.</p> 323 324 <p>Declarations should reference the library components by their fully-qualified package names, 325 where appropriate.</p> 326 327 <p>For example, the <a href= 328 "{@docRoot}resources/samples/TicTacToeMain/AndroidManifest.html">TicTacToeMain</a> example 329 application declares the library activity <code>GameActivity</code> like this:</p> 330 <pre> 331<manifest> 332 ... 333 <application> 334 ... 335 <activity android:name="com.example.android.tictactoe.library.GameActivity" /> 336 ... 337 </application> 338</manifest> 339</pre> 340 341 <p>For more information about the manifest file, see the documentation for <a href= 342 "{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p> 343 344 345 <h2 id="ProjectView">Using the Android Project View</h2> 346 347 348<p>The Android project view in Android Studio shows a flattened version of your project's structure 349that provides quick access to the key source files of Android projects and helps you work with 350the new <a href="{@docRoot}sdk/installing/studio-build.html">Gradle-based build system</a>. The 351Android project view:</p> 352 353<ul> 354<li>Groups the build files for all modules at the top level of the project hierarchy.</li> 355<li>Shows the most important source directories at the top level of the module hierarchy.</li> 356<li>Groups all the manifest files for each module.</li> 357<li>Shows resource files from all Gradle source sets.</li> 358<li>Groups resource files for different locales, orientations, and screen types in a single group 359per resource type.</li> 360</ul> 361 362<div style="float:right;margin-left:30px;width:240px"> 363<img src="{@docRoot}images/tools/projectview01.png" alt="" width="220" height="264"/> 364<p class="img-caption"><strong>Figure 9:</strong> Show the Android project view.</p> 365</div> 366 367 368<h2 id="enable-view">Enable and use the Android Project View</h2> 369 370<p>The Android project view is not yet enabled by default. To show the Android project view, 371click <strong>Project</strong> and select <strong>Android</strong>, as shown in figure 9.</p> 372 373<p>The Android project view shows all the build files at the top level of the project hierarchy 374under <strong>Gradle Scripts</strong>. Each project module appears as a folder at the top 375level of the project hierarchy and contains these three elements at the top level:</p> 376 377<ul> 378<li><code>java/</code> - Source files for the module.</li> 379<li><code>manifests/</code> - Manifest files for the module.</li> 380<li><code>res/</code> - Resource files for the module.</li> 381</ul> 382 383<p>Figure 10 shows how the Android project view groups all the instances of the 384<code>ic_launcher.png</code> resource for different screen densities under the same element.</p> 385 386<p class="note"><strong>Note:</strong> The Android project view shows a hierarchy that helps you 387work with Android projects by providing a flattened structure that highlights the most commonly 388used files while developing Android applications. However, the project structure on disk differs 389from this representation.</p> 390 391<img src="{@docRoot}images/tools/projectview03.png" alt="" 392 style="margin-top:10px" width="650" height="508"/> 393<p class="img-caption"><strong>Figure 10:</strong> The traditional project view (left) and the 394Android project view (right).</p> 395 396 397 398 399