Android ADB source code analysis summary

ADB summary of Android

The contents of this paper are as follows:

1. makefile analysis and summary

2. Introduction to adb framework

3. adbd source code analysis

3.1} adbd initialization process analysis

3.2 # adb # shell process analysis

3.3 # adb # root process analysis

4. adb common commands

1, makefile analysis and summary

   
  1. // The following is an excerpt from android/ system/core/adb/Android.mk
  2. # Copyright 2005 The Android Open Source Project
  3. #
  4. # Android.mk for adb
  5. #
  6. LOCAL_PATH:= $(call my-dir)
  7. // Using HOST_OS macro is compatible with different PC operating systems, such as windows, linux, mac, etc.
  8. ife q ($(HOST_OS),windows)
  9. adb_host_clang := false # libc++ for mingw not ready yet.
  10. else
  11. adb_host_clang := true
  12. endif
  13. adb_version := $(shell git -C $(LOCAL_PATH) rev-parse --short= 12 HEAD 2> /dev/null)-android
  14. ADB_COMMON_CFLAGS := \
  15. -Wall -Werror \
  16. -Wno-unused-parameter \
  17. -DADB_REVISION= '"$(adb_version)"' \
  18. # libadb
  19. # =========================================================
  20. # Much of adb is duplicated in bootable/recovery/minadb and fastboot. Changes
  21. # made to adb rarely get ported to the other two, so the trees have diverged a
  22. # bit. We'd like to stop this because it is a maintenance nightmare, but the
  23. # divergence makes this difficult to do all at once. For now, we will start
  24. # small by moving common files into a static library. Hopefully some day we can
  25. # get enough of adb in here that we no longer need minadb. https://b/17626262
  26. LIBADB_SRC_FILES := \
  27. adb.cpp \
  28. adb_auth.cpp \
  29. adb_io.cpp \
  30. adb_listeners.cpp \
  31. adb_utils.cpp \
  32. sockets.cpp \
  33. transport.cpp \
  34. transport_local.cpp \
  35. transport_usb.cpp \
  36. LIBADB_TEST_SRCS := \
  37. adb_io_test.cpp \
  38. adb_utils_test.cpp \
  39. transport_test.cpp \
  40. LIBADB_CFLAGS := \
  41. $(ADB_COMMON_CFLAGS) \
  42. -Wno-missing-field-initializers \
  43. -fvisibility=hidden \
  44. LIBADB_darwin_SRC_FILES := \
  45. fdevent.cpp \
  46. get_my_path_darwin.cpp \
  47. usb_osx.cpp \
  48. LIBADB_linux_SRC_FILES := \
  49. fdevent.cpp \
  50. get_my_path_linux.cpp \
  51. usb_linux.cpp \
  52. LIBADB_windows_SRC_FILES := \
  53. get_my_path_windows.cpp \
  54. sysdeps_win32.cpp \
  55. usb_windows.cpp \
  56. include $(CLEAR_VARS)
  57. LOCAL_CLANG := true
  58. // Compile and generate libadbd static library for use by adbd deamon process
  59. LOCAL_MODULE := libadbd
  60. LOCAL_CFLAGS := $(LIBADB_CFLAGS) -DADB_HOST= 0
  61. LOCAL_SRC_FILES := \
  62. $(LIBADB_SRC_FILES) \
  63. adb_auth_client.cpp \
  64. fdevent.cpp \
  65. jdwp_service.cpp \
  66. qemu_tracing.cpp \
  67. usb_linux_client.cpp \
  68. LOCAL_SHARED_LIBRARIES := libbase
  69. include $(BUILD_STATIC_LIBRARY)
  70. include $(CLEAR_VARS)
  71. LOCAL_CLANG := $(adb_host_clang)
  72. // Compile and generate libadb dynamic library for pc adb executable
  73. LOCAL_MODULE := libadb
  74. LOCAL_CFLAGS := $(LIBADB_CFLAGS) -DADB_HOST= 1
  75. LOCAL_SRC_FILES := \
  76. $(LIBADB_SRC_FILES) \
  77. $(LIBADB _$(HOST_OS)_SRC_FILES) \
  78. adb_auth_host.cpp \
  79. LOCAL_SHARED_LIBRARIES := libbase
  80. # Even though we're building a static library (and thus there's no link step for
  81. # this to take effect), this adds the SSL includes to our path.
  82. LOCAL_STATIC_LIBRARIES := libcrypto_static
  83. ife q ($(HOST_OS),windows)
  84. LOCAL_C_INCLUDES += development/host/windows/usb/api/
  85. endif
  86. include $(BUILD_HOST_STATIC_LIBRARY)
  87. include $(CLEAR_VARS)
  88. LOCAL_CLANG := true
  89. // Compile the test program to generate adbd
  90. LOCAL_MODULE := adbd_test
  91. LOCAL_CFLAGS := -DADB_HOST= 0 $(LIBADB_CFLAGS)
  92. LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS)
  93. LOCAL_STATIC_LIBRARIES := libadbd
  94. LOCAL_SHARED_LIBRARIES := liblog libbase libcutils
  95. include $(BUILD_NATIVE_TEST)
  96. include $(CLEAR_VARS)
  97. LOCAL_CLANG := $(adb_host_clang)
  98. // Compile the test program to generate adb
  99. LOCAL_MODULE := adb_test
  100. LOCAL_CFLAGS := -DADB_HOST= 1 $(LIBADB_CFLAGS)
  101. LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.cpp
  102. LOCAL_SHARED_LIBRARIES := liblog libbase
  103. LOCAL_STATIC_LIBRARIES := \
  104. libadb \
  105. libcrypto_static \
  106. libcutils \
  107. ife q ($(HOST_OS),linux)
  108. LOCAL_LDLIBS += -lrt -ldl -lpthread
  109. endif
  110. ife q ($(HOST_OS),darwin)
  111. LOCAL_LDLIBS += -framework CoreFoundation -framework IOKit
  112. endif
  113. include $(BUILD_HOST_NATIVE_TEST)
  114. # adb device tracker (used by ddms) test tool
  115. # =========================================================
  116. ife q ($(HOST_OS),linux)
  117. include $(CLEAR_VARS)
  118. LOCAL_CLANG := $(adb_host_clang)
  119. // Compile and generate ADB_ device_ Test program of tracker service
  120. LOCAL_MODULE := adb_device_tracker_test
  121. LOCAL_CFLAGS := -DADB_HOST= 1 $(LIBADB_CFLAGS)
  122. LOCAL_SRC_FILES := test_track_devices.cpp
  123. LOCAL_SHARED_LIBRARIES := liblog libbase
  124. LOCAL_STATIC_LIBRARIES := libadb libcrypto_static libcutils
  125. LOCAL_LDLIBS += -lrt -ldl -lpthread
  126. include $(BUILD_HOST_EXECUTABLE)
  127. endif
  128. # adb host tool
  129. # =========================================================
  130. include $(CLEAR_VARS)
  131. ife q ($(HOST_OS),linux)
  132. LOCAL_LDLIBS += -lrt -ldl -lpthread
  133. LOCAL_CFLAGS += -DWORKAROUND_BUG6558362
  134. endif
  135. ife q ($(HOST_OS),darwin)
  136. LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
  137. LOCAL_CFLAGS += -Wno-sizeof-pointer-memaccess -Wno-unused-parameter
  138. endif
  139. ife q ($(HOST_OS),windows)
  140. LOCAL_LDLIBS += -lws2_32 -lgdi32
  141. EXTRA_STATIC_LIBS := AdbWinApi
  142. endif
  143. LOCAL_CLANG := $(adb_host_clang)
  144. LOCAL_SRC_FILES := \
  145. adb_main.cpp \
  146. console.cpp \
  147. commandline.cpp \
  148. adb_client.cpp \
  149. services.cpp \
  150. file_sync_client.cpp \
  151. LOCAL_CFLAGS += \
  152. $(ADB_COMMON_CFLAGS) \
  153. -D_GNU_SOURCE \
  154. -DADB_HOST= 1 \
  155. // Compile and generate the adb executable file on the PC side, which is located in the SDK / platform tools / adb directory
  156. LOCAL_MODULE := adb
  157. LOCAL_MODULE_TAGS := debug
  158. LOCAL_STATIC_LIBRARIES := \
  159. libadb \
  160. libbase \
  161. libcrypto_static \
  162. libcutils \
  163. liblog \
  164. $(EXTRA_STATIC_LIBS) \
  165. # libc++ not available on windows yet
  166. ifne q ($(HOST_OS),windows)
  167. LOCAL_CXX_STL := libc++_static
  168. endif
  169. # Don't add anything here, we don't want additional shared dependencies
  170. # on the host adb tool, and shared libraries that link against libc++
  171. # will violate ODR
  172. LOCAL_SHARED_LIBRARIES :=
  173. include $(BUILD_HOST_EXECUTABLE)
  174. $(call dist- for-goals,dist_files sdk,$(LOCAL_BUILT_MODULE))
  175. ife q ($(HOST_OS),windows)
  176. $(LOCAL_INSTALLED_MODULE): \
  177. $(HOST_OUT_EXECUTABLES)/AdbWinApi.dll \
  178. $(HOST_OUT_EXECUTABLES)/AdbWinUsbApi.dll
  179. endif
  180. # adbd device daemon
  181. # =========================================================
  182. include $(CLEAR_VARS)
  183. LOCAL_CLANG := true
  184. LOCAL_SRC_FILES := \
  185. adb_main.cpp \
  186. services.cpp \
  187. file_sync_service.cpp \
  188. framebuffer_service.cpp \
  189. remount_service.cpp \
  190. set_verity_enable_state_service.cpp \
  191. LOCAL_CFLAGS := \
  192. $(ADB_COMMON_CFLAGS) \
  193. -DADB_HOST= 0 \
  194. -D_GNU_SOURCE \
  195. -Wno-deprecated-declarations \
  196. // AUTH certification is not required in engineer mode
  197. LOCAL_CFLAGS += -DALLOW_ADBD_NO_AUTH=$( if $(filter userdebug eng,$(TARGET_BUILD_VARIANT)), 1, 0)
  198. ifne q (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
  199. LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY= 1
  200. LOCAL_CFLAGS += -DALLOW_ADBD_ROOT= 1
  201. endif
  202. // Compile and generate the adbd daemon process on the device side or simulator side
  203. LOCAL_MODULE := adbd
  204. LOCAL_FORCE_STATIC_EXECUTABLE := true
  205. LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
  206. LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
  207. LOCAL_C_INCLUDES += system/extras/ext4_utils
  208. LOCAL_STATIC_LIBRARIES := \
  209. libadbd \
  210. libbase \
  211. libfs_mgr \
  212. liblog \
  213. libcutils \
  214. libc \
  215. libmincrypt \
  216. libselinux \
  217. libext4_utils_static \
  218. include $(BUILD_EXECUTABLE)

Summary of key points:

   
  1. be-all adb Procedure( PC End and device end) share a set of codes, only with the help of HOST_OS This macro is isolated, and two executable files will be generated during compilation:
  2. 1.adb/adb.exe: Run on PC end(include Windows,Linux,MacOS And other operating systems),be located platform/tools/adb Directory, through adb Commands can evoke.
  3. 2.adbd: Run on Android On the device, it starts automatically when it is turned on.
  4. Accordingly, the source code will also be ADB_HOST Macros are used to distinguish local hosts(adb)And target machine(adbd).
  5. Engineer Defined in mode ALLOW_ADBD_NO_AUTH,It will be used in the source code.

2, Introduction to adb framework

ADB It is the abbreviation of "Android Debug Bridge", which is essentially a command line tool based on "C/S" architecture. The whole ADB module consists of the following parts (see system / core / ADB / overviews.txt and transports.txt for details):

  1. The # ADB # server(adb): a background application running on the PC side, which is used to detect the connection or removal of Android # devices, and then maintain the device status list. In addition, ADB # Server will also be responsible for coordinating the data transmission between Client, Services and Android # devices.
  2. The # ADB # daemon (adbd): a background daemon running on Android # Devices or Emulator. This process is mainly used to connect to ADB} Server and provide some services for ADB} clients through USB (or TCP).
  3. The ADB command line client: the terminal that executes the ADB command. Specifically, it will connect to the ADB server and send a request command to the ADB server. If it is found that the ADB server is not started, it will automatically wake up the ADB server
  4. Services: some of The capabilities of ADB can be abstracted into services for The # ADB # command line # client to access. It can be divided into Host services and Local services, which are briefly described as follows:
   
  1. a) Host Services: Run on ADB  Server,No and devices Data exchange. Typical is execution adb devices Command, just adb  server The client returns the current adb devices The status of.
  2. b)Local Services: Run on adbd Daemons, ADB  Server Responsible for establishing adb clients and Local Services And transparently transmit data.

