# android-key

> 智能体用来管理团队 Android 签名密钥的命令：列出、查看、创建、导入、恢复和导出。

Source: https://air.build/cn/docs/cli/android-key/

`android-key` 管理团队的 Android App 密钥。AirBuild 为每个 Android App 保存一个当前密钥，这个 App 的每次构建都用它签名：模拟器用的 `.apk`、供人下载的 APK，以及用 `runbuild --sign` 发布的版本。被替换的密钥会归档，从不删除，之后还可以重新启用。

## 在哪里运行

云端团队和 Mac 团队的智能体都能使用同一个命令。登录凭据来自智能体的运行环境，无需任何设置。命令依赖 `curl` 和 `jq`，运行环境已经提供。参见[选择智能体的运行位置](https://air.build/cn/docs/get-started/where-agents-run/)。

## App 的密钥从哪里来

- **自动生成。** App 首次构建时如果还没有当前密钥，就会生成一个。它的来源为 `auto`。
- **通过聊天卡片。** 智能体可以发送一张 `android_upload_key` 卡片；成员在卡片上上传已有的 keystore，或创建新密钥。
- **用这个命令。** 成员在聊天中附上 keystore 时用 `import`，成员用文字要求新密钥时用 `create`。

只有在成员要求新密钥，或 App 是首次上架 Google Play 时，才使用 `create`。已经在 Google Play 上的 App 必须保留原来的上传密钥。

## 用法概要

```text
android-key list [--json]
android-key status PACKAGE [--json]
android-key create --package PACKAGE [--json]
android-key import FILE --package PACKAGE [--password-entry NAME [--password-field LABEL]] [--json]
android-key activate PACKAGE KEY_ID [--json]
android-key certificate PACKAGE [--key KEY_ID] [--output FILE] [--json]
android-key export PACKAGE [--key KEY_ID] [--output FILE] [--json]
android-key --help
```

`PACKAGE` 是 App 的 `applicationId`，例如 `com.example.app`：至少由两段用点分隔的部分组成，每段以字母开头，最长 150 个字符。`status`、`create`、`certificate` 和 `export` 既可以把它作为第一个参数，也可以用 `--package PACKAGE` 传入。

## 命令

| Command | 作用 |
| --- | --- |
| `list` | 列出团队为所有 App 保存的全部密钥，包括当前的和已归档的。 |
| `status PACKAGE` | 显示 App 的当前密钥，然后是已归档的密钥。 |
| `create` | 为 App 新建一个密钥并设为当前密钥。App 原有的密钥会被归档。 |
| `import FILE` | 把 keystore 发送给 AirBuild，AirBuild 检查后将其中的密钥保存为 App 的当前密钥。App 原有的密钥会被归档。 |
| `activate PACKAGE KEY_ID` | 把已归档的密钥重新设为当前密钥；原来的当前密钥被归档。id 可在 `status` 中查看。 |
| `certificate PACKAGE` | 以 PEM 格式输出密钥的证书。只有证书，不含私钥。 |
| `export PACKAGE` | 把密钥写成 keystore 文件，并在旁边写一个 `.properties` 文件。仅在成员索要自己的密钥时使用。 |

当前密钥会为 App 之后的每次构建签名，包括模拟器用的 `.apk`。

## 选项

| Option | Commands | 取值 | 默认值 | 含义 |
| --- | --- | --- | --- | --- |
| `--package` | `status`, `create`, `import`, `certificate`, `export` | `applicationId` | — | 指定 App。`create` 和 `import` 必填。 |
| `--key` | `certificate`, `export` | 密钥 id（UUID） | 当前密钥 | 指定哪个密钥。 |
| `--output` | `certificate` | 文件 | 输出到 stdout | 把 PEM 写入这个文件。 |
| `--output` | `export` | 文件 | `./PACKAGE.keystore` | keystore 的写入位置。该文件和 `FILE.properties` 都不能已存在。 |
| `--password-entry` | `import` | 「密码」中的条目名称 | 从 stdin 读取 | 从团队的「密码」中读取 keystore 密码。 |
| `--password-field` | `import` | 字段标签 | `Password` | `--password-entry` 中保存密码的字段。 |
| `--json` | all | — | 关闭 | 输出 JSON 应答而不是文本。出错时也会在 stderr 上输出 JSON 错误。 |

## 导入 keystore

- `FILE` 是成员的 keystore，最大 64 KiB。

- 必须是 **PKCS#12** 格式：也就是如今 Android Studio 和 `keytool` 生成的格式，文件名常常仍是 `.jks`。不支持旧的 JKS 格式。请成员转换格式，或从 Android Studio 重新导出：

  ```bash
  keytool -importkeystore -srckeystore FILE -destkeystore upload.p12 -deststoretype pkcs12
  ```

- PKCS#12 keystore 只有一个密码。它从 stdin 的第一行读取，或用 `--password-entry` 从团队「密码」的条目中读取。密码绝不出现在命令行上：`--password` 或 `--store-password` 之类的选项会被拒绝。请用 `request_password` 卡片向成员索要密码，绝不要在聊天中索要（参见 [password-book](https://air.build/cn/docs/cli/password-book/)）。

- AirBuild 会检查密码，以及密钥与证书是否匹配。

- 导入成功后，命令会提醒你删除 keystore 文件以及你制作的任何其他副本。

## 导出密钥

`export` 会写入两个文件，都只有所有者可读（权限 600）：

```text
PACKAGE.keystore             a PKCS#12 keystore, key alias "upload"
PACKAGE.keystore.properties  storeFile, storePassword, keyAlias, keyPassword
```

properties 文件内容如下，其中是真实的密码：

```text
storeFile=com.example.app.keystore
storePassword=...
keyAlias=upload
keyPassword=...
```

每次导出都会使用新的密码。不会输出任何机密内容，也不应把 properties 文件打印出来。只把这两个文件发给索要的人，然后删除你的副本。

## 输出

`list`：

```text
PACKAGE          STATUS    ORIGIN    SHA-256                 ID
com.example.app  active    imported  3FA1…                   <key-id>
```

没有密钥时输出 `The Team holds no Android upload keys yet.`

`status`，以及 `create`、`import` 和 `activate` 输出中的每个密钥：

```text
Active key for com.example.app:
  id           <key-id>
  status       active
  origin       created
  sha256       <certificate fingerprint>
  valid until  2051-11-19
  created      2026-11-19 by <id>
```

`status` 会在 `Archived:` 下用同样的格式列出每个已归档的密钥。没有当前密钥的 App 会输出 `com.example.app has no active upload key. The first signed build creates one; if the app is already on Google Play, import its keystore first.`

`sha256` 是证书的 SHA-256 指纹，与 Google Play Console 中显示的一致。`origin` 为 `auto`、`created` 或 `imported`；`status` 为 `active` 或 `archived`。

`create`、`import` 和 `activate` 会输出 `This key now signs com.example.app:` 和该密钥；如果替换了另一个密钥，接着输出 `Archived (restore with android-key activate com.example.app <key-id>):` 和那个密钥。没有任何变化时输出 `Nothing changed: this key already signs com.example.app.`

`export` 输出路径、密钥 id 和指纹：

```text
Wrote the active key for com.example.app (PKCS#12, alias upload, mode 600):
  keystore    ./com.example.app.keystore
  properties  ./com.example.app.keystore.properties  (storePassword, keyPassword, keyAlias; not shown here)
  id          <key-id>
  sha256      <certificate fingerprint>
Send both files only to the person who asked for them, then delete your copies.
```

## 退出码

| Code | 含义 |
| --- | --- |
| `0` | 成功。 |
| `1` | AirBuild 拒绝了请求，或无法读写某个文件。 |
| `2` | 用法错误：未知的命令或选项、缺少参数，或取值格式不对。之后会输出 `Run 'android-key --help' for usage.` |

AirBuild 拒绝请求时，命令会输出 `Error: <reason>`，并在下一行输出 `Code: <code>`。原因里会说明该怎么做。

## 错误

命令自身的消息以 `Error:` 开头；AirBuild 的拒绝还会多一行 `Code:`。

| Message or code | 处理方法 |
| --- | --- |
| `'…' is not an Android applicationId (such as com.example.app).` | 传入 App 的 `applicationId`。 |
| `'…' is not a key id (android-key status shows them).` | 从 `android-key status` 复制 id。 |
| `passwords never go on the command line. Pipe it on stdin or use --password-entry NAME.` | 通过管道传入密码，或把密码保存到团队的「密码」中并使用 `--password-entry`。 |
| `pipe the keystore password on stdin, or pass --password-entry NAME.` | 同上。 |
| `a PKCS#12 keystore has one password; name its field with --password-field.` | 使用 `--password-field`，而不是单独的密钥密码选项。 |
| `could not read "Password" from the Passwords entry "…".` | 用 `password-book get NAME` 检查条目和字段名称。 |
| `cannot read the keystore '…'.` / `the keystore '…' is empty.` | 检查文件路径。 |
| `the keystore is larger than 64 KiB; it is not an upload keystore.` | 向成员索要正确的文件。 |
| `android_keystore_jks_unsupported` | 文件是旧的 JKS 格式。用上面的 `keytool` 命令转换，或从 Android Studio 重新导出。 |
| `android_keystore_password_wrong` | 密码打不开这个 keystore。向成员索要正确的密码。 |
| `android_keystore_multiple_keys` | keystore 中有不止一个私钥。索要只包含上传密钥的 keystore。 |
| `android_keystore_key_mismatch` | keystore 中没有与其密钥匹配的证书。 |
| `android_keystore_invalid` | 无法把文件作为 keystore 读取。 |
| `'…' already exists; move it away or pass another --output.` | `export` 从不覆盖文件；换一个路径。 |
| `--output '…' is a directory; name the keystore file.` | 传入文件名。 |

## 示例

查看 App 的当前密钥及其指纹：

```bash
android-key status com.example.app
```

导入成员附上的 keystore，密码已保存在团队的「密码」中：

```bash
android-key import upload.jks --package com.example.app \
  --password-entry "Android keystore com.example.app"
```

把上传证书保存到文件：

```bash
android-key certificate com.example.app --output upload_certificate.pem
```

恢复被替换的密钥：

```bash
android-key activate com.example.app 0b7c2d3e-0000-4000-8000-000000000000
```
