教程:使用 GitLab Mobile DevOps 构建 Android 应用
在本教程中,你将使用 GitLab CI/CD 创建一个流水线,该流水线会构建你的 Android 移动应用,使用你的凭据对其进行签名,并将其分发到应用商店。
要设置移动 DevOps:
开始之前
在开始本教程之前,请确保你拥有:
- 一个可以访问 CI/CD 流水线的 GitLab 账户
- 你的移动应用代码在 GitLab 仓库中
- 一个 Google Play 开发者账户
- 在本地安装了
fastlane
设置你的构建环境
使用 GitLab 托管的 runners, 或者设置 自管理的 runners 来完全控制构建环境。
Android 构建使用 Docker 镜像,提供多个 Android API 版本。
-
在你的仓库根目录创建一个
.gitlab-ci.yml文件。 -
从 Fabernovel 添加一个 Docker 镜像:
test: image: fabernovel/android:api-33-v1.7.0 stage: test script: - fastlane test
使用 fastlane 和 Gradle 配置代码签名
为 Android 设置代码签名:
-
创建一个密钥库(keystore):
-
运行以下命令生成密钥库文件:
keytool -genkey -v -keystore release-keystore.jks -storepass password -alias release -keypass password \ -keyalg RSA -keysize 2048 -validity 10000 -
将密钥库配置放在
release-keystore.properties文件中:storeFile=.secure_files/release-keystore.jks keyAlias=release keyPassword=password storePassword=password -
将这两个文件作为 Secure Files 上传到你的项目设置中。
-
将这两个文件添加到你的
.gitignore文件中,这样它们就不会被提交到版本控制中。
-
-
配置 Gradle 使用新创建的密钥库。在应用的
build.gradle文件中:-
在 plugins 部分之后立即添加:
def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('.secure_files/release-keystore.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } -
在
android块中的任何位置添加:signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } -
将
signingConfig添加到 release 构建类型:signingConfig signingConfigs.release
-
以下是带有此配置的示例 fastlane/Fastfile 和 .gitlab-ci.yml 文件:
-
fastlane/Fastfile:default_platform(:android) platform :android do desc "Create and sign a new build" lane :build do gradle(tasks: ["clean", "assembleRelease", "bundleRelease"]) end end -
.gitlab-ci.yml:build: image: fabernovel/android:api-33-v1.7.0 stage: build script: - apt update -y && apt install -y curl - curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash - fastlane build
使用 Google Play 集成和 fastlane 设置 Android 应用分发
已签名的构建可以通过使用 Mobile DevOps Distribution 集成上传到 Google Play 商店。
- 在 Google Cloud Platform 中 创建一个 Google 服务账户, 并授予该账户访问 Google Play 中项目的权限。
- 启用 Google Play 集成:
- 在左侧边栏,选择 Search or go to 并找到你的项目。
- 选择 Settings > Integrations。
- 选择 Google Play。
- 在 Enable integration 下,选择 Active 复选框。
- 在 Package name 中,输入应用的包名。例如,
com.gitlab.app_name。 - 在 Service account key (.JSON) 中拖放或上传你的密钥文件。
- 选择 Save changes。
- 将发布步骤添加到你的流水线中。
以下是示例 fastlane/Fastfile:
default_platform(:android)
platform :android do
desc "Submit a new Beta build to the Google Play store"
lane :beta do
upload_to_play_store(
track: 'internal',
aab: 'app/build/outputs/bundle/release/app-release.aab',
release_status: 'draft'
)
end
end以下是示例 .gitlab-ci.yml:
beta:
image: fabernovel/android:api-33-v1.7.0
stage: beta
script:
- fastlane beta概述请参见 Google Play 集成演示。
恭喜!你的应用现在已经设置为自动构建、签名和分发。尝试创建一个合并请求来触发你的第一个流水线。
相关主题
有关完整的 Android 构建、签名和发布流水线示例,请查看 Mobile DevOps Android Demo 项目。
有关更多参考资料,请查看 GitLab 博客的 DevOps 部分。