adb命令 入门笔记 (mac用adb命令安装apk到android手机)

官方网站:http://adbshell.com/

Android Debug Bridge(adb)是一个命令行工具,可让您与模拟器或连接的Android设备进行通信。您可以在android sdk / platform-tools或下载ADB工具包中找到adb工具。

如何在Windows上使用ADB 命令

/ platform-tools文件夹中找到adb.exe或下载ADB工具包(zip包解压后有:adb.exe,AdbWinApi.dll,AdbWinUsbApi.dll)

注意:Android SDK路径:%LOCALAPPDATA%\ Android \ sdk => C:\ Users \ <用户名> \ AppData \ Local \ Android \ sdk

打开命令提示符。(按Windows键+ R,键入“cmd”)

设置环境变量:电脑设置》高级属性》系统变量,找到“path”,选中后点击“编辑”,把adb所在目录加到里面。

配置成功后,就不用进入到adb所在目录,直接输入adb命令就OK

 

C:\Users\adm>cd C:\Users\adm\Desktop\adb

C:\Users\adm\Desktop\adb>adb devices  
(adb devices会先启动再展示连接的设备。
如果用命令启动是adb start-server。
用命令停止进程是 adb kill-server。
注意:我的华为p20手机是不允许wifi模式下 adb connect 总是连接不上。
“仅充电”模式下允许ADB调试,不能wifi模式下ADB调试)
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached

ADB COMMANDS

Android Debug Bridge version 1.0.32

 -a                            - directs adb to listen on all interfaces for a connection
 -d                            - directs command to the only connected USB device
                                 returns an error if more than one USB device is present.
 -e                            - directs command to the only running emulator.
                                 returns an error if more than one emulator is running.
 -s           - directs command to the device or emulator with the given
                                 serial number or qualifier. Overrides ANDROID_SERIAL
                                 environment variable.
 -p      - simple product name like 'sooner', or
                                 a relative/absolute path to a product
                                 out directory like 'out/target/product/sooner'.
                                 If -p is not specified, the ANDROID_PRODUCT_OUT
                                 environment variable is used, which must
                                 be an absolute path.
 -H                            - Name of adb server host (default: localhost)
 -P                            - Port of adb server (default: 5037)
 devices [-l]                  - list all connected devices
                                 ('-l' will also list device qualifiers)
 connect [:]       - connect to a device via TCP/IP
                                 Port 5555 is used by default if no port number is specified.
 disconnect [[:]]  - disconnect from a TCP/IP device.
                                 Port 5555 is used by default if no port number is specified.
                                 Using this command with no additional arguments
                                 will disconnect from all connected TCP/IP devices.

