bug: 27850063 (update system reqs for 2.0) bug: 27725026 (update download page for 2.0) bug: 23908299 (clean up download and get started) depends on CL: Ibc4ddb4a6130ab8bd8d6044472022465829f2000 Change-Id: Id56ddec26e5701f69cd3e3ce7dc64cd28e98c8f3
397 lines
17 KiB
Plaintext
397 lines
17 KiB
Plaintext
page.title=Android Studio Overview
|
|
page.image=images/cards/card-android-studio-overview_16x9_2x.jpg
|
|
page.metaDescription=The basics of working with Android Studio, from projects to build and performance.
|
|
page.tags=studio,sdk,tools,firstapp
|
|
@jd:body
|
|
|
|
<div id="qv-wrapper">
|
|
<div id="qv">
|
|
|
|
<h2>In this document</h2>
|
|
<ol>
|
|
<li><a href="#project-structure">Project Structure</a></li>
|
|
<li><a href="#build-system">Gradle Build System</a></li>
|
|
<li><a href="#debug-perf">Debug and Profile Tools</a></li>
|
|
</ol>
|
|
|
|
|
|
<a class="button" href="{@docRoot}sdk/index.html" style="margin:15px">
|
|
Get Android Studio
|
|
</a>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<p>Android Studio is the official IDE for Android app development, based on
|
|
<a class="external-link" href="https://www.jetbrains.com/idea/" target=
|
|
"_blank">IntelliJ IDEA</a>. On top of IntelliJ's powerful code editor and
|
|
developer tools, Android Studio offers even more features that enhance your
|
|
productivity when building Android apps, such as:</p>
|
|
|
|
|
|
<ul>
|
|
<li>A flexible Gradle-based build system</li>
|
|
<li>Build variants and multiple APK file generation</li>
|
|
<li>Code templates to help you build common app features</li>
|
|
<li>A rich layout editor with support for drag and drop theme editing</li>
|
|
<li>Lint tools to catch performance, usability, version compatibility, and other problems</li>
|
|
<li>Code shrinking with ProGuard and resource shrinking with Gradle</li>
|
|
<li>Built-in support for
|
|
<a href="http://developers.google.com/cloud/devtools/android_studio_templates/">Google Cloud Platform</a>,
|
|
making it easy to integrate Google Cloud Messaging and App Engine</li>
|
|
</ul>
|
|
|
|
<p>This page provides an introduction to basic Android Studio features. For
|
|
more detailed guides to using Android Studio, start by browsing pages in the
|
|
<a href="{@docRoot}tools/workflow/index.html">Workflow</a> section.</p>
|
|
|
|
<p>For a summary of the latest changes, see the <a href=
|
|
"{@docRoot}tools/revisions/studio.html">Android Studio Release Notes</a>.</p>
|
|
|
|
|
|
|
|
<h2 id="project-structure">Project Structure</h2>
|
|
|
|
<div class="figure" style="width:230px">
|
|
<img src="{@docRoot}images/tools/studio/studio-project-files.png" width="230" />
|
|
<p class="img-caption"><strong>Figure 1.</strong> The project files in Android
|
|
view.</p>
|
|
</div>
|
|
|
|
<p>Each project in Android Studio contains one or more modules with source code
|
|
files and resource files. Different types of modules include:</p>
|
|
|
|
<ul>
|
|
<li>Android app modules</li>
|
|
<li>Test modules</li>
|
|
<li>Library modules</li>
|
|
<li>App Engine modules</li>
|
|
</ul>
|
|
|
|
<p>By default, Android Studio displays your project files in the
|
|
<em>Android</em> project view, as shown in figure 1.
|
|
This view is organized by modules to
|
|
provide quick access to the key source files of your
|
|
project.</p>
|
|
|
|
<p>All the build files are visible at the top level
|
|
under <strong>Gradle Scripts</strong> and each app module
|
|
contains the following three elements:</p></p>
|
|
<ul>
|
|
<li><b>manifests</b>: Manifest files.</li>
|
|
<li><b>java</b>: Source code files.</li>
|
|
<li><b>res</b>: Resource files.</li>
|
|
</ul>
|
|
|
|
<p>The Android project structure on disk differs
|
|
from this flattened representation. To see the actual file structure of the
|
|
project, select <strong>Project</strong> from the <strong>Project</strong>
|
|
drop-down (in figure 1, it's showing as <strong>Android</strong>).</p>
|
|
|
|
<p>You can also customize the view of the project files to focus on specific
|
|
aspects of your app development. For example, selecting the
|
|
<strong>Problems</strong> view of your project displays links to the source
|
|
files containing any recognized coding and syntax errors, such as missing an
|
|
XML element closing tag in a layout file.</p>
|
|
|
|
<p>For more information, see
|
|
<a class="external-link" href="http://confluence.jetbrains.com/display/IntelliJIDEA/Project+Organization">IntelliJ project organization</a>
|
|
and <a href="{@docRoot}tools/projects/index.html">Managing Projects</a>.</p>
|
|
|
|
|
|
<h2 id="build-system">Gradle Build System</h2>
|
|
|
|
<p>Android Studio uses Gradle as the foundation of the build system, with
|
|
more Android-specific capabilities provided by the <a
|
|
href="{@docRoot}tools/revisions/gradle-plugin.html">Android Plugin for
|
|
Gradle</a>. This build system
|
|
runs as an integrated tool from the Android Studio menu and independently from
|
|
the command line. You can use the features of the build system to:</p>
|
|
|
|
<ul>
|
|
<li>Customize, configure, and extend the build process.</li>
|
|
<li>Create multiple APKs for your app with different features using the same project and
|
|
modules.</li>
|
|
<li>Reuse code and resources across source sets.</li>
|
|
</ul>
|
|
|
|
<p>The flexibility of Gradle enables you to achieve all of this without
|
|
modifying your app's core source files. To build an Android Studio project, see
|
|
<a href="{@docRoot}tools/building/building-studio.html">Building and Running
|
|
from Android Studio</a>. To configure custom build settings in an Android
|
|
Studio project, see <a href=
|
|
"{@docRoot}tools/building/configuring-gradle.html">Configuring Gradle
|
|
Builds</a>.</p>
|
|
|
|
|
|
|
|
|
|
<h2 id="debug-perf">Debug and Profile Tools</h2>
|
|
|
|
<p>Android Studio assists you in debugging and improving the
|
|
performance of your code, including inline debugging and
|
|
performance analysis tools.</p>
|
|
|
|
|
|
<h3 id="inline-debug">Inline debugging</h3>
|
|
|
|
<p>Use inline debugging to enhance your code walk-throughs in the debugger view
|
|
with inline verification of references, expressions, and variable values.
|
|
Inline debug information includes:</p>
|
|
|
|
<ul>
|
|
<li>Inline variable values</li>
|
|
<li>Referring objects that reference a selected object </li>
|
|
<li>Method return values</li>
|
|
<li>Lambda and operator expressions</li>
|
|
<li>Tool tip values</li>
|
|
</ul>
|
|
|
|
<p>To enable inline debugging, in the <em>Debug</em> window click the Settings icon
|
|
<img src="{@docRoot}images/tools/studio-debug-settings-icon.png"/> and select the
|
|
check box for <strong>Show Values In Editor</strong>.</p>
|
|
|
|
<h3 id="mem-cpu">Memory and CPU monitor</h3>
|
|
<p>Android Studio provides a memory and CPU monitor view so you can more easily monitor your
|
|
app's performance and memory usage to track CPU usage, find deallocated objects, locate memory
|
|
leaks, and track the amount of memory the connected device is using. With your app running on a
|
|
device or emulator, click the <strong>Android</strong> tab in the lower left corner of the
|
|
runtime window to launch the Android runtime window. Click the <strong>Memory</strong> or
|
|
<strong>CPU</strong> tab. </p>
|
|
|
|
<img src="{@docRoot}images/tools/studio-memory-monitor.png"
|
|
srcset="{@docRoot}images/tools/studio-memory-monitor2x.png 2x" width"635" height="171" alt="" />
|
|
<p class="img-caption"><strong>Figure 2.</strong> Monitor memory and CPU usage.</p>
|
|
|
|
<h4 id="heap-dump">Heap dump </h4>
|
|
<p>When you're monitoring memory usage in Android Studio you can, at the same time, initiate
|
|
garbage collection and dump the Java heap to a heap snapshot in an Android-specific HPROF binary
|
|
format file. The HPROF viewer displays classes, instances of each class, and a reference tree to
|
|
help you track memory usage and find memory leaks. </p>
|
|
|
|
<img src="{@docRoot}images/tools/studio-hprof-viewer.png" alt="" />
|
|
<p class="img-caption"><strong>Figure 3.</strong> HPROF viewer with heap dump.</p>
|
|
|
|
<p>To create a snapshot of the Android app heap memory, click the
|
|
Dump Java Heap icon (<img src="{@docRoot}images/tools/studio-dump-heap-icon.png" style="vertical-align:bottom;margin:0;height:17px"/>)
|
|
in the Memory Monitor. Android Studio creates the heap snapshot file with the filename
|
|
<code>Snapshot-yyyy.mm.dd-hh.mm.ss.hprof</code>
|
|
in the <em>Captures</em> tab. Double-click the heap snapshot file to open the HPROF viewer.</p>
|
|
|
|
<p>To convert a heap dump to standard HPROF format in Android Studio, right-click a heap
|
|
snapshot in the <em>Captures</em> view and select <strong>Export to standard .hprof</strong>. </p>
|
|
|
|
|
|
|
|
<h4 id="alloc-tracker">Allocation tracker </h4>
|
|
<p>Android Studio allows you to track memory allocation as it monitors memory use. Tracking memory
|
|
allocation allows you to monitor where objects are being allocated when you perform certain actions.
|
|
Knowing these allocations enables you to adjust the method
|
|
calls related to those actions to optimize your app's performance and memory use. </p>
|
|
|
|
<img src="{@docRoot}images/tools/studio-allocation-tracker.png" alt="" />
|
|
<p class="img-caption"><strong>Figure 4.</strong> Allocation tracker.</p>
|
|
|
|
<p>For information about tracking and analyzing allocations, see
|
|
<a href="{@docRoot}/tools/help/am-memory.html#tracking">Memory Monitor</a>.
|
|
</p>
|
|
|
|
|
|
<h3>Data file access</h3>
|
|
<p>The Android SDK tools, such as <a href="{@docRoot}tools/help/systrace.html">Systrace</a>,
|
|
<a href="{@docRoot}tools/help/logcat.html">logcat</a>, and
|
|
<a href="{@docRoot}tools/help/traceview.html">Traceview</a>, generate performance and debugging
|
|
data for detailed app analysis.</p>
|
|
|
|
<p>To view the available generated data files, click <strong>Captures</strong> in the left
|
|
corner of the runtime window. In the list of the generated files, double-click a file to view
|
|
the data. Right-click any <code>.hprof</code> files to convert them to a standard
|
|
<a href="{@docRoot}tools/help/hprof-conv.html"><code>.hprof</code> </a> file format.</p>
|
|
|
|
|
|
<h3>Code inspections</h3>
|
|
<p>In Android Studio, the configured <a href="{@docRoot}tools/help/lint.html"><code>lint</code></a>
|
|
and other IDE inspections run automatically whenever you compile your program. In addition to the
|
|
configured {@code lint} checks, additional
|
|
<a class="external-link" href="https://www.jetbrains.com/idea/help/inspection-basics.html?search=inspection"
|
|
target="_blank">IntelliJ code inspections</a> and annotation validation run to streamline code
|
|
review.</p>
|
|
|
|
|
|
<p>Android Studio enables several <code>lint</code> checks
|
|
to ensure:
|
|
<ul>
|
|
<li><code> Cipher.getInstance()</code> is used with safe values</li>
|
|
<li>In custom Views, the associated declare-styleable for the custom view uses the same
|
|
base name as the class name</li>
|
|
<li>Security check for fragment injection</li>
|
|
<li>Where ever property assignment no longer works as expected</li>
|
|
<li>Gradle plugin version is compatible with the SDK</li>
|
|
<li>Right to left validation </li>
|
|
<li>Required API version</li>
|
|
<li>many others</li>
|
|
</ul>
|
|
|
|
|
|
<p>Hovering over an inspection error displays the full issue explanation inline for easy error
|
|
resolution. There is also a helpful hyperlink at the end of the error message for additional
|
|
error information.</p>
|
|
|
|
<p>With Android Studio, you can also run {@code lint} inspections for a specific build variant, or
|
|
for all build variants. You can configure the {@code lint} inspections that run by adding a
|
|
<code>lintOptions</code> property to the Android settings in the <code>build.gradle</code>
|
|
file. </p>
|
|
|
|
<pre>
|
|
android {
|
|
lintOptions {
|
|
// set to true to turn off analysis progress reporting by lint
|
|
quiet true
|
|
// if true, stop the gradle build if errors are found
|
|
abortOnError false
|
|
// if true, only report errors
|
|
ignoreWarnings true
|
|
}
|
|
</pre>
|
|
|
|
|
|
<p>You can also manage inspection profiles and configure inspections within Android Studio.
|
|
Choose <strong>File > Settings ></strong>, expand the <strong>Editor</strong> options,
|
|
and select <strong>Inspections</strong>.
|
|
The <em>Inspection Configuration</em> page appears with the supported inspections.</p>
|
|
<p><img src="{@docRoot}images/tools/studio-inspections-config.png" alt="" /> </p>
|
|
<p class="img-caption"><strong>Figure 5.</strong> Configure inspections.</p>
|
|
|
|
<p class="note"><strong>Note:</strong> To change the behavior of specific
|
|
inspection notifications, change the inspection severity, for example from <em>warning</em>
|
|
to <em>error</em>. </p>
|
|
|
|
|
|
<p>To manually run inspections in Android Studio, choose <strong>Analyze > Inspect Code</strong>.
|
|
The <em>Inspections Scope</em> dialog appears so you can specify the desired inspection profile and scope.</p>
|
|
|
|
|
|
<p>For more information, see
|
|
<a href="{@docRoot}tools/debugging/improving-w-lint.html">Improving Your Code with {@code lint}</a>
|
|
and <a href="{@docRoot}tools/help/lint.html">lint tool</a>.</p>
|
|
|
|
|
|
|
|
<h3 id="annotations">Annotations in Android Studio</h3>
|
|
<p>Android Studio supports annotations for variables, parameters, and return values to help you
|
|
catch bugs, such as null pointer exceptions and resource type conflicts. The
|
|
<a href="{@docRoot}tools/help/sdk-manager.html">Android SDK Manager</a> packages
|
|
the {@link android.support.annotation Support-Annotations} library
|
|
in the Android Support Repository for use with Android
|
|
Studio. Android Studio validates the configured annotations during code inspection. </p>
|
|
|
|
<p>To add annotations to your code in Android Studio, first add a dependency for the
|
|
{@link android.support.annotation Support-Annotations} library:</p>
|
|
<ol>
|
|
<li>Select <strong>File > Project Structure</strong>.</li>
|
|
<li>In the <em>Project Structure</em> dialog, select the desired module, click the
|
|
<strong>Dependencies</strong> tab. </li>
|
|
<li>Click the <img src="{@docRoot}images/tools/studio-add-icon.png"/> icon to include a
|
|
<strong>Library dependency</strong>.</li>
|
|
<li>In the <em>Choose Library Dependency</em> dialog, select <code>support-annotations</code> and
|
|
click <strong>Ok</strong>. </li>
|
|
</ol>
|
|
|
|
<p>The <code>build.gradle</code> file is updated with the <code>support-annotations</code>
|
|
dependency.</p>
|
|
|
|
<p>You can also manually add this dependency to your <code>build.gradle</code> file, as shown in
|
|
the following example. </p>
|
|
|
|
<pre>
|
|
dependencies {
|
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
|
compile 'com.android.support:appcompat-v7:22.0.0'
|
|
<strong>compile 'com.android.support:support-annotations:22.0.0'</strong>
|
|
}
|
|
</pre>
|
|
|
|
|
|
|
|
<h4>Inferring nullability</h4>
|
|
<p>A nullability analysis scans the contracts throughout the method hierarchies in your code to
|
|
detect:</p>
|
|
<ul>
|
|
<li>Calling methods that can return null </li>
|
|
<li>Methods that should not return null </li>
|
|
<li>Variables, such as fields, local variables, and parameters, that can be null </li>
|
|
<li>Variables, such as fields, local variables, and parameters, that cannot hold a null value </li>
|
|
</ul>
|
|
|
|
<p>The analysis then automatically inserts the appropriate null annotations in the detected
|
|
locations. </p>
|
|
|
|
<p>To run a nullability analysis in Android Studio,
|
|
select the <strong>Analyze > Infer Nullity</strong>
|
|
menu option. Android Studio inserts the Android
|
|
{@link android.support.annotation.Nullable @Nullable} and
|
|
{@link android.support.annotation.NonNull @NonNull} annotations in detected locations
|
|
in your code. After running a null analysis, it's good practice to verify the injected
|
|
annotations.</p>
|
|
|
|
<p class="note"><strong>Note:</strong> The nullability analysis may insert the IntelliJ
|
|
<a class="external-link" href="https://www.jetbrains.com/idea/help/-nullable-and-notnull-annotations.html?search=annotations">
|
|
<code>@Nullable</code></a> and
|
|
<a class="external-link" href="https://www.jetbrains.com/idea/help/-nullable-and-notnull-annotations.html?search=annotations">
|
|
<code>@NotNull</code></a> annotations instead of the Android null annotations. When running
|
|
a null analysis, manually search and replace any IntelliJ annotations or include
|
|
<code>com.intellij:annotations:12.0</code> as a compile dependency in your
|
|
<code>build.gradle</code> file. This example includes the IntelliJ annotations 12.0 library as a
|
|
dependency in the <code>build.gradle</code> file:
|
|
|
|
<pre>
|
|
dependencies {
|
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
|
compile 'com.android.support:appcompat-v7:22.0.0'
|
|
compile 'com.android.support:support-annotations:22.0.0'
|
|
<strong>compile 'com.intellij:annotations:12.0'</strong>
|
|
}
|
|
</pre>
|
|
|
|
</p>
|
|
|
|
|
|
<h4>Validating annotations</h4>
|
|
<p>You can also manually add nullability, resource, and enumerated annotations throughout your code
|
|
to perform validations for a variety of reference values, such as
|
|
<a href="{@docRoot}reference/android/R.string.html"><code>R.string</code></a> resources,
|
|
<a href="{@docRoot}guide/topics/resources/drawable-resource.htm"><code>Drawable</code></a>
|
|
resources,
|
|
<a href="{@docRoot}reference/android/graphics/Color.html"><code>Color</code></a> resources,
|
|
and enumerated constants. </p>
|
|
|
|
<p>Run <strong>Analyze > Inspect Code</strong> to validate the configured annotations. </p>
|
|
|
|
<p>For a complete list of the supported annotations, either use the auto-complete feature to display
|
|
the available options for the <code>import android.support.annotation</code> statement or
|
|
view the contents of the
|
|
{@link android.support.annotation Support-Annotations}
|
|
library. </p>
|
|
|
|
<p>For more details about Android annotations, see
|
|
<a href="{@docRoot}tools/debugging/annotations.html">Improving Code Inspection with Annotations</a>.
|
|
|
|
<h3>Log messages</h3>
|
|
<p>When you build and run your app with Android Studio, you can view adb and device log messages
|
|
(logcat) by clicking <strong>Android</strong> at the bottom of the window.</p>
|
|
|
|
<p>If you want to debug your app with the
|
|
<a href="{@docRoot}tools/help/monitor.html">Android Debug Monitor</a>, you can launch it by
|
|
clicking <strong>Monitor</strong>
|
|
<img src="{@docRoot}images/tools/monitor-studio.png" style="vertical-align:bottom;margin:0;height:19px"/>
|
|
in the toolbar. The Debug Monitor is where you can find the complete set of
|
|
<a href="{@docRoot}tools/debugging/ddms.html">DDMS</a> tools for profiling your app,
|
|
controlling device behaviors, and more. It also includes the Hierarchy Viewer tools to help
|
|
<a href="{@docRoot}tools/debugging/debugging-ui.html"> optimize your layouts</a>.</p>
|
|
|
|
|
|
|
|
|
|
|