# node-avd

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
![npm (tag)](https://img.shields.io/npm/v/node-avd/latest?label=node-avd%40latest)
![npm type definitions](https://img.shields.io/npm/types/node-avd)
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
![npm collaborators](https://img.shields.io/npm/collaborators/node-avd?color=g)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

- [node-avd](#node-avd)
  - [介绍](#介绍)
  - [准备工作](#准备工作)
  - [安装](#安装)
  - [命令行使用简单示例](#命令行使用简单示例)
  - [API 使用简单示例](#api-使用简单示例)
  - [命令行文档](#命令行文档)
  - [API 文档](#api-文档)
  - [其他](#其他)
    - [启动模拟器时报错](#启动模拟器时报错)
    - [设置模拟器硬件配置](#设置模拟器硬件配置)
  - [遇到问题](#遇到问题)
    

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

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

## 准备工作

> 提前准备好 `java` 环境,参考 [oracle java](https://www.oracle.com/java/technologies/downloads/)

## 安装

`npm i -g node-avd`

> 在 linux 上要加 sudo 来执行

## 命令行使用简单示例

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

## API 使用简单示例

```ts
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`                         | 若你电脑上没有任何关于安卓模拟器的程序，你可以使用这个命令来帮助你完成初始化工作                                                                                                                         | s |
| -可选参数                               |                                                                                                                                                                                                          |
| `-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 文档

```typescript
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](http://tva1.sinaimg.cn/large/0080GS1wgy1gyi18q9yu0j31wn0dfn5q.jpg)
可以使用 `node-avd HAXM`命令来帮助你，然后再重新启动模拟器

### 设置模拟器硬件配置

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

1. 通过`node-avd -la`得到模拟器的目录
   ![1](http://tva1.sinaimg.cn/large/0080GS1wgy1gyi18qa13qj31200adn0g.jpg)
2. 打开这个文件夹找到 config.ini 文件
   ![2](http://tva1.sinaimg.cn/large/0080GS1wgy1gyi18qlppoj326c13l4qp.jpg)
3. 使用任意文本编辑器修改其中关于屏幕尺寸的部分（搜索 lcd 字段可以快速定位屏幕尺寸部分）
   ![3](http://tva1.sinaimg.cn/large/0080GS1wgy1gyi18qg58yj31zc14x7r7.jpg)
   例如修改屏幕的长和宽：

   ```test
   hw.lcd.height = 720
   hw.lcd.width = 1790
   ```
## 遇到问题
node-avd目前还处于初步阶段，如果你遇到问题，可以在此提交[issue](https://github.com/dongwa/node-avd/issues)