adb command papers

Foreword       

       Android provides a lot of adb command, function is very powerful, can bring great convenience for the development and debugging. Of course, not to showcase the various commands of the article, but need to record frequently used command in peacetime work, to facilitate normal use at work, so the future will continue to add some common and very useful command. These commands also hope to give readers some help.

       The current command has been recorded mainly related to the following functions:

       First, commonly associated with Permission of adb command

       Second, connect your phone via Wifi connection

       Three, adb four assembly operations

       Fourth, implement a script multiple adb command operation

       Fifth, some of the information with adb dump

       Six, adb with the process

 

First, commonly associated with Permission of adb command

       Android provides a number of tools adb command, you can easily view, authorization, permission to cancel the application, you can bring a lot of convenience for the debugger, following a brief few common commands.

  1, check the specified app permissions filings

       Command: adb shell dumpsys package [package name]

       Purpose: This command is used to obtain information about the package the app, Permission only part of the information.

       Command Example:

1 adb shell dumpsys package cn.aaa.bbb

       Under the screenshot below for information on the part of the command authority:

       4 which shows a portion of permissions:

    (1) declared permissions. The application own statement (ie custom) rights. This shows the name of the authority, permission levels, and obtain the permission at what time (INSTALLED express installation time will be granted the privilege).

    (2) requested permissions. Here is the list of all the permissions request AndroidManifest.xml file, you can see that it contains dynamic application of rights and permissions when installing applications.
    (3) install permissions: the installation on the given permission. And requested permissions can compare, less a "android.permission.CAMERA" authority there, the permission for the dynamic application access. The list also shows the corresponding permission grant case, as shown granted, true indicates permission has been granted.

    (4) runtime permissions. Shown here is the need to apply for permission to run when, that is dangerous permission.

  2, permission to view statements and users

       Command: adb shell dumpsys package permission <authority name>

       Purpose: This command can view the specified authority who declared the application for which applications have permission.

      Command Example:

1 adb shell dumpsys package permission cn.aaa.bbb.TEST_PERMISSION

Excerpt follows the authority's definition information and which application to use the privilege of a key information:

Copy the code
Copy the code
 1 Permissions:
 2   Permission [cn.aaa.bbb.TEST_PERMISSION] (d4d8316):
 3     sourcePackage=cn.aaa.bbb
 4     uid=10078 gids=null type=0 prot=signature|privileged
 5     perm=Permission{f5b497 cn.aaa.bbb.TEST_PERMISSION}
 6     packageSetting=PackageSetting{96e1684 cn.aaa.bbb/10078}
 7 
 8 Packages:
 9   Package [cn.xxx.xxx] (5d0f51b):
10     ......
11     declared permissions:
12     requested permissions:
13     install permissions:
14       cn.aaa.bbb.TEST_PERMISSION: granted=true
15 
16 ......
Copy the code
Copy the code

   3. Remove the specified permissions

      命令:adb shell pm revoke [packageName] [permissionName]

      Uses: Removing packageName applications permissionName privileges (permissions can remove a number of simultaneously).

      Command example (to remove the package follows the camera's authority named cn.aaa.bbb):

1 adb shell pm revoke cn.aaa.bbb android.permission.CAMERA

After executing this command, "adb shell dumpsys package cn.aaa.bbb" command to view the authority of the previously mentioned information is as follows:

 It found through experiments that the command is valid for runtime permissions, but then install permissions invalid, information shown as the following exception:

 

  4, assign permissions granted

       命令:adb shell pm grant [packageName] [permissionName]

       Purpose: to grant permission for the permissionName packageName application (can grant more privileges at the same time). The removal of a command and the command, respectively.

       Referring to the example of a command, the experimental results are as follows:

  5. Check all system-defined permissions

       Command: adb shell pm list permissions -s [option] without permission name list of all -s will significantly defined in the system, plus a description of the -s will show the use of these rights.

      参考:【Viewing an app's permissions:https://developer.android.google.cn/guide/topics/permissions/overview#viewing

       The following shows a screenshot respectively -s command without displaying the results of the Canadian -s (redirected to view the text), which without -s screenshot, shows a total of 571 authority, where interception of a part, wherein We can see a lot of custom permissions.

  6, viewing permissions by group

       命令:adb shell pm list permissions -d -g

       Purpose: Check grouping privileges. This part is a supplement above command, parameters can be selected according to their needs.

       参考:【https://developer.android.google.cn/training/permissions/usage-notes#testing

       The following screenshot as part of the results.

 

  7, all permissions

       命令:adb shell install -g MyApp.apk

       Use: When the simulator is mounted MyApp.apk tester or, if coupled -g, all permissions automatically. I have not experimented with this, readers can self-test.

       Reference: and where the fourth point as described with reference to official website.

   

Second, connect your phone via Wifi connection

       Sometimes due to abnormal data lines, resulting connection is not on the phone on the computer, sometimes people are crazy. In another case, when the phone is connected via USB, can interfere with the mobile phone interface, impact analysis problems, such as the charge will appear on your phone screen and so on. At this time, the phone can be connected via Wifi, completely get rid of the data line, thereby flying self. Briefly outline the concrete steps, with the proviso that need Wifi and computers on the same local area network.

    (1) connected to wifi, view the phone's ip address

       Most people know that computer has an IP address, but few people pay attention to the phone's IP address. This provides two ways to view the IP phone, of course, provided that connect the Wifi.

       1) Check the details in WIFI

       Turn into the setting "WLAN, you can see the following interface, in order to enter into the details of the selected wifi, you can view the IP address.

    

       2) Check the phone information

       In turn follow the path set "on the phone" status "IP address, can be found in the following interface IP address.

     

    (2) Set the port number to the phone

       This step is necessary to connect a computer via USB (it is still not completely out of the data line), set by the command adb tcp ip 5555. 5555 default fill it, I tried the other, did not succeed.

      

    (3) to connect mobile phones

       Command: adb connect [ip address]

       Example:

        查看连接的设备,就能看到通过WIFI连接的手机了。

 

       在AndroidStudio上就可以选择该手机了,各种操作和用USB连接时都是一样的。

     (4)断开连接

       使用完后最好断开连接,断开命令为:adb disconnect [ip地址]

 

