Android ADB command collation, you do not know how to play here!

1. An overview of Android debugging bridges

The Android Debugging Bridge (adb) is a generic command-line tool that allows you to communicate with a simulator instance or connected Android device.It facilitates the operation of various devices, such as installing and debugging applications, and provides access to Unix shell s that can be used to run commands on simulators or connected devices.The tool acts as a client-server program consisting of three components:

Client, which sends commands.The client runs on the development computer.You can invoke a client from a command line terminal by issuing an adb command.

A daemon that runs commands on the device.The daemon runs as a background process on each emulator or device instance.

Server, which manages communication between clients and daemons.The server runs as a background process on the development computer.

2. How ADB works

When starting an adb client, the client first checks to see if any adb server processes are running.If not, it will start the server process.When the server starts, it binds to the local TCP port 5037 and listens for commands sent from adb clients - all adb clients use port 5037 to communicate with the adb server.

The server then sets up connections to all running simulator/device instances.It scans odd number ports between 5555 and 5585 (range of simulator/device use) to find simulator/device instances.Once the server discovers the adb daemon, it will set up a connection to the port.Note that each emulator/device instance will get a pair of sequentially ordered Ports - the even-number ports for console connections and the odd-number ports for adb connections.For example:

Simulator 1, Console: 5554
 Simulator 1, adb:5555
 Simulator 2, Console: 5556
 Simulator 2, adb:5557
 And so on...

As shown above, the simulator instance connected to adb at port 5555 is the same as the console instance listening on port 5554.

Once the server has set up connections to all simulator instances, you can access them using the adb command.Because the server manages connections to simulator/device instances and handles commands from multiple adb clients, you can control any simulator/device instance from any client (or from a script).

3.adb command

  1. Display all Android platforms in the system:
android list targets
  1. Display all AVD s in the system:
android list avd
  1. Create AVD (Simulator):
android create avd --name name--target platform number
  1. Start the simulator:
Emulator-avd name-sdcard ~/name.img (-skin 1280x800)
  1. Delete AVD (simulator):
android delete avd --name Name
  1. Create SDCard:
mksdcard 1024M ~/Name.img
  1. AVD (simulator) location:
Linux(~/.android/avd)      Windows(C:\Documents and Settings\Administrator\.android\avd)
  1. Start DDMS:
  1. Show all currently running simulators:
adb devices
  1. Execute a command on an emulator:
Adb-s simulator number command
  1. Install the application:
ADB install-r application.apk
  1. Get the files in the simulator:
adb pull <remote> <local>
  1. Write files to the simulator:
adb push <local> <remote>
  1. Enter shell mode of simulator:
adb shell
  1. Start SDK, Document, Instance Download Manager:
  1. Unloaded apk package:
adb shell 
cd data/app 
rm apk package 
adb uninstall apk The primary package name of the package 
adb install -r apk package
  1. View adb command help information:
adb help
  1. View LOG information from the command line:
ADB logcat-s tag name
  1. Commands that follow the adb shell come from:

Sourcesystemcoretoolbox directory and source frameworks\base\cmds directory.

  1. Delete system applications:
adb remount (Remount the system partition to make it writable again). 
adb shell 
cd system/app 
rm *.apk
  1. Get administrator privileges:
adb root
  1. Start Activity:
ADB shell a m start-n package n a m e/package n a m e + class n a m e (-n class n a m e, -a action,-d date,-m MIME-TYPE,-c category,-e extended data, etc.).

23. Publishing Port:

You can set any port number as the host's request port to the emulator or device.For example:

adb forward tcp:5555 tcp:8000

24. Copy files:

You can copy a file or directory to a device or from a device to a device or simulator:

adb push <source> <destination></destination></source> 
//For example: ADB push test.txt/tmp/test.txt 
//Copy a file or directory from the device or simulator: 
adb pull <source> <destination></destination></source> 
//For example: ADB pull/android/lib/

25. Examples of search simulators/devices:

Get a list of currently running instances of the simulator/device and the status of each:

adb devices

26. View bug reports:

adb bugreport

27. Record wireless communication logs:

Generally speaking, there are a lot of logs for wireless communication, so it is not necessary to record them at run time, but we can set them up by command:

adb shell 
logcat -b radio

28. Obtain the ID and serial number of the device:

adb get-product 
adb get-serialno

29. Accessing database SQLite3

adb shell 


Usage: getevent [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]  
    -t: show time stamps  
    -n: don't print newlines  
    -s: print switch states for given bits  
    -S: print all switch states  
    -v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)  
    -d: show HID descriptor, if available  
    -p: show possible events (errs, dev, name, pos. events)  
    -i: show all device info and possible events  
    -l: label event types and names in plain text  
    -q: quiet (clear verbosity mask)  
    -c: print given number of events then exit  
    -r: print rate events are received  
Usage: sendevent <device> <type> <code> <value>  

Get the arrow key press and lift events through getevent, then send the corresponding events with sendevent to simulate the arrow key press and lift, thus achieving the functions defined on the arrow key locally. Following is a python wrapped adb command to trigger the arrow key press and lift, achieving a large number of functions defined on the arrow key to trigger automatic testing.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time

def main(times):
    os.popen("adb wait-for-device")
    for i in range(times):
        print("times:==>" + str(i + 1))
        #Analog Arrow Key-Up
        send_direction_key_event("Arrow keys-Up and down", "Arrow keys-Up lift", 103, 5)
        #Analog Arrow Key-Down
        send_direction_key_event("Arrow keys-Press down", "Arrow keys-Lift Down", 108, 5)
        #Analog Arrow Key-Left
        send_direction_key_event("Arrow keys-Press left", "Arrow keys-Left lift",105, 1)

def send_direction_key_event(keytipdown, keytipup, value, sleeptime):
    os.popen("adb shell sendevent /dev/input/event0 1 "+ str(value) + " 1")
    os.popen("adb shell sendevent /dev/input/event0 0 0 0")
    os.popen("adb shell sendevent /dev/input/event0 1 "+ str(value) + " 0")
    os.popen("adb shell sendevent /dev/input/event0 0 0 0")

if __name__ == "__main__":
    #20 seconds at a time, 24 * 60 * 60 seconds a day, 4320 tests, set to 10000 below

56 original articles published. 39% praised. 100,000 visits+
Private letter follow

Tags: simulator shell Android emulator

Posted on Wed, 15 Jan 2020 20:34:10 -0500 by evildobbi