Note: This article only analyzes the implementation of ADBD.

3, adbd source code analysis

  1. adbd initialization process analysis

ADB is divided into two modes: USB ADB and wifi ADB. They are mutually exclusive. USB ADB is used by default If you want to start wifi # ADB by default, you need to set the property service adb. tcp. The port value is 55555, and then restart adbd. ADB daemon(usb adb.) The main steps of program startup are shown in the figure below.

 

The description is as follows:

  • DEFAULT_ADB_PORT is the port number of adb} server. It is defined as follows. The general value is 5037. Sometimes the ADB on the PC cannot be used, which may be because the port has been occupied by mobile assistants such as Tencent.
   
  1. #if ADB_HOST_ON_TARGET
  2. /* adb and adbd are coexisting on the target, so use 5038 for adb
  3. * to avoid conflicting with adbd's usage of 5037
  4. */
  5. # define DEFAULT_ADB_PORT 5038
  6. #else
  7. # define DEFAULT_ADB_PORT 5037
  8. #endif
  • 1.1.1-init_ transport_ The analysis of registration() is as follows:
   
  1. void init_transport_registration( void)
  2. {
  3. int s[ 2];
  4. // Create two-way communication pipeline, full duplex
  5. if( adb_socketpair(s)){
  6. fatal_errno( "cannot open transport registration socketpair");
  7. }
  8. D( "socketpair: (%d,%d)", s[ 0], s[ 1]);
  9. transport_registration_send = s[ 0]; // send data
  10. transport_registration_recv = s[ 1]; // receive data
  11. // transport_ registration_ After recv receives data, it will trigger transport_registration_func execution
  12. fdevent_install(&transport_registration_fde,
  13. transport_registration_recv,
  14. transport_registration_func, // This function is very heavy!!
  15. 0);
  16. fdevent_set(&transport_registration_fde, FDE_READ);
  17. }

