ADB (Android Debug Bridge)
Android 提供的一個通用的調試工具,借助這個工具,我們可以很好的調試開發的程序,
adb.exe在你安裝的android的sdk開發包tools(或platform-tools)目錄下
adb的全稱為Android Debug Bridge 調試橋,是連接Android手機與PC端的橋梁,
通過adb可以管理、操作模擬器和設備,如安裝軟件、系統升級、運行shell命令等。

adb使用方法:
adb [-d|-e|-s <serialNumber>] <command>
當你發出一個命令,系統啟用Android客戶端。客戶端並不與模擬器實例相關,所以如果雙服務器/設備是運行中的,你需要用-d選項 去為應被控制的命令確定目標實例。關於使用這個選項的更多信息,可以查看模擬器/設備實例術語控制命令。
1、安裝應用 到模擬器:
你可以使用adb從你的開發電腦上復制一個應用程序,並且將其安裝在一個模擬器/設備實例。像這樣做,使用install命令。這個 install命令要求你必須指定你所要安裝的.apk文件的路徑:
adb install <path_to_apk>
為 了獲取更多的關於怎樣創建一個可以安裝在模擬器/設備實例上的.apk文件的信息,可參照Android Asset Packaging Tool (aapt).
要注意的是,如果你正在使用Eclipse IDE並且已經安裝過ADT插件,那么就不需要直接使用adb(或者aapt)去安裝模擬器/設備上的應用程序。否則,ADT插件代你全權處理應用程序的 打包和安裝.
如果要刪除某個應用,按下面操作,例如應用為test:
adb shell
cd /data/app
rm test.apk
2、進入設備或模擬器的shell:
adb shell
通過上面的命令,就可以進入設備或模擬器的shell環 境中,在這個Linux Shell中,你可以執行各種Linux 的命令,另外如果只想執行一條shell命令,可以采用以下的方式:
adb shell [command]
如:adb shell dmesg會打印出內核的調試信息。
3、發布端口:
可以設置任意的端口 號,做為主機 向模擬器或設備的請求端口。如:
adb forward tcp:5555 tcp:8000
4、從模擬器/設備中拷入或 拷出文件:
可以使用adbpull ,push命令將文件復制到一個模擬器/設備實例的數據文件或是從數據文件中復制。install命令只將一個.apk文件復制到一個特定的位置,與其不 同的是,pull和push命令可令你復制任意的目錄和文件到一個模擬器/設備實例的任何位置。
從模擬器或者設備中復制文件或目錄,使用(如下 命):
adb pull <remote> <local>
將文件或目錄復制到模擬器或者設備,使用(如下命 令)
adb push <local> <remote>
在這些命令中,<local> 和<remote>分別指通向自己的發展機(本地)和模擬器/設備實例(遠程)上的目標文件/目錄的路徑
下面是一個例子:
adb push test.txt /data/dat/test/test.txt、
5、查詢模擬器/設備實例:
在發布adb命令之前,有必要知道什么樣的模擬器/設備實例與adb服務器是相連的。可以通過使用 devices
命令來得到一系列相關聯的模擬器/設備:
adb devices
作為回應,adb為每個實例都制定了相應的狀態信息:
序列號——由adb創建的一個字符串,這個字符串通過自己的控制端 口<type>-<consolePort>唯一地識別一個模擬器/設備實例。
下面是一個序列號的例子:
emulator-5554
實 例的連接狀態有三種狀態:
offline — 此實例沒有與adb相連接或者無法響應.
device — 此實例正與adb服務器連接。注意這個狀態並不能百分之百地表示在運行和操作Android系統,因此這個實例是當系統正在運行的時候與adb連接的。然 而,在系統啟動之后,就是一個模擬器/設備狀態的正常運行狀態了.
每個實例的輸出都有如下固定的格式:
[serialNumber] [state]
下面是一個展示devices命令和輸出的例子 :
$ adb devices
List of devices attached
emulator-5554 device
emulator-5556 device
emulator-5558 device
如 果當前沒有模擬器/設備運行,adb則返回 no device
6、查看bug報告:
adb bugreport
7、記錄無線通訊日志:
一般來說,無線通訊的日志非常多,在運行時沒必要去記錄,但我們還是可以通過命令,設置記錄:
adb shell
logcat -b radio
8、獲取設備的ID和序列號:
adb get-product
adb get-serialno
9、 訪問數據庫SQLite3
adb shell
sqlite3
如要打開已存在數據庫:
sqlite3 <路徑>/dbname.db
獲取設備名稱
在命令行中輸入adb shell cat /system/build.prop
其實,設備信息,主要是存放在“/system/build.prop”文件,通過“cat”命令就可以查看了.
安裝軟件:adb install apk (文件名稱.apk)
重新安裝該軟件:adb install -r apk (文件名稱.apk)
卸載apk軟件:adb uninstall apk(包名.apk)
查看手機上的運行日志,此項可以用來查錯:adb logcat
查看手機是否連接,以及連接了幾台手機:adb devices
A為手機路徑,B為電腦路徑,意思為:把文件從手機中復制到電腦上:adb pull <A> <B>
A為手機路徑,B為電腦路徑,意思為:把文件從電腦復制到手機上:adb push <B> <A>
進入手機的超級終端Terminal:adb shell
重新掛載文件系統:adb remount
重啟手機:adb reboot
重啟到Recovery界面:adb reboot recovery
重啟到bootloader界面:adb reboot bootloader
顯示該目錄下的文件和文件夾:ls
進入某個目錄:cd(注意分隔斜杠方向是/)
獲得最高權限:su
查找設備:adb wait-for-device
斷開連接:adb kill-server
開始連接:adb start-server
管理設備
adb devices // 顯示連接到計算機的設備
adb get-serialno // 獲取設備的ID和序列號serialNumber
------------------重啟----------------------------------------------
adb reboot // 重啟設備
adb reboot bootloader // 重啟到bootloader,即刷機模式
adb reboot recovery // 重啟到recovery,即恢復模式
------------------發送命令到設備--------------------------------------
adb [-d|-e|-s <serialNumber>] <command>
-d 發送命令給usb連接的設備 -e 發送命令到模擬器設備 -s <serialNumber> 發送命令到指定設備
adb相關
adb kill-server // 終止adb服務進程 adb start-server // 重啟adb服務進程
adb root // 已root權限重啟adb服務
adb wait-for-device // 在模擬器/設備連接之前把命令轉載在adb的命令器中
獲取設備硬件信息
adb shell cat /sys/class/net/wlan0/address // 獲取mac地址 adb shell cat /proc/cpuinfo // 獲取cpu序列號
管理設備app
aapt d badging <apkfile> // 獲取apk的packagename 和 classname
------------------安裝----------------------------------------------
adb install <apkfile> // 安裝apk
adb install -r <apkfile> // 保留數據和緩存文件,重新安裝apk,
adb install -s <apkfile> // 安裝apk到sd卡
------------------卸載----------------------------------------------
adb uninstall <package> // 卸載app
adb uninstall -k <package> // 卸載app但保留數據和緩存文件
------------------啟動app-------------------------------------------
adb shell am start -n <package_name>/.<activity_class_name> // 啟動應用
------------------查看內存占用----------------------------------------
adb shell top // 查看設備cpu和內存占用情況
adb shell top -m 6 // 查看占用內存前6的app
adb shell top -n 1 // 刷新一次內存信息,然后返回
adb shell procrank // 查詢各進程內存使用情況
adb shell kill [pid] // 殺死一個進程
adb shell ps // 查看進程列表
adb shell ps -x [PID] // 查看指定進程狀態
文件操作
//android中,sdcard代表內置存儲,不同系統中tf卡的設備名可能不同,使用查看adb shell ls mnt查看所有存儲設備名。
adb remount // 將system分區重新掛載為可讀寫分區
adb push <local> <remote> // 從本地復制文件到設備
adb pull <remote> <local> // 從設備復制文件到本地
adb shell ls // 列出目錄下的文件和文件夾,等同於dos中的dir命令
adb shell cd <folder> // 進入文件夾,等同於dos中的cd 命令
adb shell rename path/oldfilename path/newfilename // 重命名文件
adb shell rm /system/avi.apk // 刪除system/avi.apk
adb shell rm -r <folder> // 刪除文件夾及其下面所有文件
adb shell mv path/file newpath/file // 移動文件
adb shell chmod 777 /system/fonts/DroidSansFallback.ttf // 設置文件權限
adb shell mkdir path/foldelname // 新建文件夾
adb shell cat <file> // 查看文件內容
1.手機由於替換、由於更改權限等等的造成手機無限重啟或者直接無法開機。不刷機解決方法。
啟動cmd,追到adb目錄。
輸入以下命令(括號為顯示,沒有為無或者未知)(注意大小寫)
adb shell(應該顯示shell#@android/或類似)
adb remount(應該顯示remount succeed)
adb shell chmod #權限代碼# /#文件目錄#(權限代碼查我其他日志,一般都是644,文件目錄示例:/system/fonts/DroidSans.ttf)
adb reboot(重啟)
2.由於操作不當導致系統損壞而必須刷機,但是又想備份數據。
首先,手機開到REC模式,並且安裝了REC的ADB驅動(USB-MS模式)。
然后一樣,cmd追到adb目錄。(后面的是注釋)
adb start-server(然后幫你找手機了)
adb shell(請求shell用戶)
# mount /dbdata (其實有些網上說的是直接mount /data就可以,但是我發現我手機上面的數據都是存放在dbdata上)
(另一點,這句話我也沒試過,自己試試看吧,貌似第三方REC(比如CMW)可以在REC上先掛載。
com.android.providers.contacts 這個是你的電話本
com.android.providers.telephony 這個是你的短信
怎么弄出來呢?我也就不用多解釋了,前面已經給出了adb的復制命令了。
3.仿造REC備份整個系統。
一樣,先追到adb目錄。
然后命令是:adb backup [-system|-nosystem] -all [-apk|-noapk] [-shared|-noshared] -f <檔案名稱>
(就一條而已)
下面開始解釋(相信你會看cmd的也一樣看出是什么意思吧)
[-system|-nosystem]
這個指令是告訴adb 在備份時是否要連同系統一起備份。
若沒有打的話,默認是-system 表示會一起備份系統。
注意!若連系統一起備份,在還原的時候會復蓋系統檔案,對於已經升級后的手機是非常不好的
我不知道在沒有ROOT的情況下adb是否有權限去還原系統檔案,但就算如此 還是不建議這樣做。
因此,-nosystem是建議一定要打上的指令。
-all
這個指令是一定要打上去的,問你是否要備份全部的APP。
若有加上-nosystem的指令,那么他就只會備份你目前已經安裝上去的APP 而不會連系統APP一起備份。
[-apk|-noapk]
默認是-noapk,這個的意思是是否連安裝的APK一起備份。
若為-noapk則只會備份APK的資料檔(像是游戲存盤 設定 之類的)。
但我會建議連APK一起備份,因為這樣才不會有版本沖突的問題。
[-shared|-noshared]
默認是-noshared 這個會問你是否連手機儲存空間或是SD卡的檔案一起備份。
我是不知道若有APP安裝在SD卡上的時候 沒打這指令是否會略過,但若大部分的APP都安裝在內部空間的話,這個就可以不用打了。
(注:HTC有保留一個名為com.android.sharedstoragebackup的APK,這個APK會將SD卡或是手機空間的資料都備份 因此除非使用-nosystem指令,否則對於HTC手機來說 不管有無打-shared 手機空間的資料一定會備份)。
-f <檔案名稱>
這個是指定備份完之后,要存在電腦的哪個地方。
不過記得要打檔名 而副檔名則一定要打.ab
好的,那么我給出一條比較好的完整命令:
adb backup –system –all –apk –share –f “D:\test\test.ab”
備份補充相關要點:
這邊要先說明,由於有些電腦並非使用原生USB3.0,而是使用獨立的芯片,並非由芯片組直接提供,因此有時adb會因此無法連線或是傳輸上出問題,所以 請連接USB2.0或是連接原生的USB3.0。
首先請確定已經有安裝手機驅動(特別強調! 至於手機驅動哪找 看你是哪家手機就去哪家的官網找)
接着請連上手機 確定USB偵錯模式是否有啟動
(注:USB請設定為充電模式或MTP模式 請勿設定成磁盤機或網絡分享!)
接下來就可以直接使用backup指令了
比方說我要備份APK資料,要連同APK一起,不要去備份SD卡或外存,不要去備份到系統APP且我要指定檔案名稱為test,並存在D盤的話,那么指令就是:adb backup -apk -all -nosystem -f "D:\test.ab"
注意1:檔案路徑和檔名若有空格 請記得路徑要加雙引號””
例:-f "D:\phone backup\htc j.ab",否則會有問題。
注意2:若輸入指令后沒出現備份畫面或是adb顯示錯誤訊息,有可能是adb沒抓到手機,請使用adb devices指令(完整指令請輸入adb查看)來看adb是否有抓到手機。
這個會要你輸入這個備份檔案的保護密碼,你可以選擇不輸入。好了之后就按“備份我的資料”。
再來就慢慢等它備份啦……備份的過程中,手機畫面會出現正在備份的APK及資料。
備份完后就會自動跳出程序,並且看看D槽。
還原:
既然已經備份好了,那之后要怎么還原呢?
這時候就要使用另一個指令 adb restore
我還是以剛剛的test.ab檔案為例 那么打的時候就輸入
adb restore "D:\test.ab"
打入你的手機密碼以及你的封包所設定的密碼(假如有的話)就會開始做還原的動作,而且它會依據一開始備份時存放的路徑下去還原。
等到還原成功后,就可以去看看APP是否都回來了,也可以檢查游戲存盤或是設定那些是否還在。
進階作法及額外注意事項:
個別程式備份方法:
有不少人問我如何備份單個APP就好了,說簡單不簡單,說難也不難。難就難在得找出APP的物件名稱。
什么是物件名稱?簡單來說物件名稱才是屬於APP真正的名稱,由每一個APP開發者(或開發商)所自己定義的Java package名稱,別忘了 Android的應用都是由Java寫出來的 如果有寫過Java的話應該對package不陌生,至於我們一般看到的APP名稱都是另外再加上去的。
比如 從iOS移植到Android上的免費游戲之一,Temple Run,它表面名稱雖然就叫Temple Run,但它實際的物件名稱為 com.imangi.templerun,而不要以為這就是基本的格式了(什么前面一定是com之類的),這個完全是由開發者自行定義。雖然通常開發者都是定義com開頭后面加東西,但是當然也有很坑爹的開發者,本人下載上萬測試軟件中就出現過那么一個新手代碼是cmw.firstblood.supersystem……好吧后來我反編譯之后改掉了。
OK 這樣大概就了解了吧? 不過要怎么去看物件名稱?
基本上一般的應用程式管理員和工作管理員都是沒辦法看到物件名稱的,需要安裝類似APK管理器之類的軟件(像是超級管理器),才可以知道每個APP的類別名稱。
特別注意!類別名稱是有大小寫區分的,請仔細查看,以免備份出問題。
若知道了物件名稱后 指令要怎么打呢?我們這邊就以Temple Run為例
adb指令就要打成::
adb backup -apk com.imangi.templerun -f "D:\test.ab"
這邊說明一下,由於只是備份單一APP的資料,因此不需要加-nosystem -all -noshared等全域指令,只需要這樣簡單打就可以了,而依照你的喜好,想連APK一起備份就加-apk。
輸入完后 手機會再度出現與上頭一樣的畫面,只要按一下備份,就會把指定的APP給備份下來了。
資料另外的儲存位置:
有些APP資料或是游戲存盤,並不一定會存在放手機內部的儲存空間,有可能會存放在SD卡或是手機的使用者儲存空間。
目前已經知道,像是TempleRun,SubwaySurf,以及LINE,他們的紀錄都是存在SD卡或是手機儲存空間上,至於怎么找他們,只要到SD卡(使用者儲存空間)里頭看,會有一個Android資料夾,里面有一個data資料夾,就是檔案的存放之處,只要備份下來就可以了,但要注意的是,若在備份之前就移除程式,那么該資料也是會從儲存空間上移除的,這點還請注意。
使用鈦備份還原:
這是我找資料的時候無意間發現的 其實鈦備份也支援使用adb備份出來的資料,只要版本是5.6以后的就行了(想要的下載后面的鈦備份連接),因此,也可以使用鈦備份進行還原的動作,缺點是本身需要root 而且必須是專業版才支援。
Last week, we showed you how to install ADB and fastboot on any OS. If you're not sure why you'd want to go to the (relatively minor) trouble, here are just some of the useful things you can do with these two handy tools.
Disclaimer: These commands are intended to give you an idea of what you can do with ADB and fastboot. They are not direct instructions and not all commands work on all devices. It's perhaps better to think of this as a glossary. Due to the sheer number and variety of devices and implementations in the Android world, it's impossible for us to provide step-by-step instructions for every single device. Be sure to research your specific phone or tablet before throwing commands at it.
Manage Your Device with ADB
ADB has a wide variety of functions for managing your device, moving content to and from your phone, installing apps, backing up and restoring your software, and more. You can use ADB while your phone is plugged in to a computer. You can also use ADB with your device wirelessly by following these instructions. You'll need to briefly connect your device to your computer with a USB cable for this to work, but it should only take a few seconds to execute these commands and then you're good to use ADB wirelessly if you so choose.
adb devices
Function: Check connection and get basic information about devices connected to the computer.
When using ADB, this is probably the first one command you'll run. It will return a list of all devices that you have connected to your computer. If it returns a device ID like the one seen above, you're connected and ready to send commands.
adb reboot recovery
Function: Reboot your phone into recovery mode.
A lot of functions like flashing ROMs to your phone require you to boot into recovery mode. Normally, this requires you to hold down a particular set of buttons on your phone for a certain length of time, which is obnoxious. This command allows you to boot directly into recovery mode without performing the complex finger dance of your people.
adb reboot-bootloader
Function: Reboot your phone into bootloader mode.
Along the same lines as the previous command, this one allows you to boot directly to your phone's bootloader. Once you're in the bootloader, ADB won't work anymore. That's where fastboot comes in (which we'll get to in a bit). However, much like the recovery command, it's much easier to boot into your bootloader with a command on your computer than a complex series of buttons on your phone.
adb push [source] [destination]
Function: Copy files from your computer to your phone.
The push command allows you to copy files from your computer to your phone without touching your device. This is particularly handy for copying large files from your computer to your phone like movies or ROMs. In order to use this command, you'll need to know the full file path for both your source and destination. If the file you want to copy is already in your tools folder (where ADB lives), you can simply enter the name of the file as the source.
adb pull
Function: Copy files from your phone to your computer.
The yin to to push's yang, the pull command in ADB allows you to copy files from your phone to your computer. When pulling files, you can choose to leave out the destination parameter. In that case, the file will be copied to the folder on your computer where ADB itself lives. You can then move it to wherever you'd prefer like normal.
adb install [source.apk]
Function: Remotely install APKs on your phone.
You can use this command to install an app on your phone without touching it. While this isn't a terribly impressive trick for an app that's on the Play Store (where you can already remotely install, uninstall, and update apps), it's quite handy if you need to sideload an app.
adb shell [command]
Function: Open or run commands in a terminal on the host Android device.
We love the terminal here at Lifehacker. There are so many great things you can do with it. Most of us don't tend to bother with the terminal in Android because we don't want to type long text-based commands on a tiny touchscreen. However, the adb shell command allows you to open up a full terminal on the host device. Alternatively, you can type "adb shell" followed by a valid terminal command to execute just that one command by itself.
adb backup
Function: Create a full backup of your phone and save to the computer.
Backing up your Android phone is already something you can and should be doing automatically. However, if you need to create a complete backup before hacking away at something particularly risky, you can create a full backup with a single command. You don't even need root access (though this may mean that some protected data can't be backed up). You can read more about the parameters for this command—and there are a lot of them—here.
adb restore
Function: Restore a backup to your phone.
The corollary to the previous command, adb restore allows you to point to an existing backup file and restore it to your device. So, for example, type "adb restore C:\[restorefile].zip" and your phone will shortly be back to normal.
adb sideload
Function: Push and flash custom ROMs and zips from your computer.
This command is a relative newcomer to the ADB field and is only supported by some custom recoveries. However, you can use this single command to flash a .zip that's on your computer to your phone. Once again, this allows you to flash whole ROMs (or anything else you can flash with a .zip file) without touching your phone.
These commands are just some of the more useful ones you can use with ADB installed on your computer. You may not want to use it all the time for everyday tasks, but when you need them, you'll be glad you have them.
Unlock and Modify Your Phone's Firmware with Fastboot
As stated in our previous article, fastboot allows you to send commands to your phone while in the bootloader (the one place ADB doesn't work). While you can't do quite as many things here, the things you can do are awesome, including unlocking certain phones—like Nexuses and certain others—as well as flashing custom recoveries and even some ROMs. It should be noted, though, that not all phones support fastboot and if you have a locked bootloader, you're probably out of luck here. That being said, here are some of the most useful tools in fastboot's arsenal.
fastboot oem unlock
Function: Unlock your bootloader, making root access possible.
When people go on about how "open" Nexus devices are, this is what they're talking about. Most phones require a root exploit to gain superuser access and the ability to heavily modify your phone's firmware. With a Nexus device, you can unlock your bootloader with a single command. From there, you'll be allowed to install custom recoveries or give yourself root access.
It should be noted, this command will also completely wipe your phone. This means it's a great command to run when you get a brand new phone, but if you've been using yours for a while, do a backup first.
fastboot devices
Function: Check connection and get basic information about devices connected to the computer.
This is essentially the same command as adb devices from earlier. However, it works in the bootloader, which ADB does not. Handy for ensuring that you have properly established a connection.
fastboot flash recovery
Function: Flash a custom recovery image to your phone.
Flashing a custom recovery is an essential part of the ROM-swapper lifestyle. As with everything else in this list, you can install a custom recovery on your device without touching it by using this command.
