Chapter 1 Introducing Android Studio

                             第一章 介绍Android Studio

This chapter walks you through installing and setting up your development environment so you can follow the examples and labs in this book. First, you will install an essential prerequisite component called the Java Development Kit (JDK). Then you will download and install Android Studio as well as the Android Software Development Kit (SDK), which is a suite of software tools required to build Android apps. We will show you how to use the New Project Wizard to create a simple project called HelloWorld. Last, we will show you how to establish a connection to both an Android Virtual Device (AVD) and a physical Android device. By the end of this chapter, you will have everything you need to start developing apps in Android Studio.

这一章将会带你安装、设置你的开发环境,所以你可以跟着这本书的例子一起做。首先你必须安装一个至关重要的组件——JDK(java开发工具包);然后你还要下载安装Android Studio和SDK(安卓软件开发工具包)——开发安卓应用的一系列必软件工具。我们将会向你展示怎样使用新工程向导来创建一个简单的工程——HelloWorld。最后我们将向你展示怎样将安卓物理机和虚拟机连接起来。在这一章的最后,你将拥有开发安卓应用的所有东西。

Installing the Java Development Kit on Windows

在windows平台上安装JDK


This section pertains to Windows users. If you’re a Mac user, skip ahead to the section titled “Installing the Java Development Kit on Mac.” Android Studio uses the Java tool chain to build, so you need to make sure that you have the Java Development Kit (JDK) installed on your computer before you start using Android Studio. It’s quite possible that you already have the JDK installed on your computer, particularly if you’re a seasoned Android or Java developer. If you already have the JDK installed on your computer, and you’re running JDK version 1.6 or higher, then you can skip this section. However, you may want to download, install, and configure the latest JDK anyway. You can download the JDK from the following Oracle site:

这一部分是针对于windows用户的。如果你是一个mac用户,直接跳到“Installing the Java Development Kit on Mac.”这一部分。Android Studio使用Java工具链来进行开发。因此你要确保在你使用Android Studio之前,你的电脑上有JDK。如果你是一名经验丰富的Android开发者或者是一名老练的java开发者,很有可能你的电脑上已经安装好了JDK。如果你的电脑上已经安装好了JDK,并且你的JDK版本是1.6或者更高的版本,你可以跳过这部分。然而,不管怎样,你可能想要下载、安装并且配置最新的JDK。你可以从下面的站点下载JDK:

www.oracle.com/technetwork/java/javase/downloads/index.html

When you land on this page, click the Java Download button, shown in Figure 1-1.        当你进入下图中的这个页面时,点击java下载按钮进行下载,如图所示:

Figure 1-1. The Java Download button on the Java Downloads page

Downloading the JDK on Windows

在windows上下载JDK

The next step in the installation, shown in Figure 1-2, requires that you accept a license agreement by clicking the Accept License Agreement radio button. Then you must choose the appropriate JDK for your operating system. If you’re running Windows 7 or Windows 8, you should click the file link to the right of the Windows x64 label, also shown in Figure 1-2. 

Oracle makes frequent release updates to the JDK. By the time this book goes to press, a newer version of the JDK will almost certainly be available, so please be sure to download the latest version. Wait for the installation file to download. This file is usually around 125MB, so the download shouldn’t take long.

如图所示,安装的下一步就是要求你接受一个许可,你只需要点击接受许可按钮就可以了。然后你必须选择一个适合你的操作系统的JDK进行下载。如果你的电脑是windows7或者是windows8,你应该选择windows X86的这一项。甲骨文公司对JDK会进行比较频繁的更新,在这本书出版的时候,可能会提供更新的版本。因此要确保下载的是最新的版本。等待安装文件下载完毕,这个文件通常有215M大,因此,下载应该不会花费很长的时间。

Figure 1-2. Accept the license agreement and click the appropriate link for Windows

Executing the JDK Wizard on Windows

在windows上运行JDK安装向导

Before you install the JDK, create a directory in the root of your C: drive called Java. The name of this directory is arbitrary, though we call it Java because many of the tools we are going to install here are related to Java, including the JDK, Android Studio, and the Android SDK. Consistently installing the tools related to Android Studio in the C:\Java directory also keeps your development environment organized.