device commands:
  adb push [-p]  
                               - copy file/dir to device
                                 ('-p' to display the transfer progress)
  adb pull [-p] [-a]  []
                               - copy file/dir from device
                                 ('-p' to display the transfer progress)
                                 ('-a' means copy timestamp and mode)
  adb sync [  ]     - copy host->device only if changed
                                 (-l means list but don't copy)
                                 (see 'adb help all')
  adb shell                    - run remote shell interactively
  adb shell           - run remote shell command
  adb emu             - run emulator console command
  adb logcat [  ] - View device log
  adb forward --list           - list all forward socket connections.
                                 the format is a list of lines with the following format:
                                     " "  " "  "\n"
  adb forward   - forward socket connections
                                 forward specs are one of:
                                   tcp:
                                   localabstract:
                                   localreserved:
                                   localfilesystem:
                                   dev:
                                   jdwp: (remote only)
  adb forward --no-rebind  
                               - same as 'adb forward  ' but fails
                                 if  is already forwarded
  adb forward --remove  - remove a specific forward socket connection
  adb forward --remove-all     - remove all forward socket connections
  adb reverse --list           - list all reverse socket connections from device
  adb reverse   - reverse socket connections
                                 reverse specs are one of:
                                   tcp:
                                   localabstract:
                                   localreserved:
                                   localfilesystem:
  adb reverse --norebind  
                               - same as 'adb reverse  ' but fails
                                 if  is already reversed.
  adb reverse --remove 
                               - remove a specific reversed socket connection
  adb reverse --remove-all     - remove all reversed socket connections from device
  adb jdwp                     - list PIDs of processes hosting a JDWP transport
  adb install [-lrtsd] 
  adb install-multiple [-lrtsdp] 
                               - push this package file to the device and install it
                                 (-l: forward lock application)
                                 (-r: replace existing application)
                                 (-t: allow test packages)
                                 (-s: install application on sdcard)
                                 (-d: allow version code downgrade)
                                 (-p: partial application install)
  adb uninstall [-k]  - remove this app package from the device
                                 ('-k' means keep the data and cache directories)
  adb bugreport                - return all information from the device
                                 that should be included in a bug report.

  adb backup [-f ] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] []
                               - write an archive of the device's data to .
                                 If no -f option is supplied then the data is written
                                 to "backup.ab" in the current directory.
                                 (-apk|-noapk enable/disable backup of the .apks themselves
                                    in the archive; the default is noapk.)
                                 (-obb|-noobb enable/disable backup of any installed apk expansion
                                    (aka .obb) files associated with each application; the default
                                    is noobb.)
                                 (-shared|-noshared enable/disable backup of the device's
                                    shared storage / SD card contents; the default is noshared.)
                                 (-all means to back up all installed applications)
                                 (-system|-nosystem toggles whether -all automatically includes
                                    system applications; the default is to include system apps)
                                 ( is the list of applications to be backed up.  If
                                    the -all or -shared flags are passed, then the package
                                    list is optional.  Applications explicitly given on the
                                    command line will be included even if -nosystem would
                                    ordinarily cause them to be omitted.)

  adb restore            - restore device contents from the  backup archive

  adb disable-verity           - disable dm-verity checking on USERDEBUG builds
  adb keygen             - generate adb public/private key. The private key is stored in ,
                                 and the public key is stored in .pub. Any existing files
                                 are overwritten.
  adb help                     - show this help message
  adb version                  - show version num

scripting:
  adb wait-for-device          - block until device is online
  adb start-server             - ensure that there is a server running
  adb kill-server              - kill the server if it is running
  adb get-state                - prints: offline | bootloader | device
  adb get-serialno             - prints: 
  adb get-devpath              - prints: 
  adb status-window            - continuously print device status for a specified device
  adb remount                  - remounts the /system and /vendor (if present) partitions on the device read-write
  adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
  adb reboot-bootloader        - reboots the device into the bootloader
  adb root                     - restarts the adbd daemon with root permissions
  adb usb                      - restarts the adbd daemon listening on USB
  adb tcpip              - restarts the adbd daemon listening on TCP on the specified port
networking:
  adb ppp  [parameters]   - Run PPP over USB.
 Note: you should not automatically start a PPP connection.
  refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
 [parameters] - Eg. defaultroute debug dump local notty usepeerdns

adb sync notes: adb sync [  ]
   can be interpreted in several ways:

  - If  is not specified, /system, /vendor (if present), and /data partitions will be updated.

  - If it is "system", "vendor" or "data", only the corresponding partition
    is updated.

environmental variables:
  ADB_TRACE                    - Print debug information. A comma separated list of the following values
                                 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp
  ANDROID_SERIAL               - The serial number to connect to. -s takes priority over this if given.
  ANDROID_LOG_TAGS             - When used with the logcat option, only these debug tags are printed.

面试会问:用adb 命令获取日志的方法(写几个)

判断设备在线的命令,还有获取日志的命令

查看当前运行的APK包名:

命令行输入:adb shell dumpsys window w |findstr \/ |findstr name=

adb 命令参数

#安装APK包的命令
adb install test.apk
adb install -l test.apk  #forward lock application
adb install -r test.apk  #replace existing application
adb install -t test.apk  #allow test packages
adb install -s test.apk  #install application on sdcard
adb install -d test.apk  #allow version code downgrade
adb install -p test.apk  #partial application install

