| page.title=Managing Projects with Android Studio |
| |
| @jd:body |
| |
| <div id="qv-wrapper"> |
| <div id="qv"> |
| <h2>In this document</h2> |
| |
| <ol> |
| <li><a href="#CreatingAProject">Creating an Android Project</a></li> |
| |
| <li><a href="#CreatingAModule">Creating an Android Module</a></li> |
| |
| <li><a href="#SettingUpLibraryModule">Setting up a Library Module</a></li> |
| |
| <li><a href="#ReferencingLibraryModule">Referencing a Library Module</a></li> |
| |
| <li><a href="#ReferencingAppEngModule">Setting up an App Eng Module</a></li> |
| |
| <li><a href="#ProjectView">Using the Android Project View</a></li> |
| |
| </ol> |
| |
| </div> |
| </div> |
| |
| |
| <p>Android Studio provides graphical tools for creating and managing Android projects, which |
| contain everything that define your Android apps, from app source code to build configurations and |
| test code. Each project contains one or more different types of modules, such as |
| application modules, library modules, and test modules.</p> |
| |
| <p>This guide explains how to create Android projects and different modules using |
| <a href="{@docRoot}tools/studio/index.html">Android Studio</a>. |
| For more information about the Android project structure and module types, read <a href= |
| "{@docRoot}tools/projects/index.html">Managing Projects Overview</a>.</p> |
| |
| |
| |
| <h2 id="CreatingAProject">Creating an Android Project</h2> |
| |
| <p>Android Studio makes it easy to create Android apps for several form factors, such as phone, |
| tablet, TV, Wear, and Google Glass. The <em>New Project</em> wizard lets you choose the form factors |
| for your app and populates the project structure with everything you need to get started.</p> |
| |
| <p>Follow the steps in this section to create a project in Android Studio.</p> |
| |
| <h3 id="Step1CreatingAProject">Step 1: Create a New Project</h2> |
| |
| <p>If you didn't have a project opened, Android Studio shows the Welcome screen. |
| To create a new project, click <strong>New Project</strong>.</p> |
| |
| <p>If you had a project opened, Android Studio shows the development environment. |
| To create a new project, click <strong>File</strong> > <strong>New Project</strong>.</p> |
| |
| <p>The next window lets you configure the name of your app, the package name, and the location |
| of your project.</p> |
| |
| <img src="{@docRoot}images/tools/wizard2.png" alt="" width="500" height="381"> |
| <p class="img-caption"><strong>Figure 1.</strong> Choose a name for your project.</p> |
| |
| <p>Enter the values for your project then click <strong>Next</strong>.</p> |
| |
| <h3 id="Step2SelectFormFactor">Step 2: Select Form Factors and API Level</h2> |
| |
| <p>The next window lets you select the form factors supported by your app, such as phone, tablet, |
| TV, Wear, and Google Glass. The selected form factors become the application modules within the |
| project. For each form factor, you can also select the API Level for that app. To get more information, |
| click <strong>Help me choose</strong>.</p> |
| |
| <img src="{@docRoot}images/tools/wizard4.png" alt="" width="750" height="510"> |
| <p class="img-caption"><strong>Figure 2.</strong> Select the API Level.</p> |
| |
| <p>The API Level window shows the distribution of mobile devices running each version of Android, |
| as shown in figure 3. Click on an API level to see a list of features introduced in the corresponding |
| version of Android. This helps you choose the minimum API Level that has all the features that |
| your apps needs, so you can reach as many devices as possible. Then click <strong>OK</strong>.</p> |
| |
| <img src="{@docRoot}images/tools/wizard3.png" alt="" width="500" height="480"> |
| <p class="img-caption"><strong>Figure 3.</strong> Choose form factors for your app.</p> |
| |
| <p>Then, on the Form Factors Window, click <strong>Next</strong>.</p> |
| |
| |
| <h3 id="Step3AddActivity">Step 3: Add an Activity</h2> |
| |
| <p>The next screen lets you select an activity type to add to your app, as shown in figure 4. |
| This screen displays a different set of activities for each of the form factors you selected earlier.</p> |
| |
| <img src="{@docRoot}images/tools/wizard5.png" alt="" width="720" height="504"> |
| <p class="img-caption"><strong>Figure 4.</strong> Add an activity to your app.</p> |
| |
| <p>Choose an activity type then click <strong>Next</strong>.</p> |
| |
| <p class="note"><strong>Note:</strong> If you choose "Add No Activity", click <strong>Finish</strong> |
| to create the project.</p> |
| |
| |
| <h3 id="Step4ConfigureActivity">Step 4: Configure Your Activity</h2> |
| |
| <p>The next screen lets you configure the activity to add to your app, as shown in figure 5.</p> |
| |
| <img src="{@docRoot}images/tools/wizard6.png" alt="" width="450" height="385"> |
| <p class="img-caption"><strong>Figure 5.</strong> Choose a name for your activity.</p> |
| |
| <p>Enter the activity name, the layout name, and the activity title. Then click |
| <strong>Finish</strong>.</p> |
| |
| |
| <h3 id="Step5DevelopYourApp">Step 5: Develop Your App</h2> |
| |
| <p>Android Studio creates the default structure for your project and opens the development |
| environment. If your app supports more than one form factor, Android Studio creates a module folder |
| with complete source files for each of them as shown in figure 6.</p> |
| |
| <img src="{@docRoot}images/tools/wizard7.png" alt="" width="750" height="509"> |
| <p class="img-caption"><strong>Figure 6.</strong> The default project structure for a mobile app.</p> |
| |
| <p>Now you are ready to develop your app. For more information, see the following links:</p> |
| |
| <ul> |
| <li><a href="{@docRoot}training/">Training Lessons</a></li> |
| <li><a href="{@docRoot}training/building-wearables.html">Building Apps for Wearables</a></li> |
| <li><a href="{@docRoot}tv/">Android TV</a></li> |
| <li><a href="https://developers.google.com/glass/">Google Glass</a></li> |
| </ul> |
| |
| |
| <h2 id="CreatingAModule">Creating an Android Module</h2> |
| |
| <p>Android application modules contain the <code>src/main/</code>, <code>AndroidManifest.xml</code>, |
| <code>build.gradle</code>, build output and other files you need to generate your app's APK files. |
| Android Studio provides a <em>New Module Wizard</em> that you can use to quickly create a new |
| Android module (or a module from existing code) based on selected application settings, such as |
| minimum SDK level and activity template.</p> |
| |
| <p>To create a new module, select <strong>File</strong> > <strong>New</strong> > |
| <strong>Module</strong>. Select the desire module type then click Next to enter the basic module |
| settings:</p> |
| |
| <ul> |
| <li>Enter an <strong>Application Name</strong>. This name is used as the title of your |
| application launcher icon when it is installed on a device.</li> |
| |
| <li>Enter a <strong>Module Name</strong>. This text is used as the name of the folder where |
| your Java-based activity files are stored.</li> |
| |
| <li>Enter a <strong>Package Name</strong> and <strong>Package Location</strong>. This class |
| package namespace creates the initial |
| package structure for your applications code files and is added as the |
| <a href="{@docRoot}guide/topics/manifest/manifest-element.html#package">{@code package}</a> |
| attribute in your application's |
| <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">Android manifest file</a>. |
| This manifest value serves as the unique identifier for your application app when you |
| distribute it to users. The package name must follow the same rules as packages in the Java |
| programming language.</li> |
| |
| <li>Select the <strong>Minimum required SDK</strong>. |
| This setting indicates the lowest version of the Android platform that your application |
| supports for the selected form factor. This value sets the |
| <code>minSdkVersion</code> attribute in the build.gradle file.</li> |
| |
| <p class="note"><strong>Note:</strong> You can manually change the minimum and target SDK |
| for your module at any time: Double-click the module's build.gradle in the Project Explorer, |
| set the <strong>targetSdkVersion</strong> and <em>targetSdkVersion</em> in the |
| <em>defaultConfig</em> section.</p> |
| |
| |
| <li>Select a <strong>Target SDK</strong>. This setting indicates the highest version of |
| Android with which you have tested with your application and sets the |
| <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code |
| targetSdkVersion}</a> attribute in your application's' build.gradle file. |
| |
| |
| <li>Select a <strong>Compile With</strong> API version. This setting specifies what version |
| of the SDK to compile your project against. We strongly recommend using the most recent |
| version of the API.</li> |
| |
| <li>Select a <strong>Language Level</strong> API version. This setting specifies what version |
| of the SDK to compile your project against. We strongly recommend using the most recent |
| version of the API.</li> |
| |
| <li>Select a <strong>Theme</strong>. This setting specifies which standard Android |
| <a href="{@docRoot}design/style/themes.html">visual style</a> is applied to your |
| application. Select activity template. For more information about Android code templates, see |
| <a href="{@docRoot}tools/projects/templates.html">Using Code Templates</a>. Leave the |
| <strong>Create activity</strong> option checked so you can start your |
| application with some essential components. </li> |
| |
| <li>Click the check box for the required Support Libraries then click <strong>Next</strong>.</li> |
| |
| <li>In the <strong>Configure Launcher Icon</strong> page, create an icon and options, then click |
| <strong>Next</strong>.</li> |
| |
| <li>In the <strong>Create Activity</strong> page, select activity template then click |
| <strong>Next</strong>. For more information about Android code templates, see |
| <a href="{@docRoot}tools/projects/templates.html">Using Code Templates</a>. |
| </li> |
| |
| <li>Review the new module settings then click <strong>Finish</strong>.</li> |
| |
| </ul> |
| |
| <p>The wizard creates a new Android application module according to the options you have chosen.</p> |
| |
| |
| |
| <h2 id="SettingUpLibraryModule">Setting up a Library Module</h2> |
| |
| <p>A library module is a standard Android module, so you can create a new one in the same way |
| as you would a new application module, using the New Module wizard and selecting <em>Android |
| Library</em> as the module type. The created library module will appear in your project view |
| along with the other modules. </p> |
| |
| <p> You can easily change an existing application module to a library module by changing the |
| plugin assignment in the <strong>build.gradle</strong> file to <em>com.android.library</em>.</p> |
| |
| <pre> |
| apply plugin: 'com.android.application' |
| |
| android {...} |
| </pre> |
| |
| <pre> |
| apply plugin: 'com.android.library' |
| |
| android {...} |
| </pre> |
| |
| |
| <h3>Adding a dependency on a library module</h3> |
| |
| <p>The library dependency can be declared in the module's manifest file or in the |
| <strong<build.gradle</strong> file. </p> |
| |
| <p>A library modules's manifest file must declare all of the shared components that it includes, |
| just as would a standard Android application. For more information, see the documentation for |
| <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p> |
| |
| <p>For example, the <a href= |
| "{@docRoot}resources/samples/TicTacToeLib/AndroidManifest.html">TicTacToeLib</a> example library |
| project declares the activity <code>GameActivity</code>:</p> |
| <pre> |
| <manifest> |
| ... |
| <application> |
| ... |
| <activity android:name="GameActivity" /> |
| ... |
| </application> |
| </manifest> |
| </pre> |
| |
| |
| <p>To add the dependency declaration to the build file, edit the build file for the <code>app</code> |
| module (<code>app/build.gradle</code>) and add a dependency on the <code>lib</code> module:</p> |
| |
| <pre> |
| ... |
| dependencies { |
| ... |
| compile project(":lib") |
| } |
| </pre> |
| |
| <p>In this example, the <code>lib</code> module can still be built and tested independently, and |
| the build system creates an AAR package for it that you could reuse in other projects.</p> |
| |
| <p class="note"><strong>Note:</strong> The library settings in the <code>app/build.gradle</code> |
| file will override any shared library resources declared in the manifest file.</p> |
| |
| |
| <h2 id="ReferencingLibraryModule">Referencing a library module</h2> |
| |
| <p>If you are developing an application and want to include the shared code or resources from a |
| library module, you can also do so easily by adding a reference to the library module in the |
| module's dependency page.</p> |
| |
| <p>To add a reference to a library module, follow these steps:</p> |
| |
| <ol> |
| <li>Make sure that both the module library and the application module that depends on it are |
| in your project. If one of the modules is missing, import it into your project.</li> |
| |
| <li>In the project view, right-click the dependent module and select |
| <strong>Open</strong> > <strong>Module Settings</strong>.</li> |
| |
| <li>Right-click the plus icon to add a new dependencies. |
| <p>If you are adding references to multiple libraries, you can set their relative |
| priority (and merge order) by selecting a library and using the <strong>Up</strong> and |
| <strong>Down</strong> controls. The tools merge the referenced libraries with your application |
| starting from lowest priority (bottom of the list) to highest (top of the list). If more than one |
| library defines the same resource ID, the tools select the resource from the library with higher |
| priority. The application itself has highest priority and its resources are always used in |
| preference to identical resource IDs defined in libraries.</p> |
| </li> |
| |
| <li>Use the <strong>Scope</strong> drop-down to select how the dependency will be applied.</li> |
| |
| <li>Click <strong>Apply</strong> to create the dependency and <strong>OK</strong> to close the |
| <strong>Project Structure</strong> window.</li> |
| </ol> |
| |
| <p>Android Studio rebuilds the module, including the contents of the library module the next time |
| the project or module is built.</p> |
| |
| |
| |
| <h3>Declaring library components in the manifest file</h3> |
| |
| <p>In the manifest file of the application module, you must add declarations of all components |
| that the application will use that are imported from a library module. For example, you must |
| declare any <code><activity></code>, <code><service></code>, |
| <code><receiver></code>, <code><provider></code>, and so on, as well as |
| <code><permission></code>, <code><uses-library></code>, and similar elements.</p> |
| |
| <p>Declarations should reference the library components by their fully-qualified package names, |
| where appropriate.</p> |
| |
| <p>For example, the <a href= |
| "{@docRoot}resources/samples/TicTacToeMain/AndroidManifest.html">TicTacToeMain</a> example |
| application declares the library activity <code>GameActivity</code> like this:</p> |
| <pre> |
| <manifest> |
| ... |
| <application> |
| ... |
| <activity android:name="com.example.android.tictactoe.library.GameActivity" /> |
| ... |
| </application> |
| </manifest> |
| </pre> |
| |
| <p>For more information about the manifest file, see the documentation for <a href= |
| "{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p> |
| |
| |
| <h2 id="ProjectView">Using the Android Project View</h2> |
| |
| |
| <p>The Android project view in Android Studio shows a flattened version of your project's structure |
| that provides quick access to the key source files of Android projects and helps you work with |
| the new <a href="{@docRoot}sdk/installing/studio-build.html">Gradle-based build system</a>. The |
| Android project view:</p> |
| |
| <ul> |
| <li>Groups the build files for all modules at the top level of the project hierarchy.</li> |
| <li>Shows the most important source directories at the top level of the module hierarchy.</li> |
| <li>Groups all the manifest files for each module.</li> |
| <li>Shows resource files from all Gradle source sets.</li> |
| <li>Groups resource files for different locales, orientations, and screen types in a single group |
| per resource type.</li> |
| </ul> |
| |
| <p>The <em>Android</em> project view is enabled by default and shows all the build files at |
| the top level of the project hierarchy under <strong>Gradle Scripts</strong>. The project module |
| appears as a folder at the top level of the project hierarchy and contains these three elements |
| at the top level:</p> |
| |
| <ul> |
| <li><code>manifests/</code> - Manifest files for the module.</li> |
| <li><code>java/</code> - Source files for the module.</li> |
| <li><code>res/</code> - Resource files for the module.</li> |
| </ul> |
| |
| <p>Notice how the Android project view groups all instances of the |
| <code>ic_launcher.png</code> resource for different screen densities under the same element.</p> |
| |
| <p class="note"><strong>Note:</strong> The Android project view shows a hierarchy that helps you |
| work with Android projects by providing a flattened structure that highlights the most commonly |
| used files while developing Android applications. However, the project structure on disk differs |
| from this representation and maintains the traditional project structure.</p> |
| |
| <img src="{@docRoot}images/tools/projectview-p1.png" alt="" style="width:240px" "/> |
| |
| <img src="{@docRoot}images/tools/projectview-p2.png" alt="" style="width:240px" " /> |
| <p class="img-caption"><strong>Figure 10:</strong> Android and Traditional project view </p> |