在你安装JDK之前,在你C盘的根目录下创建一个名叫Java的目录。目录的名字是任意的,之所以要命名为Java是因为我们将会在这个目录下安装很多和Java有关的工具,包括JDK,Android Studio和Android SDK。始终在C:\Java目录下安装各种各样的工具,也会确保你的开打环境条理清晰。

Navigate to the location where your browser downloaded the installation file and execute that file by double-clicking it. Once the installation begins, you will be presented with the Installation Wizard, shown in Figure 1-3. In Windows, the JDK installer defaults to C:\Program Files\Java\. To change the installation directory location, click the Change button. We recommend installing your JDK in the C:\Java directory because it contains no spaces in the path name and it’s easy to remember. See Figure 1-4.

找到你下载的文件,然后双击运行。一旦开始安装,就会出现像图中那样的安装向导。在windows中,JDK的默认安装路径是C:\Program Files\Java\。要想改变安装路径,点击Change按钮。我们建议你把JDK安装在C:\Java目录下,因为在路径名中没有空格,并且很容易记忆。看图1-4:

Figure 1-3. Installation Wizard for the JDK on Windows


Figure 1-4. Select the JDK installation directory

Make a note of where you are installing your JDK. Follow the prompts until the installation is complete. If prompted to install the Java Runtime Edition (JRE), choose the same directory where you installed the JDK.

记住你的JDK安装目录。按照提示,一步一步进行,直到安装完成。如果提示你安装JRE(Java Runtime Edition),选择安装目录要跟JDK的安装相同。

Configuring Environmental Variables on Windows

在windows中配置环境变量


This section shows you how to configure Windows so that the JDK is found by Android Studio. On a computer running Windows, hold down the Windows key and press the Pause key to open the System window. Click the Advanced System Settings option, shown in Figure 1-5.

这一部分将会引导你配置windows,这是为了能保证Android Studio能找到JDK。在键盘上按住windows键,再按Pause键打开系统窗口。点击高级系统设置,如图所示:

Figure 1-5. Windows System window

Click the Environmental Variables button, shown in Figure 1-6. In the System Variables list along the bottom, shown in Figure 1-7, navigate to the JAVA_HOME item. If the JAVA_HOME item does not exist, click New to create it. Otherwise, click Edit.

点击环境变量,像图1-6所示。在系统变量列表中找到JAVA_HOME。如果没有JAVA_HOME,点击新建来创建它。之后,点击编辑。

Figure 1-6. System properties


Figure 1-7. Environmental variables

Clicking either New or Edit displays a dialog box similar to Figure 1-8. Be sure to type JAVA_HOME in the Variable Name field. In the Variable Value field, type the location where you installed the JDK earlier (less any trailing slashes), as shown in Figure 1-4. Now click OK.

点击新建或者编辑之后将会出现如图1-8所示的对话框。确保在变量名的区域键入JAVA_HOME。在变量值的区域键入你的JDK的安装路径(注意末尾没有斜杠),之后点击确认。如图1-4所示:

Figure 1-8. Edit the JAVA_HOME environmental variable

Just as you did with the JAVA_HOME environmental variable, you will need to edit the PATH environmental variable. See Figure 1-9. Place your cursor at the end of the Variable Value field and type the following:                                                        ;%JAVA_HOME%\bin

就像你编辑JAVA_HOME环境变量一样,你也需要编辑PATH环境变量。如图1-9所示,在变量值的末尾添加下面的内容(注意开头的分号不能丢):

;%JAVA_HOME%\bin

Figure 1-9. Edit the PATH environmental variable

Now click OK, OK, OK to accept these changes and back out of the system properties. To test that the new JDK has been installed properly, pull up a command line by clicking the Start button, typing cmd, and then pressing Enter.

点在点击确定,来保证所有的设置生效。为了测试JDK是否正确的安装,点击windows按钮,键入cmd然后回车。

In the command-line window, issue the following command and press Enter:

在命令行中键入下面的命令,并且回车:

java –version

If you get a response like the one shown in Figure 1-10, congratulations. You just installed the JDK properly.

