1. 什么是adb

在SDK的Tools文件夹下包含着Android模拟器操作的重要命令ADB,ADB的全称为Android Debug Bridge,就是调试桥的作用。借助这个工具,我们可以管理设备或手机模拟器的状态,还可以进行以下的操作:

(1)快速更新设备或手机模拟器中的代码,如应用或Android系统升级;

(2)在设备上运行Shell命令;

(3)管理设备或手机模拟器上的预定端口;

(4)在设备或手机模拟器上复制或粘贴文件。

官网如下说:

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components:

  • A client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients.
  • A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device.
  • A daemon, which runs as a background process on each emulator or device instance.

You can find theadbtool in<sdk>/platform-tools/.

When you start an adb client, the client first checks whether there is an adb server process already running. If there isn't, it starts the server process. When the server starts, it binds to 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 emulator/device instances. It locates emulator/device instances by scanning odd-numbered ports in the range 5555 to 5585, the range used by emulators/devices. Where the server finds an adb daemon, it sets up a connection to that port. Note that each emulator/device instance acquires a pair of sequential ports — an even-numbered port for console connections and an odd-numbered port for adb connections. For example:

Emulator 1, console: 5554
Emulator 1, adb: 5555
Emulator 2, console: 5556
Emulator 2, adb: 5557
and so on...

As shown, the emulator instance connected to adb on port 5555 is the same as the instance whose console listens on port 5554.

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

2. 启动adb

我使用adb安装apk时,提示:

adb server is out of date.  killing...ADB server didn't ACK* failed to start daemon *

原因上,电脑上安装的手机助手占用了adb的端口5037,所以adb启动不起来。如果可以退出手机助手,且插上手机后助手也不会霸占adb的端口,倒是可以保留。无奈,我PC上的手机助手做不到,我只能卸之。卸载后就OK了,命令行下adb操作就能启动adb server了。如果需要安装ios包,可以使用itools。试用了下,不会引起以上问题。

使用adb时,需开启android的debug模式。

3. adb shell

ad shell即可用shell来管理android设备。因为android本质上是linux系统,所以和linux上的操作命令类似。如cd/ls/rm等。

4. adb常用命令之logcat

adb命令语法:

[adb] logcat [<option>] ... [<filter-spec>] ...
  • adb logcat -c 清除所有以前的日志
  • adb logcat -d 打印日志,且自动退出log模式
  • adb logcat -f 将日志输出到文件中。注意,这个文件保存在设备上,所以需要将日志创建在可写入的地方。比如/sdcard/logs.txt
  • adb logcat -s 设置默认的过滤器, 如 我们想要输出 "System.out" 标签的信息, 就可以使用adb logcat -s System.out
  • adb logcat -t N 查看最后的N条日志
  • adb logcat | grep XXX 管道命令,同Linux

也可以进入adb shell后,运行logcat。与adb logcat 参数一样。

5. adb常用命令之pull & push

  • adb pull <remote> <local>:Copies a specified file from an emulator/device instance to your development computer
  • adb push <local> <remote>:Copies a specified file from your development computer to an emulator/device instance

当记下日志后,就可以使用pull命令,将日志文件传到本地查看了。

更多相关文章

  1. adb(Android(安卓)Debug Bridge)(一)
  2. 页面调用ADB操作Android设备
  3. Android(安卓)adb不是内部或外部命令 问题解决
  4. Linux手机打电话代码分析
  5. Android之父Andy Rubin:被乔布斯羡慕嫉妒的天才
  6. Mac OS X系统下的Android环境变量配置
  7. PC与Android设备通过USB建立通信连接
  8. Android开发之如何获取Android手机屏幕的大小(分辨率、密度)
  9. android app的类响应式设计

随机推荐

  1. Android使用WebView和JS互相调用
  2. Android(安卓)Broadcast receiver 编程
  3. Android监测手指上下左右滑动屏幕
  4. Android中如何获得一些系统设置和环境变
  5. android IPC及原理简介
  6. Android开发指南(36) —— Search
  7. Android强制使用WebView不调用系统或外部
  8. Android(安卓)开发UI牛博
  9. Android(安卓)Studio 手动完成项目配置
  10. Android手动画柱状图的例子