@devicefarmer/adbkit-monkey

中文文档
2.0.0发布于 2026/10/25Apache-2.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 270.9万

A Node.js interface to the Android monkey tool.

原始使用文档
文档目录10 节
  1. Getting started
  2. Examples
  3. API
  4. Monkey
  5. Client
  6. Api
  7. Multi
  8. More information
  9. Contributing
  10. License

adbkit-monkey

adbkit-monkey provides a Node.js interface for working with the Android monkey tool. Albeit undocumented, they monkey program can be started in TCP mode with the --port argument. In this mode, it accepts a range of commands that can be used to interact with the UI in a non-random manner. This mode is also used internally by the monkeyrunner tool, although the documentation claims no relation to the monkey tool.

Getting started

Install via NPM:

npm install --save @devicefarmer/adbkit-monkey

adbkit-monkey requires Node.js 22 or later. It is written in TypeScript and published as CommonJS JavaScript with type declarations, so it works with both require() and TypeScript.

Examples

The following examples assume that monkey is already running (via adb shell monkey --port 1080) and a port forwarding (adb forward tcp:1080 tcp:1080) has been set up.

Press the home button

var assert = require('assert');
var monkey = require('@devicefarmer/adbkit-monkey');

var client = monkey.connect({ port: 1080 });

client.press(3 /* KEYCODE_HOME */, function(err) {
  assert.ifError(err);
  console.log('Pressed home button');
  client.end();
});

Drag out the notification bar

var assert = require('assert');
var monkey = require('@devicefarmer/adbkit-monkey');

var client = monkey.connect({ port: 1080 });

client.multi()
  .touchDown(100, 0)
  .sleep(5)
  .touchMove(100, 20)
  .sleep(5)
  .touchMove(100, 40)
  .sleep(5)
  .touchMove(100, 60)
  .sleep(5)
  .touchMove(100, 80)
  .sleep(5)
  .touchMove(100, 100)
  .sleep(5)
  .touchUp(100, 100)
  .sleep(5)
  .execute(function(err) {
    assert.ifError(err);
    console.log('Dragged out the notification bar');
    client.end();
  });

Get display size

var assert = require('assert');
var monkey = require('@devicefarmer/adbkit-monkey');

var client = monkey.connect({ port: 1080 });

client.getDisplayWidth(function(err, width) {
  assert.ifError(err);
  client.getDisplayHeight(function(err, height) {
    assert.ifError(err);
    console.log('Display size is %dx%d', width, height);
    client.end();
  });
});

Type text

Note that you should manually focus a text field first.

var assert = require('assert');
var monkey = require('@devicefarmer/adbkit-monkey');

var client = monkey.connect({ port: 1080 });

client.type('hello monkey!', function(err) {
  assert.ifError(err);
  console.log('Said hello to monkey');
  client.end();
});

API

Monkey

monkey.connect(options)

Uses Net.connect() to open a new TCP connection to monkey. Useful when combined with adb forward.

  • options Any options Net.connect() accepts.
  • Returns: A new monkey Client instance.

monkey.connectStream(stream)

Attaches a monkey client to an existing monkey protocol stream.

  • stream The monkey protocol Stream.
  • Returns: A new monkey Client instance.

Client

Implements Api. See below for details.

Events

The following events are available:

  • error (err) Emitted when an error occurs.
    • err An Error.
  • end Emitted when the stream ends.
  • finish Emitted when the stream finishes.

client.end()

Ends the underlying stream/connection.

  • Returns: The Client instance.

client.multi()

Returns a new API wrapper that buffers commands for simultaneous delivery instead of sending them individually. When used with api.sleep(), allows simple gestures to be executed.

  • Returns: A new Multi instance. See Multi below.

client.send(command, callback)

Sends a raw protocol command to monkey.

  • command The command to send. When String, a single command is sent. When Array, a series of commands is sent at once.
  • callback(err, value, command) Called when monkey responds to the command. If multiple commands were sent, the callback will be called once for each command.
    • err null when successful, Error otherwise.
    • value The response value, if any.
    • command The command the response is for.
  • Returns: The Client instance.

Api

The monkey API implemented by Client and Multi.

api.done(callback)

Closes the current monkey session and allows a new session to connect.

  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.flipClose(callback)

Simulates closing the keyboard.

  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.flipOpen(callback)

Simulates opening the keyboard.

  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.get(name, callback)

Gets the value of a variable. Use api.list() to retrieve a list of supported variables.

  • name The name of the variable.
  • callback(err, value) Called when monkey responds.
    • err null when successful, Error otherwise.
    • value The value of the variable.
  • Returns: The Api implementation instance.

api.getAmCurrentAction(callback)

Alias for api.get('am.current.action', callback).

api.getAmCurrentCategories(callback)

Alias for api.get('am.current.categories', callback).

api.getAmCurrentCompClass(callback)

Alias for api.get('am.current.comp.class', callback).

api.getAmCurrentCompPackage(callback)

Alias for api.get('am.current.comp.package', callback).

api.getCurrentData(callback)

Alias for api.get('am.current.data', callback).

api.getAmCurrentPackage(callback)

Alias for api.get('am.current.package', callback).

api.getBuildBoard(callback)

Alias for api.get('build.board', callback).

api.getBuildBrand(callback)

Alias for api.get('build.brand', callback).

api.getBuildCpuAbi(callback)

Alias for api.get('build.cpu_abi', callback).

api.getBuildDevice(callback)

Alias for api.get('build.device', callback).

