1. 程式人生 > >android 開發之物理裝置連線

android 開發之物理裝置連線

前面文章介紹了搭建Android開發環境,接下來就應該是真機除錯,真機除錯還算比較簡單,本文介紹Android開發機Nexus ONe除錯過程(Google官方推薦機型之一),關於安裝Android開發環境部分請參考前面文章。

本文環境:

Windows 7 + EClipse + SDK + 開發機Nexus One(下圖,圖片來源中關村)

一、下載GOOGLE USB驅動

如果是Unbuntu在配置完Eclipse環境後,一般均會自動識別Google的開發機,而Windows平臺上則需通過Eclipse更新驅動包,方法如下:

  1. 啟動Eclipse
  2. 點選Eclipse選單Window -> Android SDK and AVD Manager
  3. 點選Available package裡,如下圖。
  4. 展開選擇Third party Add-ons
  5. 點選安裝“Google Inc.(dl-ssl.google.com)”下的Google USB Driver package,revision 4
  6. 接受許可協議並等待安裝完成

 下在完後,在您的android-sdk-windows(NDK目錄下)安裝目錄下(C:\Program Files\android-sdk-windows\extras\google\usb_driver)會多出一個Extras資料夾,可以發現Google Usb Driver大俠,下文安裝驅動時使用。

二、開機Nexus One並連線USB資料線到計算機

開啟手機,插上USB線並連線到計算機,會發現Nexus One裝置,如下圖(裝置管理器):

 安裝USB驅動方法很簡單,只需在感嘆號上點選滑鼠右鍵,選擇“更新軟體驅動(Update software driver)”,然後再彈出視窗中點選瀏覽並定位到上文的Google驅動目錄下,選中usb_driver目錄點選確定開始安裝。

安裝完後,如下圖:

 三、測試檢查裝置是否線上

手機端設定:

把手機的“應用程式”->“開發”->“usb除錯模式”開啟,圖略。

計算機端測試:

開啟Windows自帶的CMD終端,進入到C:\Program Files\android-sdk-windows\platform-tools或上級的TOOLS目錄下(具體要看adb.exe安裝到哪裡)。輸入:

adb devices

應該可以看到可用的開發機裝置列表,如下圖(HT0CJP802425就是開發機Nexus One):

後續很多針對開機機的命令列控制均可在此介面完成(比如提權、修改資料夾許可權、安裝、刪除應用程式包等等),具體命令,請在上圖中輸入adb即可檢視到:

C:\Program Files\android-sdk-windows\platform-tools>adb
Android Debug Bridge version 1.0.26

 -d                            - directs command to the only connected USB device
                                 returns an error if more than one USB device is present.
 -e                            - directs command to the only running emulator.
                                 returns an error if more than one emulator is running.
 -s <serial number>            - directs command to the USB device or emulator with
                                 the given serial number. Overrides ANDROID_SERIAL environment variable.
 -p <product name or path>     - simple product name like 'sooner', or a relative/absolute path to a product
                                 out directory like 'out/target/product/sooner'.

                                 If -p is not specified, the ANDROID_PRODUCT_OUT environment variable is used, which must be an absolute path.
 devices                       - list all connected devices
 connect <host>[:<port>]       - connect to a device via TCP/IP 
                 Port 5555 is used by default if no port number is specified.
 disconnect [<host>[:<port>]]  - disconnect from a TCP/IP device.
                                 Port 5555 is used by default if no port number is specified.
                                 Using this ocmmand with no additional arguments will disconnect from all connected TCP/IP devices.

device commands:
  adb push <local> <remote>    - copy file/dir to device
  adb pull <remote> [<local>]  - copy file/dir from device
  adb sync [ <directory> ]     - copy host->device only if changed
                                 (-l means list but don't copy)
                                 (see 'adb help all')
  adb shell                    - run remote shell interactively
  adb shell <command>          - run remote shell command
  adb emu <command>            - run emulator console command
  adb logcat [ <filter-spec> ] - View device log
  adb forward <local> <remote> - forward socket connections
                                 forward specs are one of:
                                   tcp:<port>
                                   localabstract:<unix domain socket name>
                                   localreserved:<unix domain socket name>
                                   localfilesystem:<unix domain socket name>
                                   dev:<character device name>
                                   jdwp:<process pid> (remote only)
  adb jdwp                     - list PIDs of processes hosting a JDWP transport

  adb install [-l] [-r] [-s] <file> - push this package file to the device and install it
                                 ('-l' means forward-lock the app)
                                 ('-r' means reinstall the app, keeping its data)
                                 ('-s' means install on SD card instead of internal storage)
  adb uninstall [-k] <package> - remove this app package from the device
                ('-k' means keep the data and cache directories)
  adb bugreport                - return all information from the device
                                 that should be included in a bug report.

  adb help                     - show this help message
  adb version                  - show version num

DATAOPTS:
 (no option)                   - don't touch the data partition
  -w                           - wipe the data partition
  -d                           - flash the data partition

scripting:
  adb wait-for-device          - block until device is online
  adb start-server             - ensure that there is a server running
  adb kill-server              - kill the server if it is running
  adb get-state                - prints: offline | bootloader | device
  adb get-serialno             - prints: <serial-number>
  adb status-window            - continuously print device status for a specified device
  adb remount                  - remounts the /system partition on the device read-write
  adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
  adb reboot-bootloader        - reboots the device into the bootloader
  adb root                     - restarts the adbd daemon with root permissions
  adb usb                      - restarts the adbd daemon listening on USB
  adb tcpip <port>             - restarts the adbd daemon listening on TCP on the specified port networking:
  adb ppp <tty> [parameters]   - Run PPP over USB.
 Note: you should not automatically start a PPP connection.
 <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
 [parameters] - Eg. defaultroute debug dump local notty usepeerdns

adb sync notes: adb sync [ <directory> ]
  <localdir> can be interpreted in several ways:

  - If <directory> is not specified, both /system and /data partitions will be updated.

  - If it is "system" or "data", only the corresponding partition
    is updated.

environmental variables:
  ADB_TRACE                    - Print debug information. A comma separated list of the following values
                                 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp
  ANDROID_SERIAL               - The serial number to connect to. -s takes priority over this if given.
  ANDROID_LOG_TAGS             - When used with the logcat option, only these debug tags are printed.

三、測試APK應用程式

接下來我們就可以驗證自己的APK程式,步驟如下:

  1. 點選RUN AS,選擇Run Configurations,右側Target裡選擇Manual,並點選Run按鈕。
  2. 這時應該可以在新視窗中的Choose a running Android Device列表裡看到手機終端。
  3. 雙擊該手機終端(或點選OK),APK將會自動安裝並執行在開發機。

常見問題:

如果遇到開發機訪問 no permissions拒絕訪問時,可以嘗試一下方法:

1、檢查是否開啟USB除錯模式

2、在上文命令列下,執行:

adb kill-server

adb start-server

3、或嘗試關機再次重新開機連線除錯等。

更多文章: