? ? ? ? ? ? ? ? ? ? ? ? ? ? ?第一章 介紹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下載按鈕進行下載,如圖所示:
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大,因此,下載應該不會花費很長的時間。
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:
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鍵打開系統窗口。點擊高級系統設置,如圖所示:
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,點擊新建來創建它。之后,點擊編輯。
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所示:
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
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已經正確的安裝了。
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所示:
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的更新版本。這本書出版的時候,可能會發布更新的版本,所以要確保你下載的是最新的版本。等待文件下載完畢。
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文件來運行安裝文件?,F在點擊.pkg文件開始安裝向導,然后按照要求繼續安裝,就像圖1-13到圖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,打開查找窗口,然后選擇應用>實用工具,從哪里開始,打開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再一次為你的操作系統下載對應版本,這個時候你的安裝文件應該已經開始下載了。一旦下載完成了,運行你下載的文件。
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\.
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。
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開發應用所必須的所有東西。單擊結束按鈕來退出安裝向導。
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選項。
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,你的實驗父目錄名將不能用字母開頭,寧可是一個斜杠。
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。接受這些設置,然后單擊下一步。如圖所示:
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
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虛擬機。
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(運行)按鈕開始運行。
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應用正在運行。
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所示:
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設備上運行。
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應用開發所必須的所有東西。