Appium foundation - 5. Common ADB commands

catalogue

9. View mobile phone operation log

ADB command: adb logcat

Any operation on Android phone / simulator will be recorded in the log.

The log of Android system is divided into two parts. The bottom Linux kernel log is output to / proc/kmsg, and the Android log is output to / dev/log.

(1) Android log

Command format:

[adb] logcat [<option>] ... [<filter-spec>] ...

Common usage is listed as follows:

(2) Filter logs by level

Android logs are divided into the following priorities:

  • 5: V erbose (minimum, maximum output)
  • D: Debug
  • I: Info
  • W: Warning
  • E: Error
  • F: Fatal
  • S: Silent (highest, nothing output)

Filtering logs by a certain level will output logs of that level and above.

For example, the command:

adb logcat *:W

The Warning, Error, Fatal, and Silent logs are output.

Note: under macOS, you need to add double quotation marks to the parameter with *: w as tag, such as adb logcat "*:W", otherwise an error no matches found: *:W will be reported.)

(3) Filter logs by tag and level

< filter spec > can be composed of multiple < tag > [: priority].

For example, the command:

adb logcat ActivityManager:I MyApp:D *:S

Indicates that the output tag is the log above Info level of ActivityManager, the output tag is the log above Debug level of MyApp, and the Silent level log of other tags (i.e. shielding other tag logs).

(4) Log format

You can specify the log output format with the ADB logcat - V < Format > option.

Log supports the following < Format >:

  • brief
    The default format is:
    <priority>/<tag>(<pid>): <message>
    
    Example:
    D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0
    
  • process
    The format is:
    <priority>(<pid>) <message>
    
    Example:
    D( 1785) Disconnected process message: 10, size: 0  (HeadsetStateMachine)
    
  • tag
    The format is:
    <priority>/<tag>: <message>
    
    Example:
    D/HeadsetStateMachine: Disconnected process message: 10, size: 0
    
  • raw
    The format is:
    <message>
    
    Example:
    Disconnected process message: 10, size: 0
    
  • time
    The format is:
    <datetime> <priority>/<tag>(<pid>): <message>
    
    Example:
    08-28 22:39:39.974 D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0
    
  • threadtime
    The format is:
    <datetime> <pid> <tid> <priority> <tag>: <message>
    
    Example:
    08-28 22:39:39.974  1785  1832 D HeadsetStateMachine: Disconnected process message: 10, size: 0
    
  • long
    The format is:
    [ <datetime> <pid>:<tid> <priority>/<tag> ]
    <message>
    
    Example:
    [ 08-28 22:39:39.974  1785: 1832 D/HeadsetStateMachine ]
    Disconnected process message: 10, size: 0
    
    The specified format can be used with the above filter. For example:
    adb logcat -v long ActivityManager:I *:S
    

(5) Clear log

The command is as follows:

adb logcat -c

10. Get the package name and startup name of APP

Before executing the APP automation test, you must obtain the appPackage (package name) and appActivity (startup name) of the tested APP, and then fill these two values into the specified parameters of the script to tell which APP to execute.

appPackage (package name): determines the uniqueness of the program (not the name of the application). The package name of the APP cannot be seen. You need to execute a command to view it.

appActivity (startup name): at present, it can be understood that a startup name corresponds to a page.

There are many ways to obtain the package name and startup name of APP (choose one):

Mode 1:

First open the App to be obtained in the device.

Enter the command adb shell dumpsys window windows | findstr mFocusedApp

For example, Taobao APP is as follows:

# Link free simulator
C:\Users\L>adb connect 127.0.0.1:21503
adb server version (31) doesn't match this client (41); killing...
* daemon started successfully
connected to 127.0.0.1:21503

# Get the package name and startup name of Taobao App home page
C:\Users\L>adb shell dumpsys window windows | findstr mFocusedApp
  mFocusedApp=AppWindowToken{1554db9 token=Token{a292380 ActivityRecord{9c92803 u0 com.taobao.taobao/com.taobao.tao.TBMainActivity t16}}}

C:\Users\L>

We can see from the above that Taobao APP:

appPackage: com.taobao.taobao

appActivity: com.taobao.tao.TBMainActivity

When we click tmall global on Taobao APP's home page, we can get the package name and startup name of Taobao APP again.

C:\Users\L>adb shell dumpsys window windows | findstr mFocusedApp
  mFocusedApp=AppWindowToken{f99dcd9 token=Token{8b7fb20 ActivityRecord{ab41a23 u0 com.taobao.taobao/com.taobao.browser.BrowserActivity t16}}}

C:\Users\L>

We can see from the above that Taobao APP:

appPackage: com.taobao.taobao