三、adb操作四大组件

  1、adb 启动activitry

       命令:adb shell am start -n pkgname/activityPath(可以是全路径,也可以是相对路径)

       如果成功,会看到界面被打开;如果失败,会在命令行处显示错误信息。 

       启动activity和service还有一种action方式打开,暂时没有实验成功,暂不记录。

 

  2、adb 启动service

       命令:adb shell am startservice -n pkgname/servicepath(可以是全路径,也可以是相对路径)

 

  3、adb 停止service

       命令:adb shell am stopservice -n pkgname/servicepath

 

 

  4、adb发送广播

      命令:adb shell am broadcast -a actionName --es EXTRA_KEY EXTRA_KEY_VALUE

      说明: -a 表示action,-es表示附件的键值对参数,且参数值为string(-ei对应int,-ez对应boolean)

      使用案例:

这句命令的作用就相当于执行了如下代码:

1 Intent intent = new Intent(“com.songwei.broadcast.demo”);
2 intent.putExtra(“pkgname”,“com.example.demos”);

       广播接收到的信息为:

 

四、实现一个操作多条adb命令的脚本

       有些固定操作需要执行多条adb命令,如果每次都手动输入,会比较麻烦。如果写一个脚本来自动执行,会方便很多。如下实现一个自动push SystemUI.apk的脚本:

 adb root
 adb remount
 adb push ./SystemUI.apk system/priv-app/SystemUI
 adb shell sleep 1
 adb reboot
 pause

 

五、用adb dump一些信息

  1、查看内存信息

       命令:adb shell dumpsys meminfo [进程名]

  2、查看当前显示的界面信息

       命令:adb shell dumpsys window | findstr mCurrentFocus

    

      

六、adb与进程

 1、查看进程

      adb shell

      ps -ef|grep [keyword] 关键字不用加引号,如下图所示:

      

  2、杀死进程

       adb shell

       kill -s -9 [pid] 示例如下所示:

            

  3、一个查看ps来找到某个service的案例

       需求:需要找到华为手机人脸解锁功能所在的进程。

       难点:使用人脸解锁时,没有对应的界面,判断是一个service在执行该功能,所以无法dump出window信息。

       分析:对于没有界面的功能,可以通过logcat,根据关键字段推断所在进程号,然后通过进程号,结合ps命令可以查到对应的进程名,进而推断所在模块。例如:在华为机器上判断人脸解锁在哪个模块,先获取一份Log记录,如下所示:

                       

通过tag名称,可以判断 FaceRecognizeService一行为该功能所输出的log,这样就找到了PID为1859。再结合ps命令,在命令行获取进程名

              

这样就推断出该功能所在的进程名为com.huawei.systemserver,为框架模块。从前面log中的“package = com.android.systemui”,也可以推断是SystemUI启动了该功能。

 

 

出处:https://www.cnblogs.com/andy-songwei/p/9773379.html

