[译]一些常用的Android ADB命令

jopen 8年前

    在网上看到的一篇介绍常用ADB命令的文章,翻译过来和大家分享一下。原文地址:Handy adb commands for Android


    下面是一些我找到Android的ADB有用的命令,可以手动在命令行使用,也可以在自动化打包和测试中使用。


    一、查看已连接的设备


    使用下面的命令查看所有连接的设备,同时列出它们的IDs:

adb devices

    如果连接了多个设备,可以使用 adb -s DEVICE_ID 单独查看某个设备的信息。


    二、安装APP


    使用 install 命令来安装一个APP,使用命令行参数 -r 来重新安装APP,同时会保留这个APP之前在手机上的数据。例子如下:

adb install -r APK_FILE    # example  adb install -r ~/application.apk


    三、卸载APP


    比较简单,不过做介绍,命令如下:

adb uninstall PACKAGE_NAME    # example  adb uninstall com.growingwiththeweb.example


    四、打开应用界面(Activity)


    打开应用中的某个Activity:

adb shell am start PACKAGE_NAME/ACTIVITY_IN_PACKAGE  adb shell am start PACKAGE_NAME/FULLY_QUALIFIED_ACTIVITY    # example  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 想出来的:使用 shell screencap 截图,然后使用Perl脚本保存到本地。详细介绍可以查看他的 博客

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


    七、电源开关


    这个命令会发送电源按钮事件,打开或关闭某个设备:

adb shell input keyevent 26


    八、解锁屏幕


    这个命令可以向设备发送 解锁或锁屏 的事件,它可以和上面的电源命令一起使用:打开并解锁设备:

adb shell input keyevent 82


    九、打印所有安装的APP

adb shell pm list packages -f


    十、清除APP数据

adb shell pm clear PACKAGE_NAME    # example  adb shell pm clear com.growingwiththeweb.example


    十一、Logging


    1. 在命令行展示Log信息:

adb logcat

    2. 使用Tag Name过滤Log信息:

adb logcat -s TAG_NAME  adb logcat -s TAG_NAME_1 TAG_NAME_2    #example  adb logcat -s TEST  adb logcat -s TEST MYAPP

    3. 使用Log级别过滤Log信息:

adb logcat "*:PRIORITY"    # example  adb logcat "*:W"

    以下是常用的Log级别:

        V - Verbose (最低)

        D - Debug

        I - Info

        W - Warning

        E - Error

        F - Fatal

        S - Silent (最高, 使用这个级别将什么也不会打印)


    4. 使用Tag和级别同时过滤Log:

adb logcat -s TAG_NAME:PRIORITY  adb logcat -s TAG_NAME_1:PRIORITY TAG_NAME_2:PRIORITY    #example  adb logcat -s TEST: W

    5. 使用 grep 命令过滤Log:

adb logcat | grep "SEARCH_TERM"  adb logcat | grep "SEARCH_TERM_1\|SEARCH_TERM_2"    #example  adb logcat | grep "Exception"  adb logcat | grep "Exception\|Error"

    6. 清除 logcat 信息:

adb logcat -c


    十二、更多介绍


    更多的ADB命令介绍请看Android官方文档:official adb reference site


Over!

来自:http://xianglong.me/article/handy-adb-commands-for-Android/