Page tree
Skip to end of metadata
Go to start of metadata

中文标题【初识你的安卓开发工具(Android Studio)】

Android Studio 是基于 IntelliJ IDEA 的 Android  应用的官方的整合开发环境(IDE)。在基于 IntelliJ's 的高效和强大的代码开发编辑工具上,Android Studio 提供了更多的特性来让你更加有效的编辑和创建 Android 应用,例如:

  • 一个基于 Gradle 的灵活构建系统
  • 一个快速并功能丰富的模拟器
  • 你可以开发所有安卓设备的统一环境
  • 直接运行和部署修改到你运行的 APP 上而不需要重新构建 APK 文件
  • 代码模块和 GitHub 的整合来帮助你构建基本通用的 APP 特性,同时能导入相同的代码。
  • 广泛的测试工具和框架
  • Lint 工具来捕获性能,可用性,版本兼容性和其他问题。
  • C++ 和 NDK s支持
  • 内建支持 Google 云平台(Google Cloud Platform)能够让你更加容易的整合 Google 云消息和 App 引擎

本页面提供了 Android Studio 的相关特性介绍。有关 Android Studio 的最新发布的备注请参考(Android Studio Release Notes) 页面中的内容。

本页中的内容:

相关页面:

项目结构

每一个 Android Studio 项目包含有一个或者多个的模块。每一个模块中有源代码和资源文件。模块的类型有:

  • 安卓 app 模块(Android app modules)
  • 库模块(Library modules)
  • Google App 引擎模块(Google App Engine modules)

在默认的情况下,Android Studio 在 Android 项目视图中显示你的项目文件,如右侧的图片显示的内容。这个视图是按照模块来进行组织的,能够让你能够快速的访问你项目的源代码文件。

所有的构建文件将会在最顶层的 Gradle 脚本(Gradle Scripts)中,每一个项目的模块保护有项目的文件夹:

  • manifests:包含有 AndroidManifest.xml 文件
  • java:包含有 Java 的源代码文件,同时还包括有 JUnit 测试代码。
  • res:包含有所有的非代码资源,例如 XML 布局,UI 字符串和 bitmap 图片。

Android 项目在磁盘中的文件结构和视图显示的结构是不相同的。如果你希望查看你的项目在磁盘中显示的文件结构是什么样的,请从项目的下拉中选择 项目(Project)请注意,右侧中的图片显示的是 Android 视图。

你可以为你的项目文件自定试图来满足你对项目开发的特定需求。例如你可以选择 问题(Problems)视图来查你的源代码中的编译错误和句法错误的快速链接。例如你在布局文件中可能没有关闭 XML 的标签。




本图显示了 问题(Problems)视图,用于显示你项目中存在的问题。


有关更多的信息请查看页面:Managing Projects

用户界面

Android Studio 主窗口界面由下面的一些逻辑区域组成,请参考下图。

图 3. Android Studio 的主窗口

  1. 工具栏(toolbar)让你能够执行很多操作,包括运行你的 app 和运行 Android 工具。
  2. 导航栏(navigation bar)帮你能够在你的项目中快速的进行导航和打开文件进行编辑。这里的导航栏提供和 项目(Project)窗口不同的文件结构导航。
  3. 编辑窗口 (editor window)是你创建和编辑代码的地方。基于你当前打开的文件类型不同,打开的编辑器也会进行改变。例如,当你打开的是布局文件的时候,这里的编辑器将会载入布局编辑器。
  4. 工具窗口栏( tool window bar)是在 IDE 窗口外运行的,这里保护有按钮能够让你可以扩展和收起独立的工具窗。
  5. 工具窗口( tool windows)让你能够访问特定的任务,例如项目管理,查找,版本控制等。你可以扩展和收起它们。
  6. 状态栏(status bar)显示你的项目和 IDE 自己的状态,同时也包括有任何的警告或者消息。

你可以通过移动或者隐藏工具窗口来对主窗口进行组织来让你获得更多的编辑空间。你也可以使用键盘快捷键来访问绝大部分的 IDE 特性和功能。

任何时候你都可以在你的用户界面对所有代码,数据库,行为,元素中进行查找。你可以通过双击 shift 按钮或者使用鼠标单击右上角的查找图标来打开全局查找功能。这个在项目的编辑中非常有用,例如你希望创建一个本地的 IDE 行为,但是你忘记了如何去触发它们,你可以通过这样来找到相关的代码。

工具窗口(Tool Windows)

替代使用预设的视图,Android Studio 根据你的当前编辑的和操作的项目自动载入相关的工具窗口。在默认情况下,最常用的工具窗口将会固定在应用窗口的边缘进行显示。

  • 希望扩展和收缩工具窗口,单击工具窗口栏中工具的名字就可以了。你可以对工具窗进行拖动,固定,取消固定粘合,取消粘合。
  • 如果希望返回当前默认的工具窗布局,单击 Window > Restore Default Layout 或者通过单击 Window > Store Current Layout as Default 来返回你的默认的工具窗布局。
  • 希望显示或者隐藏整个工具窗口栏,单击在 Android Studio 窗口左角的窗口图标  就可以了。
  • 希望找到特定的工具窗,将鼠标移动到窗口图片上然后选择工具窗的菜单。

