android常用的adb命令

(文章出处:http://www.growingwiththeweb.com/2014/01/handy-adb-commands-for-android.html)

 

查看所有已经连接上的设备:

这个命令可以查看所有已经连接上的设备和他的设备ID:

adb devices

如果有多个设备连接到电脑,可以通过 adb -s DEVICE_ID 来指定用哪一个。

 

安装应用:

通过install命令来安装apk文件,-r参数可以重新安装某个应用并保留应用数据。

adb install -r APK_FILE

#举例

adb install -r ~/application.apk

 

卸载应用:

adb uninstall PACKAGE_NAME

#举例

adb uninstall com.growingwiththeweb.example

 

启动一个Activity:

adb shell am start 包名/.类名
adb shell am start 包名/类的全名

#举例

adb shell am start -n com.growingwiththeweb.example/.MainActivity
adb shell am start -n com.growingwiththeweb.example/com.growingwiththeweb.example.MainActivity

 

进入设备的命令行:

adb shell 

 

屏幕截图:

Sergei Shvetsov搞出了一个很妙的一行获取屏幕截图的代码(*避免了保存手机上再adb pull出来的麻烦)。具体的原理你可以到他的博客上查看。

adb shell screencap -p | perl -pe ‘s/\x0D\x0A/\x0A/g’ > screen.png

 

模拟电源键:

这个命令将电源键的事件传递给设备:

adb shell input keyevent 26

 

 

解锁屏幕:

这个命令向屏幕发送解锁的操作,包含了上面按下电源键和解开锁屏的操作:

adb shell input keyevent 82

 

 

输出所有已经安装的应用:

adb shell pm list packages -f

 

日志相关:

输出当前实时的所有日志:

adb logcat

 

通过标签名来进行过滤:

adb logcat -s TAG_NAME
adb logcat -s TAG_NAME_1 TAG_NAME_2

adb logcat -s TEST
adb logcat -s TEST MYAPP

 

通过优先级进行过滤:

输出某个优先级及以上的日志:

adb logcat “*:PRIORITY”

#举例

adb logcat “*:W”

 

优先级分为以下几种:

  • V – Verbose
  • D – Debug
  • I – Info
  • W – Warning
  • E – Error
  • F – Fatal
  • S – Silent (最高优先级,其实是什么也不输)

 

同时通过标签和优先级进行过滤:

adb logcat -s TAG_NAME:PRIORITY

adb logcat -s TAG_NAME_1:PRIORITY_1  TAG_NAME_2:PRIORITY

#举例

adb logcat -s test:W

 

grep命令来过滤:

logcat的输出可以通过系统的grep命令来过滤:

adb logcat | grep “SEARCH_TERM”
adb logcat | grep “SEARCH_TERM_1\|SEARCH_TERM_2”

adb logcat | grep “Exception”
adb logcat | grep “Exception\|Error”

 

清除logcat缓冲区:

用这个命令来清除一些重复出现的过时的日志

adb logcat -c

 

官方adb资料:http://developer.android.com/tools/help/adb.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注