熟练adb命令

adb很方便

//常用的adb命令

启动adb 服务
adb start-server
关闭adb 服务(是kill不是stop)
adb kill-server

安装应用
//文件路径---可以把文件直接拖入命令行窗口实现快速写入
adb install C:\Users\shao\Desktop\Demo.apk
强制安装
adb install -r C:\Users\shao\Desktop\Demo.apk
-l 锁定该应用程序
-r 替换已存在的应用程序,也就是说强制安装
-t 允许测试包
-s 把应用程序安装到sd卡上
-d 允许进行将见状,也就是安装的比手机上带的版本低
-g 为应用程序授予所有运行时的权限

卸载应用
adb uninstall (apk包名)
卸载 app 但保留数据和缓存文件:
adb uninstall -k (apk包名)

文件拷贝
将文件从Android设备拷贝到电脑
adb pull android路径 电脑路径
adb pull /data/system/dropbox C:\crash\dropbox
将文件从电脑拷贝到Android设备
adb push 电脑路径 android路径
adb push C:\Users\Administrator\Desktop\Poem.apk /system/app/Poem/
利用push操作将文件添加到相册
先将图片push到android设备
adb push ./1.png /sdcard
发送广播通知系统将图片添加到相册
adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard/1.png

增删显文件
删除Android设备上文件
adb shell rm /system/app/poem/poem.apk
删除目录下所有txt文件
adb shell rm /sdcard/Z2/\*.txt
删除目录下所有文件
adb shell rm /sdcard/Z2/\*.\*
删除Android设备上文件夹
adb shell rm -rf /sdcard/Zzzz
创建文件夹
adb shell mkdir -p /sdcard/Zdir
显示该目录下所有文件
adb shell ls /sdcard/
显示该目录下所有文件(包括权限、时间等详细信息)
adb shell ls -l /sdcard/
adb shell ls -al /sdcard/

查找/搜索文件
指定文件名搜索文件
adb shell busybox find . -name "aaa.jpg"