你也可以通过键盘的快捷键来打开工具窗。Table 1 列出了最常用窗口的快捷键。

Table 1. 一些有用的工具窗快捷键

工具窗Windows and LinuxMac
项目(Project)Alt+1Command+1
版本控制(Version Control)Alt+9Command+9
运行(Run)Shift+F10Control+R
调试(Debug)Shift+F9Control+D
安卓监控(Android Monitor)Alt+6Command+6
返回到编辑(Return to Editor)EscEsc
隐藏所有工具窗(Hide All Tool Windows)Control+Shift+F12Command+Shift+F12

如果你希望隐藏所有的工具栏,工具创建和编辑标签,单击 View > Enter Distraction Free Mode。这个将会启用非娱乐模式(Distraction Free Mode)。希望退出 Distraction Free Mode 模式,单击 View > Exit Distraction Free Mode 就可以了。

你可以使用快速查找(Speed Search)功能来查找 Android Studio 中几乎所有的工具窗口。在使用快速查找功能的时候,选择工具窗然后输入你需要查找的字符串就可以了。

有关更多的快捷方式,请参考键盘快捷键(Keyboard Shortcuts)。

代码完成

Android Studio 有 3 种类型的代码完成功能,你可以通过键盘快捷键来使用者几种方法。

Table 2. 代码自动完成的键盘快捷键

