跳过内容

安卓构建

使用 Capgo 的专用基础设施构建和提交 Android 应用到 Google Play 商店。

您将学到什么

您将要学习的内容
  • 您将学习如何通过 Capgo 原生构建上传您的应用
  • 您将学习如何配置 Capgo 原生构建的凭证

前置条件

前置条件
  • 您需要有一个活跃的Google开发者账户
  • 您需要安装Android Studio
  • 您的应用必须能够在Android Studio中成功构建

第一个手动构建

第一个手动构建

在我们开始使用Capgo构建应用之前,我们应该首先设置它,并手动完成第一个Android构建。 手动构建有几个优势:

  • 您将为后续Capgo构建准备凭证
  • 您将在Play Store控制台创建一个记录

使用Android Studio手动构建应用

Building the app manually with Android Studio

为了使用 Capgo,我们需要先使用 Android Studio 手动构建应用。

  1. 打开 Android Studio

    运行 bunx cap open android 以打开 Android Studio 项目。

  2. 点击 Build -> Generate Signed App Bundles / APKs 使用 Android Studio 生成签名的应用包 / APK

  3. 选择 Android App Bundle 并点击 Next 使用 Android Studio 选择 Android App Bundle

创建 Keystore

创建 Keystore

当前,您缺少密钥库文件。该文件用于签署您的应用程序,使Google知道是您开发了该应用程序。 要生成它,我们将使用Android Studio提供的GUI方法。 还有一个使用命令行的方法,但我们不会在本教程中介绍它。

  1. 点击 Create new Android Studio创建新密钥库

  2. 填写密钥库路径 Android Studio填写密钥库路径

  3. 设置密钥库密码 Android Studio设置密钥库密码

  4. 填写剩余表单

    1. 保持密钥别名不变(key0)
    2. 填写证书详细信息。 我已经填写了假数据,但您应该填写自己的详细信息

    Android Studio 填写剩余表单

  5. 点击 OK Android Studio 点击 OK

  1. 确保所有 keystore 详情都已正确填写并点击 Next Android Studio 确保所有 keystore 详情都已正确填写

  2. 选择 release build 变体并点击 Create Android Studio 选择 release build 变体

  3. 构建成功后,您应该看到以下屏幕 Android Studio 构建成功后

    1. 此弹出窗口表示构建成功
    2. 点击 locate 按钮 - 这将打开文件资源管理器,您应该在其中看到构建文件。
  4. 确保您可以在文件资源管理器中看到构建文件 Android Studio 确保您可以在文件资源管理器中看到构建文件

在 Play Store 控制台上创建应用

标题为“在 Play Store 控制台上创建应用”
  1. 前往 Google Play 控制台

  2. 选择正确的开发者账户 Google Play 控制台选择正确的开发者账户

  3. 点击 Create app Google Play 控制台创建应用

  4. 选择应用名称和语言 Google Play Console 选择应用名称和语言

  5. 选择应用类别并确定应用是否收费或免费 Google Play Console 选择应用类别并确定应用是否收费或免费

  6. 接受条款和条件 Google Play Console 接受条款和条件

  7. 点击 Create Google Play Console 点击创建

创建内部测试组

创建内部测试组

现在你已经创建了应用,你可以创建一个内部测试组。由于我不会实际在Play Store发布应用,所以我需要创建一个内部测试组。

  1. 前往 internal testing 点击 Test and release -> Testing -> Internal testing

    Google Play Console内部测试

  2. 点击 Testers Google Play Console 测试者

  3. 点击 Create email list Google Play Console 创建邮件列表

  4. 命名邮件列表 Google Play Console 命名邮件列表

  5. 添加测试者邮箱地址 Google Play Console 添加测试者邮箱地址

  6. Enter 并点击 Save Google Play Console 按 Enter 并点击保存

  7. 点击 Create group Google Play Console 创建组

  8. 确保新列表被选中并点击 Save Google Play Console 确保新列表被选中并点击保存

将应用程序上传到内部测试组

标题:将应用程序上传到内部测试组

现在您已经创建了内部测试组,可以将应用程序上传到内部测试组

  1. 前往 Test and release -> Testing -> Internal testing Google Play Console 内部测试

  2. 点击 Releases 按钮 Google Play Console 发布按钮

  3. 点击 Create new release Google Play Console 创建新发布

  4. 点击 Upload Google Play Console 上传

  5. 选择 AAB 文件 Google Play Console 选择 AAB 文件

  6. 等待 AAB 文件上传

  7. 点击 Next Google Play Console 下一步

  8. 修复错误

    个人来说,在这个阶段我看到这个错误

    Google Play Console 大型警告

    这是因为我还没有验证我的电话号码。 我会做到这一点并继续教程。

  9. 点击 Save and publish 这将发布应用到内部测试组。 Google Play Console 保存并发布

  10. 确认发布 Google Play Console 确认发布

  11. 确保应用已发布 Google Play Console 确保应用已发布

  12. 获取临时应用名称 Google Play Console 获取临时应用名称

