# android-key

> 에이전트가 팀의 Android 서명 키를 관리할 때 쓰는 명령입니다. 키를 나열, 확인, 생성, 가져오기, 복원, 내보내기 할 수 있습니다.

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

`android-key`는 팀의 Android 앱 키를 관리합니다. AirBuild는 Android 앱마다 활성 키를 하나씩 보관하며, 그 앱의 모든 빌드가 이 키로 서명됩니다: 에뮬레이터용 `.apk`, 사람들이 내려받는 APK, `runbuild --sign`으로 만든 릴리스. 교체된 키는 삭제되지 않고 보관되며, 다시 활성화할 수 있습니다.

## 실행 위치

클라우드 팀과 Mac 팀의 에이전트 모두 같은 명령을 쓸 수 있습니다. 로그인은 에이전트 환경에서 가져오므로 따로 설정할 것이 없습니다. 명령에는 `curl`과 `jq`가 필요하며 환경에 포함되어 있습니다. [에이전트가 실행될 위치 선택하기](https://air.build/ko/docs/get-started/where-agents-run/)를 참고하세요.

## 앱이 키를 얻는 방법

- **자동으로.** 활성 키가 없는 앱의 첫 빌드가 키를 만듭니다. 출처(origin)는 `auto`입니다.
- **채팅 카드로.** 에이전트가 `android_upload_key` 카드를 보내면, 사용자가 카드에서 기존 키스토어를 업로드하거나 새 키를 만듭니다.
- **이 명령으로.** 사용자가 채팅에 키스토어를 첨부하면 `import`, 말로 새 키를 요청하면 `create`를 씁니다.

`create`는 사용자가 새 키를 요청했거나 Google Play에 처음 올리는 앱일 때만 쓰세요. 이미 Google Play에 있는 앱은 업로드 키를 유지해야 합니다.

## 개요

```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`는 `com.example.app` 같은 앱의 `applicationId`입니다. 점으로 구분된 부분이 두 개 이상이고, 각 부분은 문자로 시작하며, 최대 150자입니다. `status`, `create`, `certificate`, `export`는 이를 첫 인수로 받거나 `--package PACKAGE`로 받습니다.

## 명령

| 명령 | 하는 일 |
| --- | --- |
| `list` | 팀이 보관한 모든 앱의 모든 키(활성과 보관). |
| `status PACKAGE` | 앱의 활성 키와 보관된 키. |
| `create` | 앱의 새 키를 만들어 활성화합니다. 앱에 있던 키는 보관됩니다. |
| `import FILE` | 키스토어를 AirBuild로 보내면, AirBuild가 확인한 뒤 그 키를 앱의 활성 키로 저장합니다. 앱에 있던 키는 보관됩니다. |
| `activate PACKAGE KEY_ID` | 보관된 키를 다시 활성화하고, 기존 활성 키는 보관합니다. id는 `status`에 나옵니다. |
| `certificate PACKAGE` | 키의 인증서를 PEM으로 출력합니다. 인증서만 출력하며 개인 키는 없습니다. |
| `export PACKAGE` | 키를 키스토어 파일과 그 옆의 `.properties` 파일로 씁니다. 사용자가 자신의 키를 요청할 때만 쓰세요. |

활성 키는 에뮬레이터용 `.apk`를 포함해 이후 앱의 모든 빌드에 서명합니다.

## 옵션

| 옵션 | 명령 | 값 | 기본값 | 의미 |
| --- | --- | --- | --- | --- |
| `--package` | `status`, `create`, `import`, `certificate`, `export` | `applicationId` | — | 대상 앱. `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 응답을 출력합니다. 오류 시 stderr에도 JSON 오류를 출력합니다. |

## 키스토어 가져오기

- `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/ko/docs/cli/password-book/) 참고).

- AirBuild는 비밀번호와, 키가 인증서와 일치하는지 확인합니다.

- 가져오기에 성공하면 명령이 키스토어 파일과 만들어 둔 다른 사본을 삭제하라고 알려 줍니다.

## 키 내보내기

`export`는 소유자만 읽을 수 있는(mode 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:` 아래에 같은 방식으로 나열합니다. 활성 키가 없는 앱은 `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`은 Google Play Console에 표시되는 인증서의 SHA-256 지문입니다. `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>`를 출력합니다. reason에 해결 방법이 나옵니다.

## 오류

명령 자체의 메시지는 `Error:`로 시작하며, AirBuild의 거부에는 `Code:` 줄이 추가됩니다.

| 메시지 또는 코드 | 해결 방법 |
| --- | --- |
| `'…' is not an Android applicationId (such as com.example.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.` | 파일 이름을 넘기세요. |

## 예시

앱의 활성 키와 지문 확인하기:

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