如果你得到如图1-10所示的反馈,那么祝贺你,你的JDK已经正确的安装了。

Figure 1-10. Confirm the proper JDK installation

Installing the Java Development Kit on Mac

在Mac上安装JDK

The first two steps in installing the JDK for Mac and Windows are identical. Point your browser to the following site:

安装JDK的前两步windows和mac是完全一样的。用你的浏览器访问下面的站点:

www.oracle.com/technetwork/java/javase/downloads/index.html

When you land on this page, click the Java Download button, shown in Figure 1-11.

当你进入这个页面的时候,点击Java下载按钮,如图1-11所示:

Figure 1-11. The Java Download button on the Java Downloads page

Downloading the JDK on Mac

在Mac上下载JDK

Accept the license agreement, shown in Figure 1-12, by clicking the Accept License Agreement radio button. Then you must choose the appropriate JDK for your operating system. If you’re running a 64-bit version of OS X, you should click the file link to the right of the Mac OS X64 label, also shown in Figure 1-12. Oracle makes frequent release updates to the JDK. By the time this book goes to press, a newer version of the JDK will almost certainly be available, so please be sure to download the latest version. Wait for the installation file to download.

如图1-12所示,单击Accept License Agreement按钮来接受许可协议。之后你必须选择和你的操作系统相匹配的JDK。如果你的电脑是64位的OS X,你应该选择Mac OS X64进行下载,就像图1-12所显示的那样。甲骨文会频繁的发布JDK的更新版本。这本书出版的时候,可能会发布更新的版本,所以要确保你下载的是最新的版本。等待文件下载完毕。


Figure 1-12. Accept the license agreement and click the appropriate link for Mac

Executing the JDK Wizard on Mac

Double-click the .dmg file to execute it. Now click the .pkg file to begin the wizard and click Continue as required, as shown in Figures 1-13 through 1-15.

双击.dmg文件来运行安装文件。现在点击.pkg文件开始安装向导,然后按照要求继续安装,就像图1-13到图1-15显示的那样。

Figure 1-13. JDK 8 Update 25.pkg


Figure 1-14. Installation Wizard


Figure 1-15. Installation success

Configuring the JDK Version on Mac

在Mac上配置JDK

To configure your Mac so that the proper JDK is found by Android Studio, open a Finder window and choose Applications ➤ Utilities. From there, open Java Preferences and, as instructed, drag the new version to the top of the list so it is recognized as the preferred version.

配置你的Mac是为了Android Studio能够准确的找到JDK,打开查找窗口,然后选择应用>实用工具,从哪里开始,打开Java首选项,就像介绍的那样,把这个新版本放到列表的顶部,这是为了把它作为优先的版本。

Installing Android Studio

安装Android Studio

Before you begin downloading Android Studio, create a labs parent directory for the labs you will create in this book. We use C:\androidBook\ as our labs’ parent directory throughout the book, but you may choose or create whatever directory you see fit. For that reason, we simply call it the labs parent directory.

在你开始下载Android Studio之前,先创建一个父目录用来实验,因为这本书中将会要求你创建这样的目录。我们使用C:\androidBook\作为我们实验的父目录,这个目录将会遍及我们这本书。但是你可能选择或者创建任何你喜欢的目录。因此,我们仅仅把它叫做实验的父目录。

Downloading Android Studio is straightforward. Point your browser to this site:

下载Android Studio很简单。用你的浏览器访问下面这个站点:

developer.android.com/sdk/installing/studio.html

Now click the large green Download Android Studio for your OS button, shown in Figure 1-16. Next, select the check box labeled I Have Read and Agree with the Above Terms and Conditions. Click Download Android Studio for your OS again, and your installation file should begin downloading. Once the download is complete, execute the file you just downloaded.

现在点击这个大大的绿色Download Android Studio按钮来下载跟你的操作系统匹配的版本,就像图1-16所显示的那样。接下来选择I Have Read and Agree with the Above Terms and Conditions这个核对标签,点击Download Android Studio再一次为你的操作系统下载对应版本,这个时候你的安装文件应该已经开始下载了。一旦下载完成了,运行你下载的文件。

