ADB命令大全
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.