#卸载APK包的命令
adb uninstall com.test.app  
adb uninstall -k com.test.app  #Keep the data and cache directories around after package removal.

#打印所有包,可选择仅打包包名称包含中文本的包。
adb shell pm list packages  
#Prints all packages, optionally only those whose package name contains the text in .
adb shell pm list packages -f  #See their associated file.
adb shell pm list packages -d  #Filter to only show disabled packages.
adb shell pm list packages -e  #Filter to only show enabled packages.
adb shell pm list packages -s  #Filter to only show system packages.
adb shell pm list packages -3  #Filter to only show third party packages.
adb shell pm list packages -i  #See the installer for the packages.
adb shell pm list packages -u  #Also include uninstalled packages.
adb shell pm list packages --user    #The user space to query.

#打印apk的路径
adb shell pm path com.android.phone  #Print the path to the APK of the given 

#清除与包相关的所有数据 
adb shell pm clear com.test.abc  #Deletes all data associated with a package.

#把手机里面的文件,拉去到本地电脑
adb pull /sdcard/demo.mp4
#download /sdcard/demo.mp4  to /platform-tools directory.
adb pull /sdcard/demo.mp4 e:\
#download /sdcard/demo.mp4 to drive E.

#把本地电脑的文件,push到手机
adb push test.apk /sdcard
#Copies /platform-tools/test.apk to /sdcard directory.
adb push d:\test.apk /sdcard
#Copies d:\test.apk to /sdcard directory.


#文件操作相关的linux命令:
adb shell #第一步输入这个命令,再ls,cd,rm,mkdir,touch,pwd,cp,mv,netstat,ping,netcfg,ip
ls
ls -a #do not hide entries starting with
ls -i #print index number of each file
ls -s #print size of each file, in blocks
ls -n #list numeric UIDs and GIDs
ls -R #list subdirectories recursively
cd /system

#网络诊断相关的linux命令,netstat,ping,netcfg,ip
netstat  #network statistics
ping www.google.com  #Notes: press Ctrl-C to stop ping
ping www.google.com -c 4 
#ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination

C:\Users\adm\Desktop\adb>adb shell
HWEML:/ $ netcfg
netcfg
rmnet1   DOWN                                   0.0.0.0/0   0x00001002
rmnet0   DOWN                                   0.0.0.0/0   0x00001082
rmnet4   DOWN                                   0.0.0.0/0   0x00001002
rmnet3   DOWN                                   0.0.0.0/0   0x00001002
rmnet2   DOWN                                   0.0.0.0/0   0x00001002
rmnet6   DOWN                                   0.0.0.0/0   0x00001002
rmnet5   DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun01 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun00 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun04 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun03 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun02 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun11 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun10 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun14 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun13 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun12 DOWN                                   0.0.0.0/0   0x00001002
rmnet_ims00 UP                                     0.0.0.0/0   0x000010c3
rmnet_ims10 DOWN                                   0.0.0.0/0   0x00001002
sit0     DOWN                                   0.0.0.0/0   0x00000080
lo       UP                                   127.0.0.1/8   0x00000049
rmnet_emc0 DOWN                                   0.0.0.0/0   0x00001002
wlan0    UP                                 10.11.1.251/24  0x00001043
rmnet_r_ims01 DOWN                                   0.0.0.0/0   0x00001002
rmnet_r_ims00 DOWN                                   0.0.0.0/0   0x00001002
rmnet_r_ims11 DOWN                                   0.0.0.0/0   0x00001002
rmnet_r_ims10 DOWN                                   0.0.0.0/0   0x00001002
dummy0   UP                                     0.0.0.0/0   0x000000c3
HWEML:/ $

adb shell ip 
#show, manipulate routing, devices, policy routing and tunnels
ip [ OPTIONS ] OBJECT
OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |netns |  }

OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |-f[amily] { inet | inet6 | ipx | dnet | link } |-l[oops] { maximum-addr-flush-attempts } |-o[neline] | -t[imestamp] | -b[atch] [filename] |-rc[vbuf] [size]}

ip -f inet addr show wlan0 show WiFi IP Address

Notes: press Ctrl-C to stop


#adb logcat将日志数据打印到屏幕。用法adb logcat [option] [filter-specs]
adb logcat
注意:按Ctrl-C可停止监视器
adb logcat *:V 最低优先级,过滤为仅显示详细级别
adb logcat *:D 过滤器只显示调试级别
adb logcat *:我过滤到只显示信息级别
adb logcat *:W 过滤器仅显示警告级别
adb logcat *:E 过滤器仅显示错误级别
adb logcat *:F 过滤器仅显示致命级别
adb logcat *:S 无声,最高优先级,无需打印任何内容
adb logcat -b 
adb logcat -b radio 查看包含与无线电/电话相关的消息的缓冲区。
adb logcat -b event 查看包含与事件相关的消息的缓冲区。
adb logcat -b主要默认值
adb logcat -c 清除整个日志并退出。
adb logcat -d 将日志转储到屏幕并退出。
adb logcat -f test.logs 将日志消息输出写入test.logs。
adb logcat -g 打印指定日志缓冲区的大小并退出。
adb logcat -n  将最大旋转日志数设置为。
注意:默认值为4.需要-r选项。
adb logcat -r 输出一次旋转日志文件。
注意:默认值为16.需要-f选项。
adb logcat -s 将默认过滤器规范设置为silent。
adb logcat -v 
adb logcat -v brief 显示发出消息的进程的优先级/标记和PID(默认格式)。
adb logcat -v process 仅显示PID。)
adb logcat -v tag仅显示优先级/标记。
adb logcat -v raw 显示原始日志消息,没有其他元数据字段。
adb logcat -v time 显示发出消息的进程的日期,调用时间,优先级/标记和PID。
adb logcat -v threadtime 显示发出消息的线程的日期,调用时间,优先级,标记以及PID和TID。
adb logcat -v long 使用空行显示所有元数据字段和单独的消息。


adb shell dumpsys #这个命令会打印出超级多的信息,建议带上meminfo,batter指定想看的信息
adb shell dumpsys meminfo   #查看内存使用情况,如果手机安装的app比较多的话,打印的信息会很多,可以使用
adb shell dumpsys meminfo com.***(包名)  #查看指定包名的内存使用情况
adb shell dumpsys meminfo 1234(pid) #查看指定pid的包的内存使用情况


adb shell dumpsys battery  #展示app使用的电池,要android5.0以上的版本
Notes: A mobile device with Developer Options enabled running Android 5.0 or higher.

adb shell dumpsys batterystats collects battery data from your device
Notes: Battery Historian(https://github.com/google/battery-historian) converts that data into an HTML visualization.
 STEP 1 adb shell dumpsys batterystats > batterystats.txt
 STEP 2 python historian.py batterystats.txt > batterystats.html

adb shell dumpsys batterystats --reset erases old collection data
adb shell dumpsys activity

adb shell dumpsys gfxinfo com.android.phone measuring com.android.phone ui performance

 

mac用brew安装android-platform-tools后,安装apk到安卓机

$ brew cask install android-platform-tools    1)安装adb成功,如下

Updating Homebrew...

==> Satisfying dependencies

==> Downloading https://dl.google.com/android/repository/platform-tools_r28.0.1-darwin.zip

######################################################################## 100.0%

==> Verifying SHA-256 checksum for Cask 'android-platform-tools'.

==> Installing Cask android-platform-tools

==> Linking Binary 'adb' to '/usr/local/bin/adb'.

==> Linking Binary 'dmtracedump' to '/usr/local/bin/dmtracedump'.

==> Linking Binary 'etc1tool' to '/usr/local/bin/etc1tool'.

==> Linking Binary 'fastboot' to '/usr/local/bin/fastboot'.

