page.title=Android Studio Overview page.image=images/cards/card-android-studio-overview_16x9_2x.jpg page.metaDescription=Learn about the official IDE for Android. page.tags=studio,sdk,tools,firstapp @jd:body
Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. On top of the capabilities you expect from IntelliJ, Android Studio offers:
apk
file generationIf you're new to Android Studio or the IntelliJ IDEA interface, this page provides an introduction to some key Android Studio features.
For specific Android Studio how-to documentation, see the pages in the Workflow section, such as Managing Projects from Android Studio and Building and Running from Android Studio. For a summary of the latest changes to Android Studio, see the Android Studio Release Notes.
By default, Android Studio displays your project files in the Android project view. This view 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 Gradle-based build system. The Android project view:
Figure 1. Show the Android project view.
Figure 2. Show project build Files.
The Android project view shows all the build files at the top level of the project hierarchy under Gradle Scripts. Each project module appears as a folder at the top level of the project hierarchy and contains these three elements at the top level:
java/
- Source files for the module.manifests/
- Manifest files for the module.res/
- Resource files for the module.Gradle Scripts/
- Gradle build and property files.For example, Android project view groups all the instances of the
ic_launcher.png
resource for different screen densities under the same element.
Note: The project structure on disk differs from this flattened representation. To switch to back to the segregated project view, select Project from the Project drop-down.
When you use the Project view in Android Studio, you should notice that the project structure appears different than you may be used to in Eclipse. Each instance of Android Studio contains a project with one or more application modules. Each application module folder contains the complete source sets for that module, including {@code src/main} and {@code src/androidTest} directories, resources, build file and the Android manifest. For the most part, you will need to modify the files under each module's {@code src/main} directory for source code updates, the gradle.build file for build specification and the files under {@code src/androidTest} directory for test case creation.
You can also customize the view of the project files to focus on specific aspects of your app development:
For example, selecting the Problems view of your project displays links to the source files containing any recognized coding and syntax errors, such as missing a XML element closing tag in a layout file.
For more information, see IntelliJ project organization and Managing Projects.
You can quickly add new code and resource files by clicking the appropriate directory in the
Project pane and pressing ALT + INSERT
on Windows and Linux or
COMMAND + N
on Mac. Based on the type of directory selected, Android Studio
offers to create the appropriate file type.
For example, if you select a layout directory, press ALT + INSERT
on Windows,
and select Layout resource file, a dialog opens so you can name the file
(you can exclude the {@code .xml} suffix) and choose a root view element. The editor then
switches to the layout design editor so you can begin designing your layout.
The Android build system is the toolkit you use to build, test, run and package your apps. This build system replaces the Ant system used with Eclipse ADT. It can run 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:
The flexibility of the Android build system enables you to achieve all of this without modifying your app's core source files. To build an Android Studio project, see Building and Running from Android Studio. To configure custom build settings in an Android Studio project, see Configuring Gradle Builds.
With the Android build system, the applicationId attribute is used to
uniquely identify application packages for publishing. The application ID is set in the
android section of the build.gradle
file.
apply plugin: 'com.android.application' android { compileSdkVersion 19 buildToolsVersion "19.1" defaultConfig { applicationId "com.example.my.app" minSdkVersion 15 targetSdkVersion 19 versionCode 1 versionName "1.0" } ...
Note: The applicationId is specified only in your build.gradle file, and not in the AndroidManifest.xml file.
When using build variants, the build system enables you to uniquely identify different packages for each product flavors and build types. The application ID in the build type is added as a suffix to those specified for the product flavors.
productFlavors { pro { applicationId = "com.example.my.pkg.pro" } free { applicationId = "com.example.my.pkg.free" } } buildTypes { debug { applicationIdSuffix ".debug" } } ....
The package name must still be specified in the manifest file. It is used in your source code to refer to your R class and to resolve any relative activity/service registrations.
package="com.example.app"
Note: If you have multiple manifests (for example, a product
flavor specific manifest and a build type manifest), the package name is optional in those manifests.
If it is specified in those manifests, the package name must be identical to the package name
specified in the manifest in the src/main/
folder.
For more information about the build files and process, see Build System Overview.
AVD Manager has updated screens with links to help you select the most popular device configurations, screen sizes and resolutions for your app previews.
Click the Android Virtual Device ManagerThe AVD Manager comes with emulators for Nexus 6 and Nexus 9 devices and also supports creating custom Android device skins based on specific emulator properties and assigning those skins to hardware profiles. Android Studio installs the Intel® x86 Hardware Accelerated Execution Manager (HAXM) emulator accelerator and creates a default emulator for quick app prototyping.
For more information, see Managing AVDs.
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:
To enable inline debugging, in the Debug window click the Settings icon
and select the
check box for Show Values In Editor.
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 Android tab in the lower left corner of the runtime window to launch the Android runtime window. Click the Memory or CPU tab.
Figure 4. Monitor memory and CPU usage.
The Android SDK tools, such as Systrace, logcat, and Traceview, generate performance and debugging data for detailed app analysis.
To view the available generated data files, click Captures 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 .hprof
files to convert them to a standard
.hprof
file format.
In Android Studio, the configured lint
and other IDE inspections run automatically whenever you compile your program. In addition to the
configured {@code lint} checks, additional
IntelliJ code inspections and annotation validation run to streamline code
review.
Android Studio enables several lint
checks
to ensure:
Cipher.getInstance()
is used with safe valuesHovering 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.
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
lintOptions
property to the Android settings in the build.gradle
file.
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 }
You can also manage inspection profiles and configure inspections within Android Studio. Choose File > Settings > Project Settings and expand Editor. The Inspection Configuration page appears with the supported inspections.
Figure 5. Configure inspections.
Note: To change the behavior of specific inspection notifications, change the inspection severity, for example from warning to error.
To manually run inspections in Android Studio, choose Analyze > Inspect Code. The Inspections Scope dialog appears so you can specify the desired inspection profile and scope.
You can also run {@code lint} inspections from the command line in your SDK directory.
sdk$ lint [flags]
Note: The {@code lint} --show and --list flags can be used to display the available issues and explanations.
For more information, see Improving Your Code with {@code lint} and lint tool.
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 Android SDK Manager 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.
To add annotations to your code in Android Studio, first add a dependency for the {@link android.support.annotation Support-Annotations} library:
support-annotations
and
click Ok. The build.gradle
file is updated with the support-annotations
dependency.
You can also manually add this dependency to your build.gradle
file, as shown in
the following example.
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' }
A nullability analysis scans the contracts throughout the method hierarchies in your code to detect:
The analysis then automatically inserts the appropriate null annotations in the detected locations.
To run a nullability analysis in Android Studio, select the Analyze > Infer Nullity 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.
Note: The nullability analysis may insert the IntelliJ
@Nullable
and
@NotNull
annotations instead of the Android null annotations. When running
a null analysis, manually search and replace any IntelliJ annotations or include
com.intellij:annotations:12.0
as a compile dependency in your
build.gradle
file. This example includes the IntelliJ annotations 12.0 library as a
dependency in the build.gradle
file:
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' compile 'com.intellij:annotations:12.0' }
You can also manually add nullability, resource, and enumerated annotations throughout your code
to perform validations for a variety of reference values, such as
R.string
resources,
Drawable
resources,
Color
resources,
and enumerated constants.
Run Analyze > Inspect Code to validate the configured annotations.
For a complete list of the supported annotations, either use the auto-complete feature to display
the available options for the import android.support.annotation.
statement or
view the contents of the
{@link android.support.annotation Support-Annotations}
library.
For more details about Android annotations, see Improving Code Inspection with Annotations.
Android Studio allows you to work with layouts in both a Design View
Figure 6. Hello World App with Design View.
and a Text View.
Easily select and preview layout changes for different device images, display densities, UI modes, locales, and Android versions (multi-API version rendering).
Figure 8. Multi-API version rendering.
From the Design View, you can drag and drop elements from the Palette to the Preview or Component Tree. The Text View allows you to directly edit the XML settings, while previewing the device display.
When you build and run your app with Android Studio, you can view adb and device log messages (logcat) in the DDMS pane by clicking Android at the bottom of the window.
If you want to debug your app with the
Android Debug Monitor, you can launch it by
clicking Monitor
in the toolbar. The Debug Monitor is where you can find the complete set of
DDMS tools for profiling your app,
controlling device behaviors, and more. It also includes the Hierarchy Viewer tools to help
optimize your layouts.
When you begin the installation process, an installation and setup wizard walks you through a step-by-step installation and setup process as the wizard checks for system requirements, such as the Java Development Kit (JDK) and available RAM, and then prompts for optional installation options, such as the Intel® HAXM emulator accelerator.
During the installation process, a setup wizard walks you through the setup processes as the wizard updates your system image and emulation requirements, such GPU, and then creates an optimized default Android Virtual Device (AVD) based on Android 5 (Lollipop) for speedy and reliable emulation.
Figure 9. Installation and setup wizard.
Android Studio supports templates for Google Services and expands the available device types.
For easy cross-platform development, the Project Wizard provides templates for creating your apps for Android Wear and TV.
Figure 10. Supported form factors.
During app creation, the Project Wizard also displays an API Level dialog to help you choose the best minSdkVersion for your project.
Quick cloud integration. Using Google App Engine to connect to the Google cloud and create a cloud end-point is as easy as selecting File > New Module > App Engine Java Servlet Module and specifying the module, package, and client names.
Figure 11. Google App Engine integration.
Android Studio provides setting dialogs so you can manage the most important project and file
settings from the File menus as well as the build and configuration files. For
example, you can use the File > Project Structure menu or
the build.gradle
file to update your productFlavor
settings.
Additional settings from the File menus include:
Android Studio provides four update channels to keep Android Studio up-to-date based on your code-level preference:
By default, Android Studio uses the Stable channel. Use File > Settings > Updates to change your channel setting.
Proxies serve as intermediary connection points between HTTP clients and web servers that add security and privacy to internet connections.
To support running Android Studio behind a firewall, set the proxy settings for the Android Studio IDE and the SDK Manager. Use the Android Studio IDE HTTP Proxy settings page to set the HTTP proxy settings for Android Studio. The SDK Manager has a separate HTTP Proxy settings page.
When running the Android Plugin for Gradle from the command line or on machines where Android Studio is not installed, such as continuous integration servers, set the proxy settings in the Gradle build file.
Note: After the initial installation of the Android Studio bundle, Android Studio can run with internet access or off-line. However, Android Studio requires an internet connection for Setup Wizard synchronization, 3rd-party library access, access to remote repositories, Gradle initialization and synchronization, and Android Studio version updates.
Android Studio supports HTTP proxy settings so you can run Android Studio behind a firewall or secure network. To set the HTTP proxy settings in Android Studio:
For application-specific HTTP proxy settings, set the proxy settings in the build.gradle file as required for each application module.
apply plugin: 'com.android.application' android { ... defaultConfig { ... systemProp.http.proxyHost=proxy.company.com systemProp.http.proxyPort=443 systemProp.http.proxyUser=userid systemProp.http.proxyPassword=password systemProp.http.auth.ntlm.domain=domain } ... }
For project-wide HTTP proxy settings, set the proxy settings in the
gradle/gradle.properties
file.
# Project-wide Gradle settings. ... systemProp.http.proxyHost=proxy.company.com systemProp.http.proxyPort=443 systemProp.http.proxyUser=username systemProp.http.proxyPassword=password systemProp.http.auth.ntlm.domain=domain systemProp.https.proxyHost=proxy.company.com systemProp.https.proxyPort=443 systemProp.https.proxyUser=username systemProp.https.proxyPassword=password systemProp.https.auth.ntlm.domain=domain ...
For information about using Gradle properties for proxy settings, see the Gradle User Guide.
Note: When using Android Studio, the settings in the Android Studio IDE HTTP proxy settings page override the HTTP proxy settings in the gradle.properties file.
SDK Manager proxy settings enable proxy internet access for Android package and library updates from SDK Manager packages.
To set the SDK Manager settings for proxy internet access, start the SDK Manager and open the SDK Manager page.
The Android SDK Manager page appears. Enter the settings and click Apply.
Multi-language support is enhanced with the Translations Editor plugin so you can easily add a variety of locales to the app's translation file. With BCP 47 support, the editor combines language and region codes into a single selection for targeted localizations. Color codes indicate whether a locale is complete or still missing string translations.
To access the Translations Editor, open a strings.xml
file and click the
Open Editor link.
Figure 12. Add locales and strings in the Translations Editor.
Android Studio supports the
Material Design themes, widgets, and
graphics, such as shadow layers and API version rendering (showing the layout across different
UI versions). Also, the drawable XML tags and attributes, such as <ripple>
and <animated-selector>
, are supported.
Clicking Import Samples from the File menu or Welcome page provides seamless access to Google code samples on GitHub.
Figure 13. Get code samples from GitHub.
Figure 14. Imported code sample.