appActivity: com.taobao.browser.BrowserActivity

Tip: the content before the slash is the package name, and the content after the slash is the startup name.

Mode 2:

Obtain appPackage and appActivity through the. apk program file.

That is, obtain the package name / startup name of apk on the computer, which is not installed on the device at this time.

Step 1:

From the command line, enter the directory where the aapt.exe program is located in the Android SDK directory.

As shown in the figure:

Step 2:

Enter the command AAPT dump bagging < path_ to_ apk>

Example:

Enter the directory where the aapt.exe program is located, enter cmd in the address bar, press enter, and enter the command line terminal.

Enter the command line terminal after entering, as shown in the following figure:

Enter the AAPT dump bagging + APK path command

F:\DevInstall\envs\android-sdk-windows\build-tools\25.0.3>aapt dump badging C:\Users\L\Desktop\com.taobao.taobao_V9.15.0.apk

Executing this command will have many contents, including

  • package: name='com.taobao.taobao 'is the package name information of the app.
  • Launch activity: name ='com. Taobao. Tao. Welcome. Welcome 'is the information of the launch page of the APP. (different from the above method)

Tips:

Difference between APP startup name and startup page

The startup name refers to the name of the current startup page. The startup page is an APP welcome page that is started,

So the startup name contains the startup page.

11. Get APP start time

ADB command: adb shell am start -W package name / startup name

Example:

# Link free simulator
C:\Users\L>adb connect 127.0.0.1:21503
adb server version (31) doesn't match this client (41); killing...
* daemon started successfully
connected to 127.0.0.1:21503

C:\Users\L>


# Get the package name and startup name of Taobao App home page
C:\Users\L>adb shell dumpsys window windows | findstr mFocusedApp
  mFocusedApp=AppWindowToken{48d0f7f token=Token{a1f589e ActivityRecord{f5ca6d9 u0 com.taobao.taobao/com.taobao.tao.TBMainActivity t17}}}

C:\Users\L>


# Get the start time of Taobao app
# Package name: com.taobao.taobao
# Startup Name: com.taobao.tao.TBMainActivity
C:\Users\L>adb shell am start -W com.taobao.taobao/com.taobao.tao.TBMainActivity
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.taobao.taobao/com.taobao.tao.TBMainActivity }
Warning: Activity not started, its current task has been brought to the front
Status: ok
Activity: com.taobao.taobao/com.taobao.tao.TBMainActivity
ThisTime: 0
TotalTime: 0
WaitTime: 3
Complete

C:\Users\L>

Explanation: there are three times

  • TotalTime: APP startup time.
  • WaitTime: the time the system starts the application (that is, the time it takes the system to start the application).
  • ThisTime: the time when the startup page is opened in the APP.

Relationship between the three: waittime = totaltime + thistime

12. View memory information

ADB command: ADB shell dumpsys meminfo < package_ name>

Where, package_name can also be replaced by the pid of the program. The pid can be through adb shell top | grep app_name command.

Demo command:

C:\Users\L>adb shell dumpsys meminfo com.taobao.taobao
Applications Memory Usage (in Kilobytes):
Uptime: 904975 Realtime: 904975

** MEMINFO in pid 1413 [com.taobao.taobao] **
                   Pss  Private  Private     Swap     Heap     Heap     Heap
                 Total    Dirty    Clean    Dirty     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------   ------
  Native Heap        0        0        0        0    80384    65337    15046
  Dalvik Heap    56525    56392        0        0    63014    46630    16384
 Dalvik Other     9119     9116        0        0
        Stack     2680     2680        0        0
       Ashmem      344        4        0        0
    Other dev       26        0       24        0
     .so mmap    21428     3644     9028        0
    .apk mmap     2090        0      704        0
    .ttf mmap      176        0       24        0
    .dex mmap    46945       16    18444        0
    .oat mmap     6123        0      848        0
    .art mmap     2685     2252        0        0
   Other mmap     4808       16     2288        0
      Unknown    77191    77120        0        0
        TOTAL   230140   151240    31360        0   143398   111967    31430

...Omit the following...

Tips:
Run the above command in the command line window to get the memory information of the program as follows:

Mainly focus on:

  • Heap information of Native/Dalvik.
    Specifically, in the first and second lines above, it shows the memory allocation of JNI layer and Java layer respectively. If it is found that this value has been increasing, it indicates that the program may have a memory leak.
  • PSS information for Total.
    This value is the real memory size occupied by your application. Through this information, you can easily identify which programs in the mobile phone occupy a large memory.

Summary: that is, just look at the first two rows and the first two columns.

Posted on Wed, 03 Nov 2021 15:38:23 -0400 by randalusa