==> Linking Binary 'hprof-conv' to '/usr/local/bin/hprof-conv'.

==> Linking Binary 'mke2fs' to '/usr/local/bin/mke2fs'.

?  android-platform-tools was successfully installed!


2) usb连接android设备测试(调试模式打开),显示设备名字了,就是ok的
$ adb devices   
List of devices attached

18185521401347 unauthorized





$ adb shell
error: device unauthorized.

This adb server's $ADB_VENDOR_KEYS is not set

Try 'adb kill-server' if that seems wrong.

Otherwise check for a confirmation dialog on your device.



$ adb kill-server
$ adb shell
* daemon not running; starting now at tcp:5037

* daemon started successfully

error: device unauthorized.         需要把设备的usb调试模式打开





Adb命令安装apk报错 (定位结论是加上-t就不报错了)
$ adb install /Users/xxxxx/Downloads/wms-android-v2.0.5-improve-0109/app/build/outputs/apk/release/wms_2.0.3_release_test_20190103.apk

adb: failed to install /Users/xxxxx/Downloads/wms-android-v2.0.5-improve-0109/app/build/outputs/apk/release/wms_2.0.3_release_test_20190103.apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]



解决办法:adb命令里面加个-t 参数

$ adb install -t wms_2.0.3_release_test_20190103.apk 

Success





配置环境变量.bash_profile
记得要source .bash_profile

export PATH=${PATH}:/usr/local/bin           (这里的路径要根据你实际安装的路径来修改)

环境变量配置好后,直接输入adb   系统就可以识别这个命令了

 

adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用。通过adb我们可以在Eclipse中方面通过DDMS来调试android程序,说白了就是debug工具。adb的工作方式比较特殊,采用监听Socket TCP 5554等端口的方式让IDE和Qemu通讯,默认情况下adb会daemon相关的网络端口,所以当我们运行Eclipse时adb进程就会自动运行。

adb有什么用?:借助adb工具,我们可以管理设备或手机模拟器的状态。还可以进行很多手机操作,如安装软件、系统升级、运行shell命令等等。其实简而言说,adb就是连接Android手机与PC端的桥梁,可以让用户在电脑上对手机进行全面的操作

1. 显示系统中全部Android平台:

    android list targets

2. 显示系统中全部AVD(模拟器):

    android list avd

3. 创建AVD(模拟器):

    android create avd --name 名称 --target 平台编号

4. 启动模拟器:

    emulator -avd 名称 -sdcard ~/名称.img (-skin 1280x800)

5. 删除AVD(模拟器):

    android delete avd --name 名称

6. 创建SDCard:

    mksdcard 1024M ~/名称.img

7. AVD(模拟器)所在位置:

    Linux(~/.android/avd)      Windows(C:\Documents and Settings\Administrator\.android\avd)

8. 启动DDMS:

    ddms

9. 显示当前运行的全部模拟器:

    adb devices

10. 对某一模拟器执行命令:

      abd -s 模拟器编号 命令

11. 安装应用程序:

      adb install -r 应用程序.apk

12. 获取模拟器中的文件:

      adb pull

13. 向模拟器中写文件:

      adb push

14. 进入模拟器的shell模式:

      adb shell

15. 启动SDK,文档,实例下载管理器:

      android

16. 缷载apk包:

      adb shell

      cd data/app

      rm apk包

      exit

      adb uninstall apk包的主包名

      adb install -r apk包

17. 查看adb命令帮助信息:

      adb help

18. 在命令行中查看LOG信息:

      adb logcat -s 标签名

19. adb shell后面跟的命令主要来自:

      源码\system\core\toolbox目录和源码\frameworks\base\cmds目录。

20. 删除系统应用:

      adb remount (重新挂载系统分区,使系统分区重新可写)。

      adb shell

      cd system/app

      rm *.apk

21. 获取管理员权限:

      adb root