接受内部测试组的邀请

接受内部测试组邀请

您已将应用程序上传到内部测试组,接下来可以接受内部测试组的邀请。

  1. 前往 Test and release -> Testing -> Internal testing Google Play Console内部测试

  2. 点击 Testers Google Play Console测试者

  3. 点击 Copy link Google Play Console复制链接

  4. 将链接发送到您的手机,打开浏览器并点击 Accept Google Play Console接受邀请

  5. 确认邀请已被接受并点击“在Play Store下载” Google Play Console在Play Store下载

  6. 安装应用

    1. 如果您在使用 Android Studio 之前已经安装了应用,请点击 uninstall 按钮 Google Play Console 卸载应用
    2. 点击 install 按钮 Google Play Console 安装应用
    3. 打开应用并确认它已成功下载

配置Capgo原生构建(Android)

标题:配置Capgo原生构建(Android)

现在,您已经准备好开始Capgo原生构建的设置了。祝贺您!🎉

要求标志描述必需
存储库文件--keystore <path>您的文件路径 .jks/.keystore 用于签署APK/AAB的文件
密钥库别名--keystore-alias <alias>密钥库内的密钥别名。
密钥库密钥密码--keystore-key-password <password>密钥密码。如果密钥/存储密码匹配,则只需提供一个。请参阅下面的注释
密钥库存储密码--keystore-store-password <password>密钥库密码。如果密钥/存储密码匹配,则只需提供一个。请参阅下面的注释
Google Play服务账户--play-config <path>用于Play Store上传的JSON服务账户文件。
终端窗口
bunx @capgo/cli@latest build credentials save --platform android \
--keystore ./path/to/keystore.jks \
--keystore-alias "your-alias" \
--keystore-key-password "key-password" \
--keystore-store-password "store-password" \
--play-config ./play-store-service-account.json

密钥库、密钥库密码、密钥库密钥密码、密钥库别名

标题为“密钥库、密钥库密码、密钥库密钥密码、密钥库别名”

如果您已经遵循了 手动构建指南,您应该已经生成了密钥库。 如果您没有遵循指南,请遵循它们来生成密钥库。

Google Play服务账户

Google Play 服务账户

生成 Google Play 服务账户是一个手动且复杂的过程。 然而,这是必须要完成的才能将应用程序上传到 Google Play。请记住以下几点:

  • 必须开发者账户的拥有者
  • 。否则,您将无法设置服务账户。

您需要创建一个新的 Google Cloud 项目(与您的 Google Play 账户独立)

  1. 开始吧。 前往

  2. Google Cloud 控制台(Console) Google Console 项目选择器

  3. 如果您已经有一个项目,请选择它。否则,请创建一个新项目:

    1. 点击 New project Google Console 中的新项目按钮
    2. 为您的项目命名并点击 Create 项目命名屏幕显示名称字段和创建按钮
    3. 确保您在正确的项目中 项目名称在选择器中显示,表明正确的项目选择
  4. 让我们点击搜索栏并搜索 service accounts 点击它 Google Console 搜索栏

  5. 让我们点击 Create service account Google Console 中的创建服务账户按钮

  6. 填写服务账户的表单并点击 Done

    1. 我建议将名称设置为 Capgo Native Build Service Account
    2. 对于服务账户 ID,我建议将其设置为 capgo-native-build-service-acc
    3. 对于描述,您不必填写,但我建议将其填写为 Allows Capgo Native Build to build and submit the app to the Play Store

    Google Console 填写服务账户的表单

  7. 点击新创建的服务账户 您现在应该在列表中看到新创建的服务账户。点击它 Google Console 新创建的服务账户

  8. 点击 Keys 选项卡 Google Console中的

  9. 密钥选项卡 Add Key 点击 Create new key 并

  10. 在Google Console中添加密钥并创建新密钥 JSON 点击 Create 并

  11. 被后续使用。 close 下载 JSON 文件 JSON 文件应该已经自动下载了。您可以点击 关闭窗口。

Granting Play Store API access to the service account

为 Play Store API 配置服务账户

标题:为 Play Store API 配置服务账户

  1. 前往 Google Play Console
  2. 选择正确的开发者账户 Google Play Console 选择正确的开发者账户
  3. 点击 Users and permissions Google Play Console 用户和权限
  4. 点击 Invite new users Google Play Console 邀请新用户
  5. 复制服务账户的电子邮件地址 Google Play Console 复制服务账户的电子邮件地址
  6. 前往 Account permissions 并授予最少的必要权限:
    • App permissions,允许访问您的应用。
    • Releases,启用 Create, edit, and roll out releases.
    • 如果您的工作流程使用Play App Signing,启用相关的签名权限。
    • 如果您不确定,使用 Admin 仅在设置期间使用,然后减少权限。 Google Play Console 授予权限
  7. 点击 Invite user Google Play Console 邀请用户
  8. 确认邀请 Google Play Console 确认邀请
  9. 确认用户已被邀请 Google Play Console 确认用户已被邀请