前言       

       Android的adb提供了很多命令,功能很强大,可以为开发和调试带来很大的便利。当然本文并不是介绍各种命令的文章,而是用于记录在平时工作中需要经常使用的命令,方便平时工作时使用,所以以后会持续新增一些常用而且很有用的命令。也希望这些命令能给读者带来一定的帮助。

       当前已经记录的命令主要涉及到如下功能:

       一、常用的与Permission相关的adb命令

       二、通过Wifi连接连接手机

       三、adb操作四大组件

       四、实现一个操作多条adb命令的脚本

       五、用adb dump一些信息

       六、adb与进程

 

一、常用的与Permission相关的adb命令

       Android工具adb提供了一些命令,可以方便查看、授权、取消应用的权限,可以为调试程序带来不少的方便,下面简单介绍几个常见的命令。

  1、查看指定app中权限申请情况

       命令:adb shell dumpsys package [包名]

       用途:该命令用于获取该app的package信息,Permission信息只是其中的一部分。

       命令使用示例:

1 adb shell dumpsys package cn.aaa.bbb

       如下下截图为该命令中关于权限的部分信息:

       该图显示了4部分权限:

    (1)declared permissions。该应用自己声明(即自定义)的权限,这里显示了权限名,权限等级,以及在什么时候获取该权限(INSTALLED 表示安装的时候就会授予该权限)。

    (2)requested permissions。这里列出的是AndroidManifest.xml文件中所有request的权限,可以看出这里面包含了动态申请的权限和安装时申请的权限。
    (3)install permissions:安装的时候就赋予的权限。可以和requested permissions对比一下,这里面少了一"android.permission.CAMERA"权限,该权限为动态申请权限。该列表中还展示了权限对应的授予情况,如granted所示,true表示已经被授予了权限。

    (4)runtime permissions。这里显示的是运行时才需要申请的权限,即dangerous permission。

  2、查看权限的声明者和使用者

       命令:adb shell dumpsys package permission <权限名>

       用途:该命令可以查看指定权限是谁声明的,有哪些应用申请了该权限。

      命令使用示例:

1 adb shell dumpsys package permission cn.aaa.bbb.TEST_PERMISSION

如下节选了该权限的定义信息和其中一个使用该权限的应用的关键信息:

Copy the code
Copy the code
 1 Permissions:
 2   Permission [cn.aaa.bbb.TEST_PERMISSION] (d4d8316):
 3     sourcePackage=cn.aaa.bbb
 4     uid=10078 gids=null type=0 prot=signature|privileged
 5     perm=Permission{f5b497 cn.aaa.bbb.TEST_PERMISSION}
 6     packageSetting=PackageSetting{96e1684 cn.aaa.bbb/10078}
 7 
 8 Packages:
 9   Package [cn.xxx.xxx] (5d0f51b):
10     ......
11     declared permissions:
12     requested permissions:
13     install permissions:
14       cn.aaa.bbb.TEST_PERMISSION: granted=true
15 
16 ......
Copy the code
Copy the code

   3、移除指定权限

      命令:adb shell pm revoke [packageName] [permissionName]

      用途:移除packageName应用的permissionName权限(可以同时移除多项权限)。

      命令使用示例(如下为删除包名为cn.aaa.bbb 的相机权限):

1 adb shell pm revoke cn.aaa.bbb android.permission.CAMERA

执行完该命令后,用前文提到的命令“adb shell dumpsys package cn.aaa.bbb”查看该权限的信息如下:

 通过实验发现,该命令对runtime permissions有效,却对install permissions无效,如以下异常信息所示:

 

  4、授予指定权限

       命令:adb shell pm grant [packageName] [permissionName]

       用途:为packageName应用授予permissionName权限(可以同时授予多项权限)。该命令和上一条移除命令相对应。

       参照上一条命令的实例,实验结果如下:

  5、查看系统定义的所有权限

       命令:adb shell pm list permissions -s[option] 不加-s会显系统中定义的所有权限名列表,加了-s会显示对这些权限的用途说明。

      参考:【Viewing an app's permissions:https://developer.android.google.cn/guide/topics/permissions/overview#viewing

       下面截图分别展示了命令不加-s和加了-s后的显示结果(重定向到文本中查看),其中不加-s的截图中,一共显示了571条权限,这里截取了一部分,其中可以看到不少自定义的权限。

  6、按组查看权限

       命令:adb shell pm list permissions -d -g

       用途:查看权限的分组情况。这部分是上面一条命令的补充,参数可以根据自己的需要选择。

       参考:【https://developer.android.google.cn/training/permissions/usage-notes#testing

       下列截图为结果的一部分。

 

  7、授予所有权限

       命令:adb shell install -g MyApp.apk

       用途:当安装MyApp.apk到模拟器或测试机上时,如果加上-g,可以自动授予所有权限。这一点笔者没有实验过,读者可以自行测试。

       参考:该处和第4点一样参考官网说明。

   

