[Learn Android Studio 汉化教程]第一章 : Android Studio 介绍_android virtual device汉化
作者:菜鸟追梦旅行 | 2024-03-06 13:15:21
赞
踩
android virtual device汉化
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.
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.
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.
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.
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.
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.
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.
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.
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:
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:
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。
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.
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.
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.
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.
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按钮关闭安装向导。
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.
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.
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:
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.
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.
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.
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.
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.
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.
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.
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:
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.
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.