node-avd
TypeScript icon, indicating that this package has built-in type declarations

0.1.2-beta.1 • Public • Published

node-avd

npm (tag) npm type definitions tested with jest styled with prettier npm collaborators

介绍

如果你想使用Android Emulator,但是不想安装庞大的Android Studio,或者觉得安装的过程太繁琐,那么这个包可以帮到你。

node-avd可以通过几个简单的命令就可以帮助你安装好avd需要的依赖环境并启动模拟器,当前也提供了一些方法来管理模拟器。

准备工作

提前准备好 java 环境,参考 oracle java

安装

npm i -g node-avd

在 linux 上要加 sudo 来执行

命令行使用简单示例

  1. 在命令行中进入一个空的文件夹,例如:cd ~/avd
  2. node-avd init -n test :初始化并创建一个名为 test 的模拟器
  3. node-avd start test : 启动模拟器 test

API 使用简单示例

import { Avd, getPlatform } from 'node-avd';

const Example = {
  exampleCmdlineToolsUri: `https://dl.google.com/android/repository/commandlinetools-${getPlatform()}-8512546_latest.zip`,
  exampleSdkHome: `${process.cwd()}`, // any empty dir
  exampleSystemImage: 'system-images;android-28;default;x86', // a system will be use to avd,can find list in node-avd -ls
  exampleAvdName: `car` //the name of avd
};


async function init() {
  /** 1. new Avd()  */
  const avd = new Avd(Example.exampleSdkHome);
  /** 2. install android commandline tools */
  await avd.installCmdlineTools(Example.exampleCmdlineToolsUri);
  /** 3. install a system image */
  await avd.installSystemImage(Example.exampleSystemImage);
  /** 4. create an avd */
  await avd.createAvd({ name: Example.exampleAvdName, system: Example.exampleSystemImage });
  /** 5. start an avd */
  avd.startAvd(Example.exampleAvdName, false /* set cold start*/);
}
init();

命令行文档

命令 描述
node-avd init 若你电脑上没有任何关于安卓模拟器的程序,你可以使用这个命令来帮助你完成初始化工作
-可选参数
-r <path> 指定你想把 Android 安装在哪里 (default: 当前目录)
-u <url> 指定 Android comandline tools 的下载链接 (default: https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip)
-s <system> 指定安装一个系统镜像,你可以使用node-avd -ls来查看有哪些可以安装的镜像 (default: "system-images;android-28;default;x86")
-n <name> 若指定了一个 name,那么初始化工作会顺带帮你创建一个名为 name 的模拟器
node-avd create -n <name> 创建一个名为name的新模拟器
-可选参数
-s <system> 指定要使用的系统镜像(default: "system-images;android-28;default;x86")
node-avd start <name> 根据 name 来启动一个模拟器
node-avd setHome <path> 若你的电脑上已经安装了 Android Studio 或者 Android CmonnanLine Tools 你可以使用这个命令来设置你的 Android Sdk home 的目录位置
node-avd remove <name> 删除名为name的模拟器
node-avd setAvdIni <name> -d <device> 根据预定义的设备来修改模拟器的配置,-d 参数可选的值有 car,tv1080p,tv720p,table
node-avd -la | -list-avds 查看你的已经拥有的模拟器
node-avd -ls | --list-system 查看所有的系统镜像包括已经安装的和可以下载的
node-avd HAXM 安装 HAXM 插件,当你启动模拟器得到一个x86 emulation currently requires hardware acceleration!CPU acceleration status: HAXM is not installed on this machine 这样的错误时,你可以使用此命令后再重新启动

你可以通过 node-avd -h 来查看全部的命令

API 文档

export declare class Avd {
  constructor(ANDROID_SDK_ROOT: string);
  checkRootPath(ANDROID_SDK_ROOT: string): void;
  /**
   * @description install the Android commandline tools
   * @param latestCmdlineToolsUri url of Android commandline tools
   * @link https://developer.android.com/studio#command-tools
   */
  installCmdlineTools(latestCmdlineToolsUri: string): Promise<void>;
  /**
   * @description install a system image that you can find in the result of the `./sdkmanager --list` command
   * @param systemImage which system image you want to use
   * @returns
   */
  installSystemImage(systemImage: string): Promise<number>;
  /**
   * Creates a new Android Virtual Device.
   * @param options.name the name of avd
   * @param options.system which system the avd use
   */
  createAvd(options: IAvdOptions): Promise<number>;
  /** delete an avd by name */
  removeAvd(name: string): Promise<number>;
  /** start an avd by name */
  startAvd(name: string, cold?: boolean): Promise<string>;
  /** return the list of Emulator you have */
  getAvdList(): Promise<string>;
  /** return the list of all system you can install */
  getAllSystemList(): Promise<string>;
  getAvdPath(name: string): Promise<string | undefined>;
  /**
   * if you get an error like:
   * x86 emulation currently requires hardware acceleration!CPU acceleration status: HAXM is not installed on this machine.
   * You can use this cmd help you
   */
  fixHAXM(): Promise<unknown>;
  /** Modify hardware configurations of Emulator based on predefined devices such as {predefinitionDevices}  */
  setAvdIniByPredefinition(name: string, device: deviceType): Promise<void>;
}

其他

启动模拟器时报错

若你启动时遇到如下问题: HAXM error 可以使用 node-avd HAXM命令来帮助你,然后再重新启动模拟器

设置模拟器硬件配置

若你想修改模拟器的硬件配置,例如屏幕尺寸,你可以使用node-avd -la得到模拟器的目录,打开目录找到一个 config.ini 文件,修改对应值后重启模拟器即可 例如修改模拟器的屏幕尺寸:

  1. 通过node-avd -la得到模拟器的目录 1

  2. 打开这个文件夹找到 config.ini 文件 2

  3. 使用任意文本编辑器修改其中关于屏幕尺寸的部分(搜索 lcd 字段可以快速定位屏幕尺寸部分) 3 例如修改屏幕的长和宽:

    hw.lcd.height = 720
    hw.lcd.width = 1790
    

遇到问题

node-avd目前还处于初步阶段,如果你遇到问题,可以在此提交issue

Package Sidebar

Install

npm i node-avd

Weekly Downloads

23

Version

0.1.2-beta.1

License

ISC

Unpacked Size

441 kB

Total Files

46

Last publish

Collaborators

  • dongwa
  • zhaoyinggang