Adb 全名是 Android Debug Bridge,是開發或使用 Android 時很常用到的工具。使用者可以從Android 官方站下載 SDK,在其中的 platform-tools (原本在 \Tools) 中找到。

當機器上有打開 USB debug mode 時,使用者即可通過adb 進行各種 debug 、底層(linux user space)的 Android 功能。比較常用的功能:

- tools\ddms.bat: Android AP/Framework 層最主要的 debug tool (已經改名為)

 - 安裝 Android 應用程式 (*.apk)

- 連接機器,使用 linux userspace 的功能。 ex: ping, ssh, ftp ... blah blah.


這篇文章主要是整理了一些adb 的基本功能,後面補上一些開發時常用的功能。

文件參考:

- 官方的說明文件: http://developer.android.com/intl/zh-TW/guide/developing/tools/adb.html

Android模擬器adb命令介紹

- Source code: system/core/adb/ ,除了 adb client 跟 adbd (Android 系統內負責處理 adb 功能的 daemon)的實作外,裡面包含了 service 跟 overview 的文件。

 

功能介紹

1. 通過adb 進入機器或模擬器的shell模式

adb shell

也可以執行各種Linux的命令,其命令格式為:adb shell command

PS: 當 adb shell 之後提示字元為"#"時,表示使用者為 root (最大權限),若是 "$" 則是以shell 權限工作


adb shell ls 就是列出目錄

adb shell dmesg 會列印出Linux kernel log

adb shell cat /proc/kmsg 持續印出 kernel log (需要 root)

adb shell keyevent 1 輸入 keyevent,可輸入的內容參考 adb shell keyevent



2. 安裝Android 應用程式(*.apk)

可執行adb install android123.apk,這樣名為android123的安裝包就會安裝到Android模擬器中,前提是android123.apk文件需要放到SDK\Tools目錄下。

比較特殊的安裝方法還有

"-r": 當已經安裝過舊版本的程式時,可以使用 -r 去覆蓋。

"-f": 強制安裝,通常在安裝程式時會遇到相容問題,可使用此參數解決


3. PC 端與Android 機器的檔案傳輸

除了使用記憶卡模式外,還可使用下面命令可以進行檔案傳輸:

把 android123.txt 傳到機器上的/tmp/ 資料夾中:
adb push android123.txt /tmp/android123.txt

從機器上把 android123.txt 抓到PC端:

adb pull /tmp/android123.txt android123.txt

4. 顯示系統資訊 - dumpsys

除了直接輸入 adb shell dumpsys 外,也可以另外指定要顯示的 service,簡列一些參數,用法如:

adb shell dumpsys SurfaceFlinger

 

battery: 列出基本的電池資訊

batteryinfo: 各種功能使用 power 的狀況,同About Phone 裡面的電池使用狀況。

SurfaceFlinger: 系統的 Surface 使用情況

power: 列出 Power Manager 的參數,如 wakelock 時間等

alarm: 列出目前有註冊 alarm 者

 

5. 其他

- Android 預設可編譯成三種模式: eng, userdebug, user。一般使用者拿到的機器多是 user 版,當然如果是開發人員,可能會使用 eng 或 userdebug 版 進行debug。或是使用者自行 root 機器後,可使用下列指令取得 root 權限

adb root

- 一般為了防止系統出問題,所以 /system 通常在掛載時會設定為唯讀(read only),當使用者有root權限時,可使用下面指令將系統重新掛載成 R/W 模式,可對 /system 內的檔案做修改

adb remount

- 如果在使用 adb 時發現有* daemon not running. starting it now *的提示可以結束adb
adb kill-server

- 顯示 android 機器連接狀況

$adb devices
結果如下
List of devices attached
1234567890ABCDEF        device


-  wait-for-device: 等待正在運行的設備,通常是用在 script 中,後面可接著其他的 adb 指令

$ adb wait-for-device
$ adb shell ps


- Port forwarding,在某些應用如模擬器的網路連接使用、VNC時,會用到這項功能。主要是用來將機器上的的TCP port 5555 轉發到 port 1234

$ adb forward tcp:5555 tcp:1234 


- 擷取系統內的各種資訊,產生 bug report 

$ adb bugreport


- Sideload 功能: adb 1.03.2 後支援 sideload 功能,可在 recovery mode 燒錄 OTA 更新檔

重開機進入 recovery mode
$ adb reboot recovery

預設按 Vol UP + Power 可顯示 recovery mode 選單,選擇 "Apply update from adb 後,可使用 sideload 的方式燒錄 OTA 更新檔
$ adb sideload ota.zip




Android - How to mount the SDCard image file to Android Emulator

(1)首先必須產生SDCard的image file
mksdcard: create a blank FAT32 image to be used with the Android emulator
usage: mksdcard [-l label]
if is a simple integer, it specifies a size in bytes
if is an integer followed by 'K', it specifies a size in KiB
if is an integer followed by 'M', it specifies a size in MiB

ex:mksdcard 1024M sdcard.iso


讓SDCard連到Android Emulator
a:./emulator -sdcard ~/.android/SDK-1.0/sdcard.iso
b:用Eclipse中設定程式的Open Run Dialog裏,Target頁籤的Aditional Emulator Command Line Option中加入啟動參數 -sdcard scard.iso


linux 底下也可以使用 mount 掛載 sdcard.img
mount -o loop sdcard.img android_sdcard

使用 am - activity manager 啟動 java 程式

usage: am [start|instrument]
       am start [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                [-c <CATEGORY> [-c <CATEGORY>] ...]
                [-e <EXTRA_KEY> <EXTRA_VALUE> [-e <EXTRA_KEY> <EXTRA_VALUE> ...]
                [-n <COMPONENT>] [-D] [<URI>]
       am instrument [-e <ARG_NAME> <ARG_VALUE>] [-p <PROF_FILE>]
                [-w] <COMPONENT>

啟動瀏覽器:

# am start -n com.google.android.browser/com.google.android.browser.BrowserActivity

am start -n com.google.android.browser/com.google.android.browser.BrowserActivity
Starting: Intent { comp={com.google.android.browser/com.google.android.browser.BrowserActivity} }
//啟動瀏覽器,打開目標網址
# am start -a android.intent.action.VIEW -d http://www.google.com
am start -a android.intent.action.VIEW -d http://www.google.com
Starting: Intent { action=android.intent.action.VIEW data=http://www.google.com }
//撥打電話,號碼是123456789
# am start -a android.intent.action.CALL -d tel:123456789
am start -a android.intent.action.CALL -d tel:123456789
Starting: Intent { action=android.intent.action.CALL data=tel:123456789}

資料來源:
http://blackdidi.wordpress.com/2008/10/10/android-how-to-mount-the-sdcard-image-file-to-android-emulator/
http://www.android123.com.cn/moniqi/48.html
Android adb shell 啟動java程序


官方ADB文件:
http://code.google.com/android/reference/adb.html

arrow
arrow
    全站熱搜

    huenlil 發表在 痞客邦 留言(1) 人氣()