Figure 1-16. Download Android Studio

After the Installation Wizard begins, move through its screens by clicking the Next buttons until you reach the Choose Components screen. There, select all the component check boxes, shown in Figure 1-17. Then click Next. Agree to the terms and conditions once again. When you reach the Configuration Settings: Install Locations screen, shown in Figure 1-18, select the locations for Android Studio and the Android SDK. To be consistent, we chose to install Android Studio in C:\Java\astudio\ and the Android SDK in C:\Java\asdk\.

安装向导开始之后,点击下一步按钮来跳过它的界面,知道进入Choose Components的界面。就像图1-17所显示的那样,选择所有的核对后的组件。然后点击下一步,再一次同意这些条款和条件。如图1-18所示,当你看到Configuration Settings这一页——安装位置页面,选择Android Studio和Android SDK的安装位置。为了保持一致,我们选择Android Studio的安装路径为C:\Java\astudio\,然后Android SDK安装在C:\Java\asdk\.

Figure 1-17. Choose components


Figure 1-18. Select locations for Android Studio and the SDK

Click through several Next buttons as you install both Android Studio and the Android SDK. You should eventually arrive at the Completing the Android Studio Setup screen, shown in Figure 1-19. The Start Android Studio check box enables Android Studio to launch after you click Finish. Make sure the check box is selected, and then go ahead and click Finish, and Android Studio will launch. Please note that from here on out, you will need to navigate to either the desktop icon or the Start menu to launch Android Studio

点击很多次的Next按钮来安装Android Studio和Android SDK。如图1-19所示,最终安装完成的时候,你应该到达Completing the Android Studio Setup这一页面。The Start Android Studio复选框可以保证你安装完毕后启动Android Studio。确保复选框时选中的,然后继续点击Finish,最后Android Studio将会启动。请记住从现在开始,你将需要从桌面图标或者开始菜单来启动Android Studio。

Figure 1-19. Completing the Android Studio setup

When Android Studio starts for the very first time, the Setup Wizard, shown in Figure 1-20, will analyze your system looking for an existing JDK (such as the one you installed earlier), as well as the location of the Android SDK. The Setup Wizard should download everything you need to begin developing apps in Android Studio. Click the Finish button to dismiss the Setup Wizard.

如图1-20所示的设置向导,当第一次启动Android Studio的时候会会分析你的系统来寻找已经存在的JDK(比如你早先安装的那个)和Android SDK的位置。设置向导应该下载你开始在Android Studio开发应用所必须的所有东西。单击结束按钮来退出安装向导。

Figure 1-20. Setup Wizard – Downloading Components

Creating Your First Project: HelloWorld

创建你的第一个工程:HelloWorld

Once the Setup Wizard is complete, the Welcome to Android Studio dialog box appears, shown in Figure 1-21. Click the Start a New Android Project option.

如图1-21所示,一旦设置向导完成之后,Welcome to Android Studio对话框就会出现。单击Start a New Android Project选项。

Figure 1-21. Welcome to Android Studio

In the New Project wizard that appears (see Figure 1-22), type HelloWorld in the Application Name field and type gerber.apress.com in the Company Domain field. Notice that the package name is the reverse company domain plus the name of the project. Install your HelloWorld project in the root of your labs parent directory. As mentioned earlier, we use C:\androidBook\ if you’re running Windows. If you’re running Mac or Linux, your labs parent directory name will not begin with a letter, but rather a forward slash.

出现的New Project wizard中(看图1-22),在Application Name(应用程序名字)区域键入HelloWorld,然后在Company Domain(公司域名)区域中键入gerber.apress.com。注意,包名是倒过来的公司域名加上工程的名字。在你实验父目录的根目录中安装你的HelloWorld工程。就像之前提到过的那样,如果你使用的是windows的话,我们使用C:\androidBook\目录。如果你使用的是mac或者是linux,你的实验父目录名将不能用字母开头,宁可是一个斜杠。

Figure 1-22. Configure your new project

The Android operating system can run on many platforms, including game consoles, televisions, watches, glasses, smartphones, and tablet computers. By default, the Phone and Tablet check box will be selected, and API-8 will be selected as the minimum SDK. Accept these settings and click Next, as shown in Figure 1-23.