类型(Type)描述(DescriptionWindows and LinuxMac
基本自动完成(Basic Completion)

对变量,类型,方法,表达式等显示基本的代码建议。

如果你在同一行中调用基本自动完成 2 次,你将会看到更多的结果包括私有成员,没有导入的静态成员。

Control+SpaceControl+Space
智能自动完成(Smart Completion)

基于上下文显示相关的选项。智能自动完成知道期待的类型和数据流。

如果你在同一行中调用基本自动完成 2 次,你将会看到更多的结果包括链,

Control+Shift+SpaceControl+Shift+Space
声明自动完成(Statement Completion)为你完成当前的声明,自动为你添加括号,大括号,格式化等。Control+Shift+EnterShift+Command+Enter

你可以通过单击 Alt+Enter 来使用快速修正功能和显示行为的方向。

有关代码自动完成的更多功能,请参考代码完成(Code Completion)页面。

找到示例代码

在  Android Studio 中可以帮助你可以找到高质量,Google 提供的 Android 示例代码。这示例代码可以在代码浏览器中找到。这些代码是基于你当前项目的高亮标记进行查找和匹配的。有关更多信息,请参考找到示例代码(Find Sample Code)。


Navigation

Here are some tips to help you move around Android Studio.

  • Switch between your recently accessed files using the Recent Files action. Press Control+E (Command+E on a Mac) to bring up the Recent Files action. By default, the last accessed file is selected. You can also access any tool window through the left column in this action.
  • View the structure of the current file using the File Structure action. Bring up the File Structure action by pressing Control+F12 (Command+F12 on a Mac). Using this action, you can quickly navigate to any part of your current file.
  • Search for and navigate to a specific class in your project using the Navigate to Class action. Bring up the action by pressing Control+N(Command+O on a Mac). Navigate to Class supports sophisticated expressions, including camel humps, paths, line navigate to, middle name matching, and many more. If you call it twice in a row, it shows you the results out of the project classes.
  • Navigate to a file or folder using the Navigate to File action. Bring up the Navigate to File action by pressing Control+Shift+N (Command+Shift+O on a Mac). To search for folders rather than files, add a / at the end of your expression.
  • Navigate to a method or field by name using the Navigate to Symbol action. Bring up the Navigate to Symbol action by pressing Control+Shift+Alt+N (Command+Shift+Alt+O on a Mac).
  • Find all the pieces of code referencing the class, method, field, parameter, or statement at the current cursor position by pressing Alt+F7.


Style and Formatting

As you edit, Android Studio automatically applies formatting and styles as specified in your code style settings. You can customize the code style settings by programming language, including specifying conventions for tabs and indents, spaces, wrapping and braces, and blank lines. To customize your code style settings, click File > Settings > Editor > Code Style (Android Studio > Preferences > Editor > Code Style on a Mac.)

Although the IDE automatically applies formatting as you work, you can also explicitly call the Reformat Code action by pressing Control+Alt+L(Opt+Command+L on a Mac), or auto-indent all lines by pressing Control+Alt+I (Alt+Option+I on a Mac).

Figure 4. Code before formatting.

Figure 5. Code after formatting.

Version Control Basics

Android Studio supports a variety of version control systems (VCS’s), including Git, GitHub, CVS, Mercurial, Subversion, and Google Cloud Source Repositories.

After importing your app into Android Studio, use the Android Studio VCS menu options to enable VCS support for the desired version control system, create a repository, import the new files into version control, and perform other version control operations:

  1. From the Android Studio VCS menu, click Enable Version Control Integration.
  2. From the drop-down menu, select a version control system to associate with the project root, and then click OK.

The VCS menu now displays a number of version control options based on the system you selected.

Note: You can also use the File > Settings > Version Control menu option to set up and modify the version control settings.


Gradle 构建系统


Android Studio uses Gradle as the foundation of the build system, with more Android-specific capabilities provided by the Android plugin for Gradle. 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 do the following:

  • Customize, configure, and extend the build process.
  • Create multiple APKs for your app, with different features using the same project and modules.
  • Reuse code and resources across sourcesets.

By employing the flexibility of Gradle, you can achieve all of this without modifying your app's core source files. Android Studio build files are namedbuild.gradle. They are plain text files that use Groovy syntax to configure the build with elements provided by the Android plugin for Gradle. Each project has one top-level build file for the entire project and separate module-level build files for each module. When you import an existing project, Android Studio automatically generates the necessary build files.

To learn more about the build system and how to configure, see Configure Your Build.


Build Variants

The build system can help you create different versions of the same application from a single project. This is useful when you have both a free version and a paid version of your app, or if you want to distribute multiple APKs for different device configurations on Google Play.

For more information about configuring build variants, see Configuring Gradle Builds.


Multiple APK Support

Multiple APK support allows you to efficiently create multiple APKs based on screen density or ABI. For example, you can create separate APKs of an app for the hdpi and mdpi screen densities, while still considering them a single variant and allowing them to share test APK, javac, dx, and ProGuard settings.

For more information about multiple APK support, read Build Multiple APKs.


Resource Shrinking

Resource shrinking in Android Studio automatically removes unused resources from your packaged app and library dependencies. For example, if your application is using Google Play services to access Google Drive functionality, and you are not currently using Google Sign-In, then resource shrinking can remove the various drawable assets for theSignInButton buttons.

Note: Resource shrinking works in conjunction with code shrinking tools, such as ProGuard.

For more information on shrinking code and resources, see Shrink Your Code and Resources.


Managing Dependencies

Dependencies for your project are specified by name in the build.gradle file. Gradle takes care of finding your dependencies and making them available in your build. You can declare module dependencies, remote binary dependencies, and local binary dependencies in your build.gradle file. Android Studio configures projects to use the Maven Central Repository by default. (This configuration is included in the top-level build file for the project.) For more information about configuring dependencies, read Configure Build Variants.


Debug and Profile Tools


Android Studio assists you in debugging and improving the performance of your code, including inline debugging and performance analysis tools.


Inline debugging

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:

  • Inline variable values
  • Referring objects that reference a selected object
  • Method return values
  • Lambda and operator expressions
  • Tooltip values

Figure 6. An inline variable value.

To enable inline debugging, in the Debug window, click Settings and select the checkbox for Show Values Inline.


Performance monitors

Android Studio provides performance monitors so you can more easily track your app’s memory and CPU usage, find deallocated objects, locate memory leaks, optimize graphics performance, and analyze network requests. With your app running on a device or emulator, open the Android Monitor tool window, and then click the Monitors tab.

For more information about performance monitors, see Android Monitor.


Heap dump

When you’re monitoring memory usage in Android Studio, you can simultaneously 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.

For more informatin about working with heap dumps, see Dumping and Analyzing the Java Heap.


Allocation tracker

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 optimize your app’s performance and memory use by adjusting the method calls related to those actions.

For information about tracking and analyzing allocations, see Allocation Tracker.


Data file access

The Android SDK tools, such as Systracelogcat, and Traceview, generate performance and debugging data for detailed app analysis.

To view the available generated data files, open the Captures tool 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 the standard .hprof file format.


Code inspections

Whenever you compile your program, Android Studio automatically runs configured Lint and other IDE inspections to help you easily identify and correct problems with the structural quality of your code.

The Lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization.

Figure 7. The results of a Lint inspection in Android Studio.

In addition to Lint checks, Android Studio also performs IntelliJ code inspections and validates annotations to streamline your coding workflow.

For more information, see Improving Your Code with Lint and lint tool.


Annotations in Android Studio

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 Support-Annotations library in the Android Support Repository for use with Android Studio. Android Studio validates the configured annotations during code inspection.

For more details about Android annotations, see Improving Code Inspection with Annotations.


Log messages

When you build and run your app with Android Studio, you can view adb output and device log messages (logcat) by clicking Android Monitor at the bottom of the window.

If you want to debug your app with the Android Device Monitor, you can launch the Device Monitor by clicking Tools > Android > Android Device Monitor. The Device 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 tool to help optimize your layouts.

  • No labels