启动界面
启动activity,并显示启动用了多少时间
adb shell am start -n com.noahedu.intelligentguidestudy/com.noahedu.youyou.view.activity.MainActivity
启动activity(action启动)
adb shell am start -a com.noahedu.action.tv.boutique.fromLauncher
[-a ] [-d ] [-t ]
[-c [-c ] ...]
[-e|--es ...]
[--ez ...]
[-e|--ei ...]
[-n ] [-f ] []
启动Service(包名/service名)
adb shell am startservice -n com.noahedu.intelligentguidestudy/com.noahedu.mic.service.MicService
启动Service(action启动)
adb shell am startservice -a com.noahedu.mic.service.MicService
发送广播
adb shell am broadcast -a
带参数启动(action启动activity,并且传递key为type,value为1的参数
adb shell am start -a com.noahedu.action.tv.boutique.fromLauncher --es type "1"
--es key stringValue传递 String 参数;
--ez key booleanValue传递 Boolean 参数;
--ei key intValue 传递 int 参数;
--el key longValue 传递 long 参数;
--ef key floatValue 传递 float 参数;

查看磁盘空间状况
adb shell df -h

查看当前activity
adb shell dumpsys activity activities | find "Run"

查看机器分辨率
adb shell wm size

获取像素密度
adb shell wm density

android系统版本
adb shell getprop ro.build.version.release

查看手机系统是32位还是64位
adb shell getprop ro.product.cpu.abi
(64位显示arm64-v8a)

android机器厂商
adb shell getprop ro.product.brand

android机器名称
adb shell getprop ro.product.model

android机器设备信息(如sdk版本号,cup类型,desity, 机器名,厂商等等)
adb shell cat /system/build.prop

Logcat相关
调节日志缓存大小
adb logcat -G 50M
保存日志到文件
adb logcat -v time >i:\logv\logv.txt

获取app信息
列出手机装的所有app的包名:
adb shell pm list packages
列出系统应用的所有包名:
adb shell pm list packages -s
列出除了系统应用的第三方应用包名:
adb shell pm list packages -3
清除app数据(根据包名,包名必须存在正确才会提示success
adb shell pm clear com.noahedu.intelligentguidestudy

adb屏幕操作(需要手机相应权限)
adb模拟点击(点击到这个点206*395)
adb shell input tap 206 395
adb模拟滑动
adb shell input swipe 1061 499 695 498

Service相关
查看Service正在运行(这里MyService是服务的类名,建议使用类名查找更准确,有内容返回说明正在运行)
adb shell dumpsys activity services | adb shell grep MyService
查看Service正在运行2(也可以判断service是否运行,测试没运行会返回(nothing),建议用上面的方法)
adb shell dumpsys activity services MyService
应用程序进程是否正在运行(后台或前台):
adb shell pidof
停止正在运行的Service
adb shell am stopservice com.your.package.name/.YourServiceName

进程相关
查看进程:(有的时候只能查询到少数几个,原因不明)
adb shell ps
根据关键字查找进程:(进程名一般为包名com.xxxx.yyyy,输入xxxx、yyyy即可查询,包含xxxx、yyyy即可)
adb shell "ps | grep xxx"
关闭进程(根据进程id,id通过上面ps命令查找)用此方法关闭,进程(app)中service会立刻重新启动
adb shell kill -9 进程id
关闭进程(根据包名)用此方法关闭,进程(app)中service不会立刻重新启动
adb shell am force-stop com.noahedu.homework_check

截图/截取屏幕
截屏图片存放到 /sdcard/1screen.png
adb shell screencap -p /sdcard/1screen.png
将截屏图片拷贝到电脑桌面
adb pull /sdcard/1screen.png C:\Users\Adminnistrator\Desktop\pic

修改动画速率
0为关闭动画 1为正常速度 10为慢10倍速度
对于有些机型不生效
窗口动画缩放:
adb shell settings put global window_animation_scale 0.5
过渡动画缩放:
adb shell settings put global transition_animation_scale 10
动画程序时长调整:
adb shell settings put global animator_duration_scale 2.25

显示布局边界
显示布局边界(需要重新打开界面)
adb shell setprop debug.layout true
关闭显示布局边界(需要重新打开界面)
adb shell setprop debug.layout false

使用adb检查是否有内存泄漏

方法

  1. 启动你要检测的activity,比如要看首页是否有内存泄漏,那就启动首页,完成后按back键退出。
  2. 使用cmd 输入adb shell dumpsys meminfo [packageName]查看是否有内存泄漏。
  3. 查看Objects中的activities的个数是否为0,不为0即为泄漏。

一般我们无论进入多少activity,按back键返回桌面后,Activities一定是为0的,如果不为0,就说明发生了内存泄漏,这时候再配合LeakCanary去检测就很容易事半功倍。

adb在代码中执行(不在需要 adb shell)

            try {
                //不在需要 adb shell, 不大明白的是是不是只能执行adb shell命令
                String command = "input swipe  1061 499 695  498";
                Runtime.getRuntime().exec(command);
                Toast.makeText(this,"已执行操作",Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
                command_message.setText(e.toString());
            }

执行adb shell 需要返回结果

    /**
     * Execute the command.
     *
     * @param commands        The commands.
     * @param isRooted        True to use root, false otherwise.
     * @param isNeedResultMsg True to return the message of result, false otherwise.
     * @return the single {@link CommandResult} instance
     */
    public static CommandResult execCmd(final String[] commands,
                                        final boolean isRooted,
                                        final boolean isNeedResultMsg) {
        int result = -1;
        if (commands == null || commands.length == 0) {
            return new CommandResult(result, "", "");
        }
        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec(isRooted ? "su" : "sh");
            os = new DataOutputStream(process.getOutputStream());
            for (String command : commands) {
                if (command == null) continue;
                os.write(command.getBytes());
                os.writeBytes(LINE_SEP);
                os.flush();
            }
            os.writeBytes("exit" + LINE_SEP);
            os.flush();
            result = process.waitFor();
            if (isNeedResultMsg) {
                successMsg = new StringBuilder();
                errorMsg = new StringBuilder();
                successResult = new BufferedReader(
                        new InputStreamReader(process.getInputStream(), "UTF-8")
                );
                errorResult = new BufferedReader(
                        new InputStreamReader(process.getErrorStream(), "UTF-8")
                );
                String line;
                if ((line = successResult.readLine()) != null) {
                    successMsg.append(line);
                    while ((line = successResult.readLine()) != null) {
                        successMsg.append(LINE_SEP).append(line);
                    }
                }
                if ((line = errorResult.readLine()) != null) {
                    errorMsg.append(line);
                    while ((line = errorResult.readLine()) != null) {
                        errorMsg.append(LINE_SEP).append(line);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (successResult != null) {
                    successResult.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
        return new CommandResult(
                result,
                successMsg == null ? "" : successMsg.toString(),
                errorMsg == null ? "" : errorMsg.toString()
        );
    }

    /**
     * The result of command.
     */
    public static class CommandResult {
        public int    result;
        public String successMsg;
        public String errorMsg;

        public CommandResult(final int result, final String successMsg, final String errorMsg) {
            this.result = result;
            this.successMsg = successMsg;
            this.errorMsg = errorMsg;
        }

        @Override
        public String toString() {
            return "result: " + result + "\n" +
                    "successMsg: " + successMsg + "\n" +
                    "errorMsg: " + errorMsg;
        }
    }

其他
清屏 : cls
切换到其他盘符(D盘):cd /d d:/
切换到根目录:cd /
切换到上级目录: cd .. cd ../
切换到上两级目录:cd ../.. cd ../../

参考:
adb常用命令以及模拟器使用
Android 调试桥 (adb)
如何使用android中的adb shell知道服务是否正在运行

你可能感兴趣的:(熟练adb命令)