Therefore, we need to analyze transport next_ registration_ Recv will receive data, i.e. transport_ registration_ When will send send data.

  • 1.1.2-adbd_cloexec_auth_socket() is analyzed as follows:
   
  1. void adbd_cloexec_auth_socket() {
  2. int fd = android_get_control_socket( "adbd");
  3. if (fd == -1) {
  4. D( "Failed to get adbd socket\n");
  5. return;
  6. }
  7. fcntl(fd, F_SETFD, FD_CLOEXEC);
  8. }
  • 1.1.3-should_drop_privileges() is analyzed as follows:
   
  1. // adbd defaults to root permission, that is, the highest permission. This is for security needs. Consider whether to downgrade, that is, reduce permissions.
  2. static bool should_drop_privileges() {
  3. #if defined(ALLOW_ADBD_ROOT)
  4. char value[PROPERTY_VALUE_MAX];
  5. // The simulator is not degraded
  6. // The emulator is never secure, so don't drop privileges there.
  7. // TODO: this seems like a bug --- shouldn't the emulator behave like a device?
  8. property_get( "ro.kernel.qemu", value, "");
  9. if ( strcmp(value, "1") == 0) {
  10. return false;
  11. }
  12. // The properties that affect `adb root` and `adb unroot` are ro.secure and
  13. // ro.debuggable. In this context the names don't make the expected behavior
  14. // particularly obvious.
  15. //
  16. // ro.debuggable:
  17. // Allowed to become root, but not necessarily the default. Set to 1 on
  18. // eng and userdebug builds.
  19. //
  20. // ro.secure:
  21. // Drop privileges by default. Set to 1 on userdebug and user builds.
  22. property_get( "ro.secure", value, "1");
  23. bool ro_secure = ( strcmp(value, "1") == 0);
  24. property_get( "ro.debuggable", value, "");
  25. bool ro_debuggable = ( strcmp(value, "1") == 0);
  26. // Drop privileges if ro.secure is set...
  27. bool drop = ro_secure;
  28. property_get( "service.adb.root", value, "");
  29. bool adb_root = ( strcmp(value, "1") == 0);
  30. bool adb_unroot = ( strcmp(value, "0") == 0);
  31. // The default debug mode will not be degraded, that is, adb has root permission
  32. // ...except "adb root" lets you keep privileges in a debuggable build.
  33. if (ro_debuggable && adb_root) {
  34. drop = false;
  35. }
  36. // ...and "adb unroot" lets you explicitly drop privileges.
  37. if (adb_unroot) {
  38. drop = true;
  39. }
  40. return drop;
  41. #else
  42. return true; // "adb root" not allowed, always drop privileges.
  43. #endif /* ALLOW_ADBD_ROOT */
  44. }
  • 1.1.4-usb_init() analysis is as follows:
   
  1. void usb_init()
  2. {
  3. if (access(USB_FFS_ADB_EP0, F_OK) == 0)
  4. usb_ffs_init();
  5. else
  6. usb_adb_init(); //Take this case, and then analyze this function
  7. }
  8. continue usb_adb_init()analysis...
  9. static void usb_adb_init()
  10. {
  11. usb_handle* h = reinterpret_cast<usb_handle*>(calloc( 1, sizeof(usb_handle)));
  12. if (h == nullptr) fatal( "couldn't allocate usb_handle");
  13. h->write = usb_adb_write;
  14. h->read = usb_adb_read;
  15. h->kick = usb_adb_kick;
  16. h->fd = -1;
  17. adb_cond_init(&h->notify, 0);
  18. adb_mutex_init(&h-> lock, 0);
  19. // Open the file /dev/android_adb_enable to trigger
  20. // the enabling of the adb USB function in the kernel.
  21. // We never touch this file again - just leave it open
  22. // indefinitely so the kernel will know when we are running
  23. // and when we are not.
  24. // Open android_adb_enable node, i.e. enable adb usb
  25. int fd = unix_open( "/dev/android_adb_enable", O_RDWR);
  26. if (fd < 0) {
  27. D( "failed to open /dev/android_adb_enable\n");
  28. } else {
  29. close_on_exec(fd);
  30. }
  31. D( "[ usb_init - starting thread ]\n");
  32. adb_thread_t tid;
  33. if(adb_thread_create(&tid, usb_adb_open_thread, h)){ //Next, analyze this function
  34. fatal_errno( "cannot create usb thread");
  35. }
  36. }
  37. continue usb_adb_open_thread()analysis...
  38. static void *usb_adb_open_thread( void *x)
  39. {
  40. struct usb_handle *usb = ( struct usb_handle *)x;
  41. int fd;
  42. while ( true) {
  43. // wait until the USB device needs opening
  44. adb_mutex_lock(&usb-> lock);
  45. while (usb->fd != -1)
  46. adb_cond_wait(&usb->notify, &usb-> lock);
  47. adb_mutex_unlock(&usb-> lock);
  48. D( "[ usb_thread - opening device ]\n");
  49. do {
  50. /* XXX use inotify? */
  51. fd = unix_open( "/dev/android_adb", O_RDWR); //Open adb node
  52. if (fd < 0) {
  53. // to support older kernels
  54. fd = unix_open( "/dev/android", O_RDWR);
  55. }
  56. if (fd < 0) {
  57. adb_sleep_ms( 1000);
  58. }
  59. } while (fd < 0);
  60. D( "[ opening device succeeded ]\n");
  61. close_on_exec(fd);
  62. usb->fd = fd; //Assign the adb node to USB - > FD, which is important!
  63. D( "[ usb_thread - registering device ]\n");
  64. register_usb_transport(usb, 0, 0, 1); //Next, analyze this function
  65. }
  66. // never gets here
  67. return 0;
  68. }
  69. continue register_usb_transport()analysis...
  70. //transport.cpp
  71. void register_usb_transport(usb_handle *usb, const char *serial, const char *devpath, unsigned writeable)
  72. {
  73. atransport *t = reinterpret_cast<atransport*>(calloc( 1, sizeof(atransport)));
  74. if (t == nullptr) fatal( "cannot allocate USB atransport");
  75. D( "transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb, serial ? serial : "");
  76. // Initialize atlansport
  77. init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM));
  78. if(serial) {
  79. t->serial = strdup(serial);
  80. }
  81. if(devpath) {
  82. t->devpath = strdup(devpath);
  83. }
  84. adb_mutex_lock(&transport_lock);
  85. t->next = &pending_list;
  86. t->prev = pending_list.prev;
  87. t->next->prev = t;
  88. t->prev->next = t;
  89. adb_mutex_unlock(&transport_lock);
  90. register_transport(t); //Next, analyze this function
  91. }
  92. continue register_transport(t)analysis...
  93. /* the fdevent select pump is single threaded */
  94. static void register_transport(atransport *transport)
  95. {
  96. tmsg m;
  97. m.transport = transport;
  98. m.action = 1;
  99. D( "transport: %s registered\n", transport->serial);
  100. //See this socket fd: transport_registration_send, we thought of 1.1.1 - init_ transport_ Problems left in registration(). Let's continue...
  101. if(transport_write_action(transport_registration_send, &m)) {
  102. fatal_errno( "cannot write transport registration socket\n");
  103. }
  104. }
  105. continue transport_write_action(transport_registration_send, &m)analysis...
  106. static int
  107. transport_write_action( int fd, struct tmsg* m)
  108. {
  109. char *p = ( char*)m;
  110. int len = sizeof(*m);
  111. int r;
  112. while(len > 0) {
  113. r = adb_write(fd, p, len); // Here is to transport_registration_send write data!!!!
  114. if(r > 0) {
  115. len -= r;
  116. p += r;
  117. } else {
  118. if((r < 0) && (errno == EINTR)) continue;
  119. D( "transport_write_action: on fd %d, error %d: %s\n",
  120. fd, errno, strerror(errno));
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  • Transport will then be called_ registration_ func(int _fd, unsigned ev, void *data)
   
  1. static void transport_registration_func( int _fd, unsigned ev, void *data)
  2. {
  3. int s[ 2];
  4. ......
  5. if(adb_socketpair(s)) {
  6. fatal_errno( "cannot open transport socketpair");
  7. }
  8. D( "transport: %s socketpair: (%d,%d) starting", t->serial, s[ 0], s[ 1]);
  9. t->transport_socket = s[ 0];
  10. t->fd = s[ 1];
  11. fdevent_install(&(t->transport_fde), t->transport_socket,transport_socket_events,t); //important
  12. fdevent_set(&(t->transport_fde), FDE_READ);
  13. // From the perspective of adb driver, create adb input channel
  14. if(adb_thread_create(&input_thread_ptr, input_thread, t)){
  15. fatal_errno( "cannot create input thread");
  16. }
  17. // From the perspective of adb driver, create adb output channel
  18. if(adb_thread_create(&output_thread_ptr, output_thread, t)){
  19. fatal_errno( "cannot create output thread");
  20. }
  21. ......
  22. }

The follow-up process is as follows:

So far, the main initialization process of adbd has been analyzed. The key is to initialize the adb driver node and then create the output_thread constantly reads the changes of node content

2. adb} shell process analysis

According to Section 3.1, output_thread reads the data from the adb driver node and calls write_. Packet (T - > FD, T - > serial, & P)) triggers the call to transport_ socket_ The events () function performs data processing.

3. adb# root process analysis

 

 

4, adb common commands

The general format of adb command is: adb [- e | - d | - s < device serial number >] < subcommand >

  • adb} version to view the adb version
  • ADB # tcpip # 5555, set the attribute ersist adb. tcp. Port = 5555, restart adbd and enter ADB # wifi mode.
  • adb devices to get the device list and device status
   
  1. a.device: The equipment is connected normally
  2. b.offline: The connection is abnormal and the device is not responding
  3. c.unknown: No device connected
  • adb # get state: get the status of the device. The status of the device includes # 3 # minutes, device, offline and unknown
  • adb kill-server , adb start-server 
   
  1. end adb Service, start adb Service, usually two commands are used together.
  2. Generally, when an exception occurs in the connection, use adb devices The equipment is not listed normally, and it is used when the equipment status is abnormal kill- server,Then run start- server Restart service
  • adb # logcat, print the system log of # Android #
  • adb bugreport 
   
  1. Print dumpsys,dumpstate,logcat The output of is also used to analyze errors
  2. There is a lot of output. It is recommended to redirect to a file
  3. adb bugreport > d:\bugreport. log
  • adb install 