Android系统可以运行在很多的平台上,包括游戏机、电视、手表、眼镜、智能手机还有平板电脑。默认的,手机和平板电脑是被选中的。API-8将会被选择作为最小的SDK。接受这些设置,然后单击下一步。如图所示:

Figure 1-23. Select the form factors your app will run on

The subsequent screen in the New Project Wizard prompts you to choose a layout. Choose Blank Activity and click the Next button. Accept the default names, as shown in Figure 1-24.

随后新工程向导将会引导你选择一个布局。选择Blank Activity然后点击Next。就像图1-24所示的那样,接受默认名。

They should be the following:

他们应该是下面这些:

Activity Name: MainActivity

Layout Name: activity_main

Title: MainActivity

Menu Resource Name: menu_main


Figure 1-24. Choose options for your new file

Using Android Virtual Device Manager

使用Android虚拟设备管理器

The Android Virtual Device Manager allows you to create Android Virtual Devices (AVDs), which you can then run to emulate a device on your computer. There’s an important but subtle distinction between simulation and emulation. Simulation means that the virtual device is merely a façade that simulates how an actual physical device might behave, but does not run the targeted operating system. The iOS development environment uses simulation, and this is probably a good choice for iOS given the limited number of devices available for that platform. With emulation, however, your computer sets aside a block of memory to reproduce the environment found on the device that the emulator is emulating. Android Studio uses emulation, which means the Android Virtual Device Manager launches a sandboxed version of the Linux kernel and the entire Android stack in order to emulate the environment found on the physical Android device. Although emulation provides a much more faithful environment on which to test your apps than simulation does, booting up an AVD can drag into the minutes, depending on the speed of your computer. The good news is that after your emulator is active in memory, it remains responsive. Nevertheless, if you have an Android phone or tablet, we recommend using the physical device to test your apps, rather than using an AVD. That said, let’s first set up an AVD using the Android Virtual Device Manager, and later in the chapter we’ll show you how to connect your physical device, if you have one.

Android虚拟设备管理器允许你创建Android模拟器,在这个模拟器中你可以在你的电脑中运行一个仿真的Android移动设备。在simulation和emulation之间有一个很重要但是却很细微的差别。simulation的意思是虚拟设备仅仅在表面上模仿实际的物理设备可能会有的行为,但是并不实际运行目标操作系统。ios开发环境使用simulation,这对于ios来说可能是一个很好的选择,因为ios对于平台可提供的设备数量是有限制的。然而对于emulation的情况是这样子的:你的计算机将会单独划分出一块内存来为正在模拟的设备提供所需要的资源开销。Android Studio使用emulation,那就意味着Android虚拟设备管理器将会启动一个linux内核的沙箱和一个Android堆,来模拟整个Android设备的环境。虽然跟simulation相比,emulation提供了更加准确可靠的环境来测试你的应用,但是启动Android虚拟机需要花费的时间会比较长一些,这取决于你的电脑的速度。好消息是你的模拟器在内存中被激活后,它将会一直保持相应。尽管如此,如果你有Android平台的手机或者平板,我们推介使用真实的机器来测试你的应用而不是仅仅使用Android徐虚拟机。那就是说,我们首先用Android虚拟设备管理器来启动一个Android虚拟机,在这一章的后面我们将会教你如何连接你的真实设备,当然,前提是你要有一台Android设备。

Click the Android Virtual Device Manager icon encircled in Figure 1-25. On the first screen of the Andriod Virtual Device Manager Wizard, click the Create Virtual Device button. On the next screen, shown in Figure 1-26, choose Galaxy Nexus and click Next. The next screen, shown in Figure 1-27, allows you to select a system image. Select the first option for Lollipop (or the latest API) with an ABI of x86_64. Click Next. On the next screen, click the Finish button to verify your AVD settings. Congratulations, you just created a new AVD.