22. 启动Activity:

      adb shell am start -n 包名/包名+类名(-n 类名,-a action,-d date,-m MIME-TYPE,-c category,-e 扩展数据,等)。

23、发布端口:

    你可以设置任意的端口号,做为主机向模拟器或设备的请求端口。如: 
adb forward tcp:5555 tcp:8000

24、复制文件:

    你可向一个设备或从一个设备中复制文件, 
     复制一个文件或目录到设备或模拟器上: 
  adb push  
      如:adb push test.txt /tmp/test.txt 
     从设备或模拟器上复制一个文件或目录: 
     adb pull  
     如:adb pull /addroid/lib/libwebcore.so .

25、搜索模拟器/设备的实例:

     取得当前运行的模拟器/设备的实例的列表及每个实例的状态: 
    adb devices

26、查看bug报告: 
adb bugreport 
27、记录无线通讯日志:

    一般来说,无线通讯的日志非常多,在运行时没必要去记录,但我们还是可以通过命令,设置记录: 
    adb shell 
    logcat -b radio

28、获取设备的ID和序列号:

     adb get-product 
     adb get-serialno

29、访问数据库SQLite3

     adb shell 
     sqlite3

#cd system/sd/data //进入系统内指定文件夹 
#ls //列表显示当前文件夹内容 
#rm -r xxx //删除名字为xxx的文件夹及其里面的所有文件 
#rm xxx //删除文件xxx 
#rmdir xxx //删除xxx的文件夹


【操作命令】

  1. 查看设备

  adb devices

这个命令是查看当前连接的设备, 连接到计算机的android设备或者模拟器将会列出显示

  2.安装软件

  adb install

adb install :这个命令将指定的apk文件安装到设备上

  3. 卸载软件

  adb uninstall <软件名>

  adb uninstall -k <软件名>

如果加 -k 参数,为卸载软件但是保留配置和缓存文件.

  4. 进入设备或模拟器的shell:

  adb shell

通过上面的命令,就可以进入设备或模拟器的shell环境中,在这个linux Shell中,你可以执行各种Linux的命令,另外如果只想执行一条shell命令,可以采用以下的方式:

  adb shell [command]

如:adb shell dmesg会打印出内核的调试信息。

  5. 发布端口

可以设置任意的端口号,做为主机向模拟器或设备的请求端口。如:

adb forward tcp:5555 tcp:8000

  6. 从电脑上发送文件到设备

  adb push <本地路径> <远程路径>

用push命令可以把本机电脑上的文件或者文件夹复制到设备(手机)

  7. 从设备上下载文件到电脑

  adb pull <远程路径> <本地路径>

用pull命令可以把设备(手机)上的文件或者文件夹复制到本机电脑

  8、查看bug报告

adb bugreport

  9、记录无线通讯日志

一般来说,无线通讯的日志非常多,在运行时没必要去记录,但我们还是可以通过命令,设置记录:

adb shell

logcat -b radio

  10、获取设备的ID和序列号

adb get-product

adb get-serialno

adb shell

sqlite3


利用adb命令查看apk文件包名的一些方法
前提是已经下载android SDK并配好环境变量!

在控制台输入命令$adb shell pm

可以看到adb shell pm的相关用法,详细信息请自己看输出

要看一个apk文件的相关信息最简单实用的方法是:

aapt dump badging [yourapp.apk]

如果只是想查看手机上应用的packageName,则输入命令:

adb shell pm list packages

列出如下信息:

package:android

package:cn.ninegame.gamemanager

package:com.alipay.android.app

package:com.android.backupconfirm

package:com.android.browser

package:com.android.calculator2

package:com.android.calendar

package:com.android.camera

package:com.android.certinstaller

...

如果想知道应用对应的apk文件在手机上的安装位置则可以在上面的命令后加-f参数

adb shell pm list packages -f


--------------------- 
作者:Python知识圈 
来源:CSDN 
原文:https://blog.csdn.net/m0_37615390/article/details/76919255 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

 

 

你可能感兴趣的:(入门系列)