Install the application, overwrite the installation is to use -r option
   
  • adb uninstall
   
  1. Uninstall the application. The following parameter is the package name of the application. Please distinguish it from apk file name
  2. '-k' means keep the data and cache directories , -k Option to save data and cache directory when uninstalling
  • adb # pull to copy the files or folders on the # Android # device to the local
  • adb # push, push local files to # Android # devices
  • adb , root, adb , remount, obtain , root , permission and mount the system file system in the read-write state
  • adb # reboot, restart # Android # device
  • adb # reboot # loader, restart the device and enter # fastboot # mode, the same as the # adb # reboot bootloader # command
  • adb # reboot # recovery, restart the device and enter # recovery # mode
  • adb connect <device-ip-address>/adb disconnect <device-ip-address>

The above commands are built-in commands of the adb program and have nothing to do with Android. The commands of the Android system are implemented in system/bin (pm, am, screenrecord, getprop, input, ime, wm, settings, monkey, dumpsys, logcat, log, uiautomator, etc.). In addition, it is the command that comes with linux. Check it yourself.

Postscript: due to the actual development needs of adb, I studied the adb source code. The above is a summary. In the spirit of open source, we will make progress together!

Reference article:

https://blog.csdn.net/wlwl0071986/article/details/50935496

https://blog.csdn.net/qq_35970872/article/details/78912611

https://www.cnblogs.com/zzb-Dream-90Time/p/8166223.html

Keywords: Java Android Apache adb

Added by goodluck4287 on Fri, 28 Jan 2022 03:30:27 +0200