您现在准备好保存凭据并运行第一个构建。 您可以使用以下命令保存凭据:

终端窗口
bunx @capgo/cli@latest build credentials save --platform android \
--keystore ./path/to/keystore.jks \
--keystore-alias "your-alias" \
--keystore-key-password "key-password" \
--keystore-store-password "store-password" \
--play-config ./play-store-service-account.json

CI/CD 设置(GitHub Actions)

标题:CI/CD 设置(GitHub Actions)

如果您已经完成 Keystore、Keystore 密码、Keystore 密钥密码、Keystore 别名Google Play 服务账户,你已经拥有CI/CD所需的一切。 本节仅介绍如何将这些值作为GitHub Actions机密和环境变量传递。

1) 将凭据文件转换为单行base64

标题:1) 将凭据文件转换为单行base64
终端窗口
# Android keystore (.jks or .keystore)
base64 -i ./path/to/keystore.jks | tr -d '\n' > keystore_base64.txt
# Google Play service account JSON
base64 -i ./play-store-service-account.json | tr -d '\n' > play_config_base64.txt

2) 创建仓库机密

标题:2) 创建仓库机密

In GitHub > Repository > Settings > Secrets and variables > Actions,

添加:密钥名称
CAPGO_TOKENYour Capgo API token
APP_ID您的 Capgo __CAPGO_KEEP_1__ token com.example.app)
ANDROID_KEYSTORE_FILE您的 __CAPGO_KEEP_0__ 应用程序 ID(例如: keystore_base64.txt
KEYSTORE_KEY_ALIAS内容 从密钥库中获取别名
KEYSTORE_KEY_PASSWORD密钥库、密钥库密码、密钥库密钥密码、密钥库别名
KEYSTORE_STORE_PASSWORD密钥库密钥密码
PLAY_CONFIG_JSON密钥库存储密码 play_config_base64.txt

3) 在您的GitHub Actions工作流中使用环境变量

标题为“3) 在您的GitHub Actions工作流中使用环境变量”
github/workflows/android-build.yml
name: Android Cloud Build
on:
workflow_dispatch:
push:
branches: [main]
jobs:
android-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Request Android build with Capgo
run: bunx @capgo/cli@latest build request ${{ secrets.APP_ID }} --platform android
env:
CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}
ANDROID_KEYSTORE_FILE: ${{ secrets.ANDROID_KEYSTORE_FILE }}
KEYSTORE_KEY_ALIAS: ${{ secrets.KEYSTORE_KEY_ALIAS }}
KEYSTORE_KEY_PASSWORD: ${{ secrets.KEYSTORE_KEY_PASSWORD }}
KEYSTORE_STORE_PASSWORD: ${{ secrets.KEYSTORE_STORE_PASSWORD }}
PLAY_CONFIG_JSON: ${{ secrets.PLAY_CONFIG_JSON }}

恭喜你!你现在已经准备好运行第一个构建了。

Run:

终端窗口
bunx @capgo/cli@latest build request com.example.app --platform android

这将启动构建过程

默认情况下,Android 发布构建将上传到 Google Play,并且可以保持不活动状态直到您在 Play 控制台完成发布。如果您的 CI 发布应该自动提交 Play 发布,请传递 --submit-to-store-review with a release build:

终端窗口
npx @capgo/cli@latest build request com.example.app \
--platform android \
--build-mode release \
--submit-to-store-review \
--store-release-name "1.2.3" \
--store-release-notes "General improvements" \
--store-release-notes-locale "en-US=General improvements" \
--store-release-notes-locale "nl-NL=Algemene verbeteringen" \
--store-release-notes-locale "fr-FR=Ameliorations generales"

需要使用相同的Google Play服务帐户设置,和正常的上传路径(PLAY_CONFIG_JSON 在CI或保存Android凭据本地使用。 --store-release-name 用于Google Play发布名称。 --store-release-notes 是Play的fallback更改日志, --store-release-notes-locale "locale=notes" entry writes a localized Play changelog for that locale. Capgo sends the Play release as completed条目为该区域写入Play的本地化更改日志。

从Android Builds继续 标题:从Android Builds继续 如果您正在使用 Capgo CI/CD 为产品工作流程在Capgo CI/CD中 Capgo 本机构建 为产品工作流程在Capgo 本机构建中 Capgo 集成 为产品工作流程在Capgo 集成中 CI/CD 集成 CI/CD 集成的实现细节 GitHub 动作集成 for the implementation detail in GitHub Actions Integration.