二、通过Wifi连接连接手机

       有时候由于数据线异常等原因,导致电脑上连接不上手机,有时候会让人很抓狂。还有一种情况,当通过USB连接手机时,会干扰手机界面,影响分析问题,比如手机上会显示充电界面等。此时可以通过Wifi来连接手机,彻底摆脱数据线的困扰,从而放飞自我。这里简单介绍一下具体操作步骤,前提条件是Wifi需要和电脑在同一个局域网。

    (1)连接wifi,查看手机的ip地址

       大多数人都知道电脑有IP地址,却很少有人留意手机的IP地址。这提供两种查看手机IP的方式,当然前提条件是先连接上Wifi。

       1)在WIFI详情中查看

       依次进入 设置 》 WLAN,可以看到如下界面,依次进入到所选wifi的详情,便可查看到IP地址。

    

       2)在手机信息中查看

       依次按照路径 设置 》 关于手机 》 状态信息 》 IP地址,便可在如下界面找到IP地址。

     

    (2)给手机设置端口号

       这一步需要先通过USB连接电脑(所以还是不能完全脱离数据线),通过命令 adb tcp ip 5555 来设置。默认填5555就可以了,我试过其他的,没成功。

      

    (3)连接手机

       命令:adb connect [ip地址]

       实例:

        查看连接的设备,就能看到通过WIFI连接的手机了。

 

       在AndroidStudio上就可以选择该手机了,各种操作和用USB连接时都是一样的。

     (4)断开连接

       使用完后最好断开连接,断开命令为:adb disconnect [ip地址]

 

三、adb操作四大组件

  1、adb 启动activitry

       命令:adb shell am start -n pkgname/activityPath(可以是全路径,也可以是相对路径)

       如果成功,会看到界面被打开;如果失败,会在命令行处显示错误信息。 

       启动activity和service还有一种action方式打开,暂时没有实验成功,暂不记录。

 

  2、adb 启动service

       命令:adb shell am startservice -n pkgname/servicepath(可以是全路径,也可以是相对路径)

 

  3、adb 停止service

       命令:adb shell am stopservice -n pkgname/servicepath

 

 

  4、adb发送广播

      命令:adb shell am broadcast -a actionName --es EXTRA_KEY EXTRA_KEY_VALUE

      说明: -a 表示action,-es表示附件的键值对参数,且参数值为string(-ei对应int,-ez对应boolean)

      使用案例:

这句命令的作用就相当于执行了如下代码:

1 Intent intent = new Intent(“com.songwei.broadcast.demo”);
2 intent.putExtra(“pkgname”,“com.example.demos”);

       广播接收到的信息为:

 

四、实现一个操作多条adb命令的脚本

       有些固定操作需要执行多条adb命令,如果每次都手动输入,会比较麻烦。如果写一个脚本来自动执行,会方便很多。如下实现一个自动push SystemUI.apk的脚本:

 adb root
 adb remount
 adb push ./SystemUI.apk system/priv-app/SystemUI
 adb shell sleep 1
 adb reboot
 pause

 

五、用adb dump一些信息

  1、查看内存信息

       命令:adb shell dumpsys meminfo [进程名]

  2、查看当前显示的界面信息

       命令:adb shell dumpsys window | findstr mCurrentFocus

    

      

六、adb与进程

 1、查看进程

      adb shell

      ps -ef|grep [keyword] 关键字不用加引号,如下图所示:

      

  2、杀死进程

       adb shell

       kill -s -9 [pid] 示例如下所示:

            

  3、一个查看ps来找到某个service的案例

       需求:需要找到华为手机人脸解锁功能所在的进程。

       Difficulty: With Face Unlock, there is no corresponding interface, is a service judge in performing this function, you can not dump out the window information.

       Analysis: For there is no interface function, you can logcat, according to a key field to infer where the process ID, then by process ID, in conjunction with the ps command can be found in the corresponding process name, and then infer where the module. For example: the face Huawei machine determines which module is unlocked, to obtain a Log records, as follows:

                       

By tag name, you can determine the behavior of the log FaceRecognizeService a function of the output, so as to find the PID 1859. Combined with the ps command to get the process name in the command line

              

This deduce the function where the process called com.huawei.systemserver, the framework module. From the front of the log "package = com.android.systemui", it may be deduced SystemUI start this function.

 

Guess you like

Origin www.cnblogs.com/mq0036/p/11824273.html