转载自:http://ask.android-studio.org/?/question/789
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.
<翻译> 本章将引导您完成安装和设置开发环境,然后你就可以跟随本书的例子和课程学习。首先,您将安装被称为Java开发工具包(JDK)的必要组件。然后你要下载和安装Android Studio以及Android软件开发工具包(SDK),这些都是开发Android应用程序所必需的工具。接着,我们将向你展示如何使用新建项目向导来创建一个简单的项目HelloWorld。最后,我们将向你展示怎样连接Android虚拟设备(AVD)和Android物理设备。在本章结束的时候,你将在Android Studio中拥有开发Android应用程序的一切东西。
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:www.oracle.com/technetwork/jav ... .html
When you land on this page, click the Java Download button, shown in Figure 1-1.
<翻译> 本节适用于Windows用户。如果你是Mac用户,请跳转到下一节“在Mac上安装JDK”。Android Studio 使用java编译环境构建,因此在开始使用Android Studio之前你需要确保你已经安装Java开发工具包(JDK)。如果你是一个经验丰富Android或Java开发人员的话,你很有可能已经在你的电脑上安装了JDK。如果你的电脑上的JDK运行版本是1.6或更高的话,你可以跳过这一节。此外,你将下载,安装和配置最新版本的JDK。你可以从下面的Oracle网站上下载JDK:
www.oracle.com/technetwork/jav ... .html
打开网站之后,点击java Download 按钮,如图1-1所示。
![](http://ask.android-studio.org/uploads/questions/20151219/c25e50eea7dbebdf981dbe4dde00eb71.jpg)
Figure 1-1. The Java Download button on the Java Downloads page
<翻译> 图1-1 Java下载界面上的Java Download 按钮
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.<翻译> 下一步如图1-2所示,需要单击单选按钮来接受许可证协议。然后选择适合你操作系统的JDK。如果你是Windows 7或者Windows 8系统,你应该点击文件链接右边窗口的x64版本,如图1-2所示。Oracle会频繁的发布JDK更新版本。在本书印刷之际,新版本的JDK基本能够使用了,所以请下载最新版本的JDK。然后等待安装文件下载。文件大小通常约125MB,所以下载应该花不了多长时间。
Figure 1-2. Accept the license agreement and click the appropriate link for Windows
<翻译> 图1-2 接受许可协议,点击适当的链接
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.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.
<翻译> 安装JDK之前,在你C盘的根目录下新建一个名为Java的文件夹。文件夹的名字可以是任意的,之所以称为Java是因为有许多跟Java相关的工具要安装在这里,包括JDK,Android Studio和Android SDK。将Android Studio相关的工具安装在C:\Java目录下能够保证你的开发环境更有条理。
找到你浏览器下载安装文件的位置然后双击执行。当安装开始,你将看到安装向导,如图1-3所示。在Windows系统下,JDK默认安装在C:\Program Files\Java\目录下。要更改安装目录的位置,请单击Change按钮。我们建议安装JDK字 C:\Java目录下是因为它的路径名中不包含空格,很容易记住。如图1-4所示。
Figure 1-3. Installation Wizard for the JDK on Windows
<翻译> 图1-3 JDK安装向导窗口
Figure 1-4. Select the JDK installation directory
<翻译> 图1-4 选择JDK安装目录
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的安装位置。按照提示,直到安装完成。如果提示安装Java运行环境(JRE),选择跟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,按住Windows键和Pause键打开系统窗口。单击高级系统设置选项,如图1-5所示。
Figure 1-5. Windows System window
<翻译> 图1-5 Windows系统窗口
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所示。如图1-7所示,在下面的系统变量列表中找到JAVA_HOME项。如果JAVA_HOME不存在,点击新建来创建,否则,点击编辑。
Figure 1-6. System properties
<翻译> 图1-6 系统属性
Figure 1-7. Environmental variables
<翻译> 图1-7 环境变量
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
<翻译> 图1-8 编辑JAVA_HOME环境变量
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环境变量一样,你需要编辑PATH环境变量。如图1-9所示。将你的光标移动到变量值的最后面然后输入如下路径:
;%JAVA_HOME%\bin
Figure 1-9. Edit the PATH environmental variable
<翻译> 图1-9 编辑PATH环境变量
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. In the command-line window, issue the following command and press Enter:
<翻译> 为了测试新的JDK安装成功,单击开始按钮,输入cmd,然后回车,打开命令行窗口。在命令行窗口中输入以下命令并按回车:
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
<翻译> 图1-10 确认JDK安装成功
Installing the Java Development Kit on Mac
在Mac上安装Java开发工具包
The first two steps in installing the JDK for Mac and Windows are identical. Point your browser to the following site:www.oracle.com/technetwork/jav ... .html
When you land on this page, click the Java Download button, shown in Figure 1-11.
<翻译> Mac和Windows安装JDK的前两个步骤是相同的。用浏览器打开如下网站:
www.oracle.com/technetwork/jav ... .html
在界面上点击Java Download按钮,如图1-11所示。
Figure 1-11. The Java Download button on the Java Downloads page
<翻译> 图1-11 Java Download页面上的Java Download按钮
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所示,点击接受许可协议单选按钮接受许可协议。然后你必须选择适合你操作系统的JDK版本。如果你电脑是64位的OS X,你应该点击右侧的Mac OS X64标签,如图1-12所示。Oracle会频繁的发布JDK更新版本。在本书印刷之际,新版本的JDK基本能够使用了,所以请下载最新版本的JDK。等待安装文件下载。
Figure 1-12. Accept the license agreement and click the appropriate link for Mac
<翻译> 图1-12 接受许可协议,点击适当的链接
Executing the JDK Wizard on Mac
Mac上执行JDK向导
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
<翻译> 图1-13 JDK 8 Update 25.pkg
Figure 1-14. Installation Wizard
<翻译> 图1-14 安装向导
Figure 1-15. Installation success
<翻译> 图1-15 安装成功
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。打开Finder窗口然后选择应用➤实用工具。从那里打开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 for you OS’ 绿色大按钮,如图1-16所示。接着,勾选我已阅读并同意上述条款条件复选框。再次点击‘Download Android studio’ 按钮,安装文件将开始下载。下载完成之后执行下载文件。
Figure 1-16. Download Android Studio
<翻译> 图1-16 下载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.
<翻译> 安装向导开始之后,点击Next按钮一路向下直到选择组件窗口。勾选全部组件复选框如图1-17所示。然后点击Next。再次同意条款和条件。当你到达Configuration Settings:Install Locations窗口时如图1-18所示,选择Android Studio 和Android SDK的安装位置。为了保持一致,我们将Android Studio安装在C:\ Java \ astudio \下,将Android SDK安装在C:\ Java \ asdk \下。
Figure 1-17. Choose components
<翻译> 图1-17 选择组件
Figure 1-18. Select locations for Android Studio and the SDK
<翻译> 图1-18 选择Android Studio和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所示。勾选Start Android Studio复选框之后点击Finish按钮启动Android Studio。请确保选中复选框,然后点击Finish按钮,Android Studio才会启动。请注意从现在起,你需要从桌面图标或开始菜单中启动Android Studio。
Figure 1-19. Completing the Android Studio setup
<翻译> 图1-19 完成Android Studio设置
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.
<翻译> Android Studio第一次打开的时候,安装向导将查找你系统上的JDK(如前面安装的一个)和Android SDK的位置。安装向导应该为你的Android Studio 下载一切开始开发应用程序的东西。点击Finish按钮关闭安装向导。
Figure 1-20. Setup Wizard – Downloading Components
<翻译> 图1-20 安装向导-下载组件
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.<翻译> 一旦安装向导完成,Android Studio的欢迎对话框将会出现,如图1-21所示。点击Start a new Android Studio project选项。
Figure 1-21. Welcome to Android Studio
<翻译> 图1-21 欢迎来到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.
<翻译> 在新建项目向导(如图1-22),在Application Name输入HelloWorld,在Company Domain 中输入gerber.apress.com。请注意,Package name是反转的Company Domain加上Application Name。创建HelloWorld项目在课程的根目录文件夹下。正如前面提到的,如果是Windows系统,我们使用C:\androidBook\。如果是Mac或者Linux系统,你的根目录名称不是以字母开头,而是一个斜杠。
Figure 1-22. Configure your new project
<翻译> 图1-22 配置你的新项目
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。同意这些设置然后点击Next,如图1-23所示。
Figure 1-23. Select the form factors your app will run on
<翻译> 图1-23 选择APP运行的形式
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. They should be the following:
<翻译> 后续新项目向导将提示你选择一种布局。选择Blank Activity然后点击Next按钮。同意默认名字,如图1-24所示,他们应该如下:
Activity Name: MainActivity
Layout Name: activity_main
Title: MainActivity
Menu Resource Name: menu_main
Figure 1-24. Choose options for your new file
<翻译> 图1-24 为新文件选择设置
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.<翻译> Android虚拟设备管理器允许你创建Android虚拟设备(AVDs),然后你可以在你的电脑上运行模拟器。模拟和仿真有一个很重要但是微妙的区别。模拟意味着虚拟设备只有一个外形,模拟实际的物理设备如何运作,但是不针对特定的操作系统。IOS开发环境使用模拟器,对于有限数量的设备的平台的IOS来说可能是一个不错的选择。
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 Studio使用仿真器,这意味着Android虚拟设备管理器启动一个 Linux内核的大沙箱和整个Android栈为了仿真基于Android物理设备的环境。尽管仿真器提供了一个比模拟器更可靠的环境来测试你的应用程序,但是启动一个AVD需要数分钟,这取决于你电脑的速度。好消息是你的仿真器仍然活跃在内存中,它仍然是有响应的。然而,如果你有Android手机或者平板电脑,我们建议使用物理设备来测试你的应用程序,而不是使用AVD。也就是说,我们首先使用Android虚拟设备管理器创建一个AVD,在后来的章节我们将想你展示如何连接你的物理设备,如果你有的话。
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.
<翻译> 点击Android虚拟设备管理器图标如图1-25所示。在Android虚拟设备管理器向导的第一个界面中点击创建虚拟设备按钮。在下一个界面选择Galaxy Nexus如图1-26,然后点击Next。下一个界面允许你选择一个系统镜像如图1-27。选择第一个选项Lollopop(或最新的API)和x86_64的ABI。点击Next。在下一个界面上,点击Finish按钮来验证你的AVD设置。恭喜你,你已经创建了一个新的AVD。
Figure 1-25. AVD icon
<翻译> 图1-25 AVD图标
Figure 1-26. Select the Galaxy Nexus hardware
<翻译> 图1-26 选择Galaxy Nexus硬件
Figure 1-27. Select the x86_64 system image
<翻译> 图1-27 选择 x86_64系统镜像
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. 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 Studio还没有设备定义的设备,我们建议你去 phonearena.com 查找你的型号。在那里你会找到技术规格,你可以用来创建一个新设备的定义。在你创建好了新设备的定义之后,使用相同的步骤来创建一个新的AVD。有一个优秀的第三方市场的Android模拟器叫Genymotion。Genymotion模拟器对非商业目的用户是免费的而且用起来不错。解释如何设置和使用Genymotion超出了本书的范围,但是你可以从genymotion.com上下载Genymotion模拟器。
Running HelloWorld on an AVD
在AVD上运行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.<翻译> 在新创建的AVD中运行HelloWorld应用程序,单机工具栏上的绿色运行按钮,如图1-28.
Figure 1-28. Run button
<翻译> 图1-28 运行按钮
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.
<翻译> 确保Launch Emulator单选按钮被选中,然后选择Nexus API 21的组合框。点击OK如图1-29所示.耐心等待几分钟启动AVD。然后你应该能够看到HelloWorld应用程序运行在你的电脑模拟器上。如图1-30所示。
Figure 1-29. Choosing a device and launching the emulator
<翻译> 图1-29 选择设备开启模拟器
Figure 1-30. Emulator screenshot
<翻译> 图1-30 模拟器截图
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.<翻译> 如前面所说,尽管AVDs对于模拟特定的设备是有用的,尤其是那些你手头没有的设备,但是在物理设备上开发应用程序是更可取的。如果你的电脑不能通过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.
<翻译> 你可以在 developer.android.com/tools/extras/oem-usb.html# 上找到合适的USB驱动程序,或者使用你最喜欢的搜索引擎找到适合你型号的USB驱动程序。下载驱动程序并将之安装在电脑上。在你的Android设备上,点击设置,然后打开开发人员选项。确保USB调试检查框被选中。一些设备,如三星需要密码来启用USB调试,所以你可能要使用你最喜欢的搜索引擎研究如何打开你设备上的USB调试开关。如果介绍不太清晰,YouTube上一些视频资源也可以帮助你如何开启特定设备的USB调试开关。
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.
<翻译> 大多数的Android设备配备有一根一端有一个USB接口另一端有一个小型USB接口的数据线。你的Android设备通过这根数据线连接到你的电脑上。点击在图1-31中圈起来的Android 设备监听器按钮。如果驱动程序安装正确,你应该能够看到列出来的已连接设备如图1-32.
Figure 1-31. Android Device Monitor button
<翻译> 图1-31 Android设备监听器按钮
Figure 1-32. Android Device Monitor screen showing the connected physical device
<翻译> 图1-32 Android设备监听器显示已连接的物理设备
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设备之间的连接是使用了一种叫做Android调试桥(ADB)的服务。如果你没有看到设备,点击IDE左下角的Terminal按钮,输入以下命令:
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.
<翻译> 现在点击绿色运行按钮(如图1-28所示)。选择已连接的Android设备。在图1-33中,已连接的设备是一台HTC One X智能手机。点击OK,等待几秒钟,你将看到HelloWorld运行在你的Android设备上了。
Figure 1-33. The Choose Device screen with the physical Android device listed
<翻译> 图1-33 在Android物理设备列表中选择设备
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.<翻译> 在这一章,你安装了Java开发工具包,或称JDK,你也安装了Android Studio和Android SDK。你用新建项目向导创建一个简单的HelloWorld应用程序。然后你新建一个Android虚拟设备,或称AVD。我们向你介绍了如何安装所需的USB驱动程序。最后,我们向你展示了怎样在AVD和Android物理设备上运行HelloWorld。现在,你在Android Studio中拥有开发Android应用程序的所有必需的软件。