常用 adb 命令

常用 adb 命令

  • 导出 anr 文件
  1. 先将 anr 文件夹拷贝到 sdcard
    adb shell ->cd data -> cp -r anr /sdcard/Android
    2.将文件从 sdcard 拷贝到电脑
    adb shell pull /sdcard/Android/anr C:\Users\Desktop
  • 查看某个进程 的进程 id

top |grep walleve

  • adb devices 提示 offline 是因为没有在同一个局域网内

  • 查看进程号
    adb shell top |grep com.xx.xxx

  • 启动 service

//不带参数
adb shell am startservice -n com.apache.test/.portal.service.SoundboxService   

//带参数

adb shell am startservice -n com.apache.test/.portal.service.SoundboxService   -es action  exit

  • 查看启动 activity:
adb shell am start com.apache.soundbox
  • am start 启动Activity
adb shell am start -a com.noahedu.noahdict.screenshots.recognition --ei type 5

上面的命令对应的代码就是
Intent intent = new Intent("com.noahedu.noahdict.screenshots.recognition");
intent.putExtra("type", 5);
startActivity(intent);

-a action;activity对应的action;
--es key stringValue; 传递 String 参数;
--ez key booleanValue; 传递 Boolean 参数;
--ei key intValue; 传递 int 参数;
--el key longValue; 传递 long 参数;
--ef key floatValue; 传递 float 参数;
  • 抓取log,在升级后出现闪退的机器中执行如下命令

1,adb shell dumpsys dropbox --print >dropbox.log

2,adb bugreport >bugreport.zip

  1. adb shell dumpsys dropbox --print
  • 查看系统版本、设备型号信息
adb shell cat /data/devinfo.txt

输出:
devid=
sv=V8-T653T11-LF1R015
hv=MT9653
devmodel=XXX-CN-MT9653-C11G
  • 查看当前进程
 adb shell "ps |grep com.apache"

  • 写入变量到系统中(跟应用安装、卸载无关)

setprop persist.url.Gloable.log true 打开日志
setprop persist.url.Gloable.log false 关闭日志
setprop persist.url.tedulauncher.env release 配置域名环境为正式
setprop persist.url.tedulauncher.env debug 配置域名环境为测试
setprop persist.url.tedulauncher.env https://www.xx.com 直接配置域名
setprop persist.http.tedulauncher.cache -1 不设置缓存

* adb 读取属性名称

adb shell getprop persist.sys.logd.level


*  日志 

先 adb shell
然后
logcat >/data/test.log

logcat -v time |grep Soundbox // 过滤出 包含 Soundbox 关键字的日志
logcat -v time |grep -E "Soundbox|Jar" //-E 代表匹配多个关键字
logcat -v time > D:\Logcat\logcat.log //将日志保存到 logcat.log 文件中

//查看logcat 缓存大小
logcat -g
//设置logcat 缓存大小