如图1-25所示,点击Android虚拟设备管理器的图标。在Android虚拟设备管理器向导的第一幅图片中,点击 Create Virtual Device按钮。在第二幅图中,如图1-26,选择Galaxy Nexus然后点击next。下一幅图中,如图1-27,允许你选择一个系统照片。选择第一个选项Lollipop(棒棒糖),并且ABI选择x86_64。点击Next(下一步)。下一幅图中,点击Finish(结束)按钮来核实你的AVD设置。祝贺你,你已经创建了一个Android虚拟机。

Figure 1-25. AVD icon


Figure 1-26. Select the Galaxy Nexus hardware


Figure 1-27. Select the x86_64 system image

Note The x86_64 version requires Intel hardware acceleration , which works on a limited number of Intel chip sets. If you attempt to install x86_64 and it fails, try the armeabi-vxx version instead.

笔记:x86_64版本的要求Intel的硬件加速,这个硬件加速在一定数量下的Intel芯片中起作用。如果你尝试安装x86_64并且失败了,尝试armeabi-vxx版本的。

Tip If you want to create an AVD for a device for which Android Studio does not already have a device definition, we recommend going to phonearena.com and searching for your model. There you will find technical specs, which you can use to create a new device definition. Once you create a new device definition, use the same steps to create a new AVD.

建议:如果你想要为某个设备创建一个Android虚拟机,而这个设备在Android Studio中不能找到,我们建议你去phonearena.com这个站点来寻找你的设备的模型。在那你将会找到技术规格说明书,之后你就可以创建一个新的设备模型。一旦你创建了一个新的设备模型,使用同样的步骤来创建一个新的Android虚拟机。

There is an excellent third-party Android emulator on the market called Genymotion. The Genymotion emulator is free for noncommercial purposes and performs very well. Explaining how to set up and use Genymotion is beyond the scope of this book, but you can download the Genymotion emulator from genymotion.com.

有一款第三方的非常好用的Android模拟器名叫Genymotion。Genymotion模拟器性能优良而且还是免费的。价绍如何安装和使用Genymotion已经超出了本书的范围,你可以从genymotion.com.这个站点下载Genymotion。

Running HelloWorld on an AVD

在Android虚拟机上运行HelloWorld

To run your HelloWorld app on the newly created AVD, click the green Run button on the toolbar, as shown in Figure 1-28.

如图1-28所示,在新创建的虚拟机上运行你的应用HelloWorld。点击工具栏中绿色的Run(运行)按钮开始运行。

Figure 1-28. Run button

Be sure that the Launch Emulator radio button is selected and then choose the Galaxy Nexus API 21 in the combo box. Click OK, as shown in Figure 1-29. Be patient, because launching an AVD can take a few minutes. You should now see your HelloWorld app running in a window on your computer, as shown in Figure 1-30.

确保驱动模拟器的按钮被选中,然后在组合框中选择Galaxy Nexus API21.点击OK。耐心等待,因为启动Android虚拟机需要花费一些时间。你现在就可以在你电脑的窗口中看到你的HelloWorld应用正在运行。

Figure 1-29. Choosing a device and launching the emulator


Figure 1-30. Emulator screenshot

Running HelloWorld on an Android Device

在Android设备上运行HelloWorld

As already mentioned, although AVDs are useful for emulating specific devices, particularly those that you do not have on hand, developing apps on a physical Android device is far more desirable. If your computer does not recognize your Android device when you connect it to your computer via a USB cable, you probably require a USB driver. If your computer initially recognizes your Android device, you should probably forgo installing a different or newer version of the USB driver, as this could cause the USB connection to fail.

前面已经提到过了,虽然AVD对于模拟某种特定的设备是有用的,尤其是你手上没有Android设备的时候,在具体的物理设备上开发应用是非常必要的。当你通过USB数据线将你的Android设备链接到你的计算机上的时候,你的计算机并不识别,这个时候你可能需要USB驱动。如果你的计算机只有刚开始的时候识别你的Android设备,那么你可能不能安装一个不同的或者是更新一点的USB驱动,因为这可能造成USB链接失败。

Note Mac and Linux users do not usually need to download USB drivers to establish a USB connection between their Android devices and their computers.

笔记:Mac用户和linux用户通常不需要下载USB驱动来将他们的Android设备和他们的计算机连接起来。

