# android-key

> Agent 用來管理團隊 Android 簽署金鑰的指令：列出、檢查、建立、匯入、還原與匯出金鑰。

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

`android-key` 用來管理團隊的 Android App 金鑰。AirBuild 為每個 Android App 保存一把使用中的金鑰，該 App 的每個版本都以它簽署：模擬器用的 `.apk`、供人下載的 APK，以及以 `runbuild --sign` 製作的正式版本。被取代的金鑰會封存，絕不刪除，之後也能重新啟用。

## 執行位置

雲端團隊與 Mac 團隊中的 Agent 都能使用同一個指令。登入資訊來自 Agent 的環境，因此無須任何設定。此指令需要 `curl` 與 `jq`，環境已提供。請參閱[選擇 Agent 的執行位置](https://air.build/tw/docs/get-started/where-agents-run/)。

## App 如何取得金鑰

- **自動產生。** App 第一次建置時，若沒有使用中的金鑰，就會建立一把。其來源為 `auto`。
- **透過聊天卡片。** Agent 可以傳送 `android_upload_key` 卡片；使用者在卡片上上傳既有的金鑰庫，或建立新金鑰。
- **使用此指令。** 使用者在聊天中附上金鑰庫時用 `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` 傳入。

## 指令

| 指令 | 作用 |
| --- | --- |
| `list` | 團隊所有 App 的所有金鑰，包含使用中與已封存的。 |
| `status PACKAGE` | App 使用中的金鑰，以及其已封存的金鑰。 |
| `create` | 為 App 建立新金鑰並設為使用中。App 原有的金鑰會封存。 |
| `import FILE` | 將金鑰庫傳送給 AirBuild，經檢查後將其金鑰存為 App 使用中的金鑰。App 原有的金鑰會封存。 |
| `activate PACKAGE KEY_ID` | 將已封存的金鑰重新設為使用中；原本使用中的金鑰會封存。id 可從 `status` 查看。 |
| `certificate PACKAGE` | 以 PEM 格式印出金鑰的憑證。只有憑證，不含私密金鑰。 |
| `export PACKAGE` | 將金鑰寫成金鑰庫檔案，並在旁邊產生一個 `.properties` 檔。僅在使用者要求取得金鑰時使用。 |

使用中的金鑰會簽署 App 之後的每個版本，包括模擬器用的 `.apk`。

## 選項

| 選項 | 指令 | 值 | 預設 | 意義 |
| --- | --- | --- | --- | --- |
| `--package` | `status`, `create`, `import`, `certificate`, `export` | `applicationId` | — | 指定 App。`create` 與 `import` 必須加上。 |
| `--key` | `certificate`, `export` | 金鑰 id（UUID） | 使用中的金鑰 | 指定金鑰。 |
| `--output` | `certificate` | 檔案 | 輸出到 stdout | 將 PEM 寫入此檔案。 |
| `--output` | `export` | 檔案 | `./PACKAGE.keystore` | 金鑰庫的寫入位置。此檔案與 `FILE.properties` 都不得已存在。 |
| `--password-entry` | `import` | 密碼項目名稱 | 從 stdin 讀取 | 從團隊的**密碼**讀取金鑰庫密碼。 |
| `--password-field` | `import` | 欄位標籤 | `Password` | `--password-entry` 中存放密碼的欄位。 |
| `--json` | 全部 | — | 關閉 | 以 JSON 回應取代文字輸出。發生錯誤時，也會將 JSON 錯誤輸出到 stderr。 |

## 匯入金鑰庫

- `FILE` 是使用者的金鑰庫，最大 64 KiB。

- 必須是 **PKCS#12** 格式：也就是目前 Android Studio 與 `keytool` 產生的格式，檔名常仍為 `.jks`。不支援舊的 JKS 格式。請使用者轉換格式，或從 Android Studio 重新匯出：

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

- PKCS#12 金鑰庫只有一組密碼。密碼會從 stdin 的第一行讀取，或以 `--password-entry` 從團隊的密碼項目讀取。密碼絕不會出現在指令列上：`--password` 或 `--store-password` 等選項會被拒絕。請以 `request_password` 卡片向使用者索取密碼，切勿在聊天中詢問（請參閱 [password-book](https://air.build/tw/docs/cli/password-book/)）。

- AirBuild 會檢查密碼，並確認金鑰與其憑證相符。

- 匯入成功後，指令會提醒你刪除金鑰庫檔案以及你製作的任何副本。

## 匯出金鑰

`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.
```

## 結束代碼

| 代碼 | 意義 |
| --- | --- |
| `0` | 成功。 |
| `1` | AirBuild 拒絕了請求，或無法讀取或寫入檔案。 |
| `2` | 用法錯誤：未知的指令或選項、缺少引數，或值的格式錯誤。後面會接著 `Run 'android-key --help' for usage.`。 |

AirBuild 拒絕請求時，指令會印出 `Error: <reason>`，並在下一行印出 `Code: <code>`。原因中會說明該怎麼做。

## 錯誤

指令本身的訊息以 `Error:` 開頭；AirBuild 的拒絕訊息會多一行 `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` | 密碼無法開啟金鑰庫。向使用者索取正確的密碼。 |
| `android_keystore_multiple_keys` | 金鑰庫中有多把私密金鑰。請索取只含上傳金鑰的金鑰庫。 |
| `android_keystore_key_mismatch` | 金鑰庫中沒有與其金鑰相符的憑證。 |
| `android_keystore_invalid` | 無法將檔案讀取為金鑰庫。 |
| `'…' 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
```

匯入使用者附上的金鑰庫，其密碼已存在團隊的**密碼**中：

```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
```