//读取属性名称
private static String getPropertyResult(String key, String defaultValue) {
        String property = null;
        try {
            Class clazz = Class.forName("android.os.SystemProperties");
            Method method = clazz.getMethod("get", String.class, String.class);
            method.setAccessible(true);
            property = (String) method.invoke(clazz, key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return property;
    }

String host = getPropertyResult("persist.url.tedulauncher.env",  "release");

    /**
     * 控制log 开关
     *
     * @return 通过开关控制是否支持打印
     */
    public static boolean isLogSwitchOpen() {
        String result = getPropertyResult(ENABLE_LOG_KEY, "false");
        Log.d(TAG, "isLogSwitchOpen: result= " + result);
        return TextUtils.equals(result, ENABLE_LOG_VALUE_TRUE);
    }
  • disable 某个应用
adb shell pm disable  packagename
  • 查看被 disable 的命令
adb shell pm list packages -d  
  • 查看当前显示的 activity
adb shell dumpsys activity| grep -i resume  (mac)
adb shell dumpsys activity| findStr -i resume (windows)
  • monkey
    执行
    adb shell monkey -s 10 --pct-touch 30 --throttle 300 -p com.kidsedu 10000 --monitor-native-crashes
    adb shell monkey -s 10 --pct-touch 30 --throttle 300 -p com.kidsedu -v 10000 --monitor-native-crashes

停止monkey:

  1. 通过命令 adb shell ps 找到 moneky 所在的进程 pid(monkey进程的名字为:com.android.commands.monkey)
  2. adb shell kill pid
  • 查看某个包名的进程id

adb shell ps -A | grep com.kidsedu

  • 查看正在运行的 activity

adb shell dumpsys activity activities | grep mResumedActivity
输出

 mResumedActivity: ActivityRecord{3200dc1 u0 com.noahedu.launcher/.Launcher t3}

其中的 com.noahedu.launcher/.Launcher t3就是当前处于前台的 Activity。
在 Windows 下以上命令可能不可用,可以尝试

adb shell dumpsys activity activities | findstr mResumedActivity 或 adb shell "dumpsys activity activities | grep mResumedActivity"。
  • 查看正在运行的 Services
adb shell dumpsys activity services []
  • 查看应用的详细信息
adb shell dumpsys package 
//查看 APK 路径
adb shell dumpsys package com.apache.android|grep path 

  • 查看安装路径
adb shell pm list packages -f | grep "com.apache.android"
  • clear 清除应用数据
pm clear com.tencent.karaoke
  • 电脑输入文字到手机
adb shell input text "www.baidu.com"

应用管理

  • 启动指定包名的应用

adb shell am start -n packageName

  • 启动应用/ 调起 Activity
    命令格式:

adb shell am start [options]
例如:adb shell am start -n com.tencent.mm/.ui.LauncherUI

表示调起微信主界面。

adb shell am start -n org.mazhuang.boottimemeasure/.MainActivity --es "toast" "hello, world"
  • 不指定Activity名称启动(启动主Activity)
    命令格式:

adb shell monkey -p -c android.intent.category.LAUNCHER 1

例如:

adb shell monkey -p com.tencent.mm -c android.intent.category.LAUNCHER 1

表示调起微信主界面。

  • 强制停止应用
adb shell am force-stop packageName
  • adb install -r/-s

使用adb install命令可以从开发用电脑中复制应用程序并且安装到模拟器或手机上,adb install命令必须指定待安装的.apk文件的路径;
-r:保留数据和缓存文件,重新安装apk
-s:安装apk到sd卡

  • adb reboot

作用:重启连接成功的设备。
如果连接了多个设备可以指定重启其中一个设备,命令如下
adb -s xxx reboot

  • adb shell cat /sys/class/net/wlan0/address

作用:获取机器mac地址;
说明:adb shell命令表示进入设备或模拟器的shell环境中,在这个Linux Shell中,你可以执行各种Linux的命令;
adb shell netcfg //查看当前连接成功手机的ip地址;

  • adb shell rename

重命名文件;

  • adb shell rm -r

删除指定路径文件的文件或者文件夹;

  • adb shell mv

移动文件到新的目录

  • adb shell mkdir path/foldelname

作用:新建文件夹;
adb shell chmod777 设置文件的权限;
adb shell cat 查看文件内容;

  • adb forward tcp:8000 tcp:9000

作用:执行此命令后, PC端的8000端口会被adb监听, 这个时候我们只需要往8000端口写数据, 这个数据就会发送到手机端的9000端口上,一般用来主机和移送设备进行额外的数据传输;

  • adb shell am XX

常见的命令如下所示:
//启动 Activity
adb shell am start -W com.kidsedu.test/com.kidsedu.ui.MainActivity
a:adb shell am start [options] intent;
//使用Action方式打开系统设置-输入法设置
adb shell am start -a android.settings.INPUT_METHOD_SETTINGS
//使用组件名方式启动照相机功能
adb shell am start -ncom.android.camera/.Camera //打开拨号界面,并传递一个DATA_URI数据给拨号界面
am start -a android.intent.action.CALL-d tel:10086
//使用ComponentName 方式启动一个Service
adb shell am startservice com.karaoke.RecordService
adb shell am force-stop com.some.package
adb shell am kill com.some.package
adb shell am monitor //监控程序的crash和anr错误;
adb shell am start -W com.some.package/首屏Activity全路径: 监控冷启动时间

  • 查看sp中数据

adb shell -> run-as com.kidsedu 参考:https://www.jianshu.com/p/5648fdba83cd

monkey

  • 开始 monkey
adb shell monkey -p com.kidsedu --throttle 100 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v –s 10000  100000 > /Users/jerry/Documents/monkey_log.txt
  • 停止 monkey
  1. 找出 monkey所在进程

adb shell ps |grep monkey

5.停止进程id

adb shell kill 8203

查询日志开关

getprop persist.sys.log.level

打开日志:getprop persist.sys.log.level v

关闭日志:getprop persist.sys.log.level s

抓取日志

logcat -v time > /mnt/xxx(U盘路径)

如需过滤

logcat -v time | grep (包名/其他过滤字段)

查看正在运行的包名:dumpsys activity | grep packageName

查看应用版本号

dumpsys package 包名 | grep versionName

如:dumpsys package com.tcl.videocall | grep versionName

查apk安装路径

pm list packages -f | grep 包名

清理应用缓存:pm clear 包名

如:pm clear com.tcl.videocall

查看应用PID:ps -ef | grep 包名

如:ps -ef | grep com.tcl.videocall

查看应用进程信息:top | grep PID

如:top | grep 8446

杀进程

kill -9 PID

查看电视机型

cat /data/devinfo.txt

你可能感兴趣的:(常用 adb 命令)