api.getBuildDisplay(callback)

Alias for api.get('build.display', callback).

api.getBuildFingerprint(callback)

Alias for api.get('build.fingerprint', callback).

api.getBuildHost(callback)

Alias for api.get('build.host', callback).

api.getBuildId(callback)

Alias for api.get('build.id', callback).

api.getBuildManufacturer(callback)

Alias for api.get('build.manufacturer', callback).

api.getBuildModel(callback)

Alias for api.get('build.model', callback).

api.getBuildProduct(callback)

Alias for api.get('build.product', callback).

api.getBuildTags(callback)

Alias for api.get('build.tags', callback).

api.getBuildType(callback)

Alias for api.get('build.type', callback).

api.getBuildUser(callback)

Alias for api.get('build.user', callback).

api.getBuildVersionCodename(callback)

Alias for api.get('build.version.codename', callback).

api.getBuildVersionIncremental(callback)

Alias for api.get('build.version.incremental', callback).

api.getBuildVersionRelease(callback)

Alias for api.get('build.version.release', callback).

api.getBuildVersionSdk(callback)

Alias for api.get('build.version.sdk', callback).

api.getClockMillis(callback)

Alias for api.get('clock.millis', callback).

api.getClockRealtime(callback)

Alias for api.get('clock.realtime', callback).

api.getClockUptime(callback)

Alias for api.get('clock.uptime', callback).

api.getDisplayDensity(callback)

Alias for api.get('display.density', callback).

api.getDisplayHeight(callback)

Alias for api.get('display.height', callback). Note that the height may exclude any virtual home button row.

api.getDisplayWidth(callback)

Alias for api.get('display.width', callback).

api.keyDown(keyCode, callback)

Sends a key down event. Should be coupled with api.keyUp(). Note that api.press() performs the two events automatically.

  • keyCode The key code. All monkeys support numeric keycodes, and some support automatic conversion from key names to key codes (e.g. 'home' to KEYCODE_HOME). This will not work for number keys however. The most portable method is to simply use numeric key codes.
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.keyUp(keyCode, callback)

Sends a key up event. Should be coupled with api.keyDown(). Note that api.press() performs the two events automatically.

  • keyCode See api.keyDown().
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.list(callback)

Lists supported variables.

  • callback(err, vars) Called when monkey responds.
    • err null when successful, Error otherwise.
    • vars An array of supported variable names, to be used with api.get().
  • Returns: The Api implementation instance.

api.press(keyCode, callback)

Sends a key press event.

  • keyCode See api.keyDown().
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.quit(callback)

Closes the current monkey session and quits monkey.

  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.sleep(ms, callback)

Sleeps for the given duration. Can be useful for simulating gestures.

  • ms How many milliseconds to sleep for.
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.tap(x, y, callback)

Taps the given coordinates.

  • x The x coordinate.
  • y The y coordinate.
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.touchDown(x, y, callback)

Sends a touch down event on the given coordinates.

  • x The x coordinate.
  • y The y coordinate.
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.touchMove(x, y, callback)

Sends a touch move event on the given coordinates.

  • x The x coordinate.
  • y The y coordinate.
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.touchUp(x, y, callback)

Sends a touch up event on the given coordinates.

  • x The x coordinate.
  • y The y coordinate.
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.trackball(x, y, callback)

Sends a trackball event on the given coordinates.

  • x The x coordinate.
  • y The y coordinate.
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.type(text, callback)

Types the given text.

  • text A text String. Note that only characters for which key codes exist can be entered. Also note that any IME in use may or may not transform the text.
  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

api.wake(callback)

Wakes the device from sleep and allows user input.

  • callback(err) Called when monkey responds.
    • err null when successful, Error otherwise.
  • Returns: The Api implementation instance.

Multi

Buffers Api commands and delivers them simultaneously for greater control over timing.

Implements all Api methods, but without the last callback parameter.

multi.execute(callback)

Sends all buffered commands.

  • callback(err, values) Called when monkey has responded to all commands (i.e. just once at the end).
    • err null when successful, Error otherwise.
    • values An array of all response values, identical to individual Api responses.

More information

Contributing

See CONTRIBUTING.md.

License

See LICENSE.

Copyright © CyberAgent, Inc. All Rights Reserved.

版本与发布记录

@devicefarmer/adbkit-monkey 共发布 5 个版本,首次发布于 2020/05/16;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/10/2518 天前
1.2.12022/06/054 年前
1.2.02021/09/225 年前
1.1.02021/03/085 年前
1.0.12020/05/166 年前

关于 @devicefarmer/adbkit-monkey 的常见问题

@devicefarmer/adbkit-monkey 是什么?

A Node.js interface to the Android monkey tool.

如何安装 @devicefarmer/adbkit-monkey?

在项目目录运行 npm install @devicefarmer/adbkit-monkey,或使用 pnpm add / yarn add / bun add。

@devicefarmer/adbkit-monkey 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@devicefarmer/adbkit-monkey 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@devicefarmer/adbkit-monkey 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@devicefarmer/adbkit-monkey 可以商用吗?

@devicefarmer/adbkit-monkey 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@devicefarmer/adbkit-monkey 的下载量有多少?

根据 npm 官方下载统计,@devicefarmer/adbkit-monkey 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,709,436 次,日均约 90,315 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@devicefarmer/adbkit-monkey 还在维护吗?

最新版本 2.0.0 发布于 2026/10/25(18 天前);至今共发布 5 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/05/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。