You can use the table at developer.android.com/tools/extras/oem-usb.html#Drivers to find the appropriate USB driver, or use your favorite search engine to find the USB driver for your model. Download the driver and install it on your computer. On your Android device, tap Settings and then Developer Options. Make sure the USB Debugging check box is selected. Some devices, such as Samsung devices, require a secret code to enable USB debugging, so you may want to use your favorite search engine to research how to enable USB debugging on your device. YouTube is also a good source of how-to videos on enabling USB debugging on your specific device if this process is not patently obvious. Most Android devices ship with a cable that has a USB male plug on one end and a micro-USB male plug on the other. Connect your Android device to your computer by using this cable. Click the Android Device Monitor button encircled in Figure 1-31. If the driver was installed properly, you should see the device listed there and connected, as shown in Figure 1-32.

你可以用电脑在developer.android.com/tools/extras/oem-usb.html#Drivers这个站点上找到与你的设备匹配的USB驱动,或者使用你最爱的搜索引擎寻找。下载驱动程序然后安装。在你的Android设备中,找到设置然后选择开发者选项。确保USB Debugging(调试)复选框被选中。一些设备,比如三星,要求一个密码才能打开USB调试,因此你可能还需要上网查找怎样打开你设备的USB调试。如果关于如何打开USB调试这个步骤不是很清晰,YouTube上有很好的视频教学资源。大多数Android设备都是用USB数据线来连接到另外一个设备上的。用USB数据线将你的Android设备连接到你的电脑上。如图1-31所示,点击Android设备监视器按钮,。如果驱动被正确的安装了,你应该能看到被连接的设备的列表。如图1-32所示:

Figure 1-31. Android Device Monitor button
Figure 1-32. Android Device Monitor screen showing the connected physical device

Note Keep in mind that the connection between your computer and your Android device is established by using a server called the Android Debug Bridge (ADB). If you don’t see the device, click the Terminal button at the lower-left corner of the IDE and issue the following command:

笔记:请记住,你的Android设备和你的计算机之间的连接时由一个名叫ADB(Android Debug Bridge)的服务器建立的。如果你看不到这个设备,点击Terminal(终端)按钮(按钮在IDE的左下角),然后键入下面的命令:

adb start-server

If after restarting the ADB server you still don’t see the device, it’s possible, though unlikely, that the USB driver requires a system reboot to take effect.

如果重启ADB服务器之后你还是不能看到这个设备,可能,虽然也许不可能,就是USB驱动要求系统重启后才能起作用。

Now click the green Run button (shown previously in Figure 1-28). Select the connected Android device. In Figure 1-33, the connected device is an HTC One X Android smartphone. Click OK, wait a few seconds, and you will see HelloWorld running on your Android device.

现在点击绿色的Run(运行)按钮,如图1-28所示。选择连接的Android设备。在途1-33中,连接 的设备是一台HTC oneX的Android智能洲手机。点击OK,等一小会儿,你就会看到HelloWorld在你的Android设备上运行。

Figure 1-33. The Choose Device screen with the physical Android device listed

Summary

摘要

In this chapter, you installed the Java Development Kit, or JDK, and you also installed Android Studio and the Android SDK. You used the New Project Wizard to create a simple app called HelloWorld. Then you created an Android Virtual Device, or AVD. We showed you how to install any required USB drivers. Finally, we showed you how to launch HelloWorld on both an AVD and a physical Android device. You should now have all the software required to begin developing Android apps in Android Studio.

在这一章中,你安装了JDK(Java开发工具包)你也安装了Android Studio还有Android SDK(Android开发工具包)。你使用New Project Wizard(新工程向导)创建了一个名叫HelloWorld的简单应用。然后你创建了一个Android虚拟机。我们向你展示了如何安装必需的USB驱动。最后,我们向你展示了怎样在你的虚拟机和你的物理设备上运行HelloWorld。现在在你的中Android Studio中,应该已经拥有Android应用开发所必须的所有东西。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,013评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,205评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,370评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,168评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,153评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,954评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,271评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,916评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,382评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,877评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,989评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,624评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,209评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,199评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,418评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,401评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,700评论 2 345

推荐阅读更多精彩内容