生成、测试和部署 Android 应用
Azure DevOps Services
本快速入门将介绍如何在 Azure Pipelines 中设置 YAML 管道,以便自动生成、测试和部署 Android 应用。
先决条件
Azure DevOps 组织和项目拥有管道创建和应用部署权限。 要创建项目,请参阅在 Azure DevOps 中创建项目。
一个 GitHub 帐户。
重要
在 GitHub 流程中,系统可能会提示你创建 GitHub 服务连接 或重定向到 GitHub 以登录、安装或授权 Azure Pipelines,或者对 GitHub 组织进行身份验证。 按照屏幕上的说明完成该过程。 有关详细信息,请参阅访问 GitHub 存储库。
创建并运行管道
执行以下任务,为简单的 Android 应用程序设置管道。
- 要获取示例应用的代码,请在 GitHub 帐户下创建 Android 示例应用存储库分支。
- 在 Azure DevOps 项目中,如果此管道是项目中的第一个管道,选择“管道”、“新建管道”或“创建管道”>。
- 选择 GitHub 作为源代码位置。
- 在“选择存储库”屏幕上,选择分支 Android 示例存储库。
- 在“配置管道”屏幕上,选择“Android”。
- Azure Pipelines 基于 Android 模板提供初学者管道。 查看管道代码。
- 选择保存并运行。
- 可以选择编辑“提交消息”并提供说明。 然后再次选择“保持并运行”将 azure-pipelines.yml 文件提交到存储库并启动生成。
“生成和运行”页显示生成详细信息和进度。 如果要查看管道的运行情况,请选择页面下方的“作业”。
现在,存储库中有一个可用的 Android YAML 管道 (azure-pipelines.yml),可供你自定义。
自定义管道
要更改管道,请在管道页面上选择“编辑”。 以下部分介绍了自定义 Android 管道的一些常见方法。
配置 Gradle
初学者 YAML 管道使用适用于 Android 项目的常见开源生成工具 Gradle。 有关详细信息,请参阅 Gradle 任务。
在示例任务中,参数 tasks
生成 assembleDebug
生成类型。 你可以调整所需的生成变体的 tasks
值,例如 build
、test
和 assembleRelease
。
示例任务还假定 gradlew
文件位于存储库的根目录中。 如果没有,相应地调整 workingDirectory
和 gradleWrapperFile
值。
workingDirectory
应该类似于存储库的根目录,例如 AndroidApps/MyApp
或 $(system.defaultWorkingDirectory)/AndroidApps/MyApp
。 gradleWrapperFile
路径应该类似于存储库的根目录,例如 AndroidApps/MyApp/gradlew
或 $(system.defaultWorkingDirectory)/AndroidApps/MyApp/gradlew
。
- task: Gradle@3
inputs:
workingDirectory: ''
gradleWrapperFile: 'gradlew'
gradleOptions: '-Xmx3072m'
publishJUnitResults: false
testResultsFiles: '**/TEST-*.xml'
tasks: 'assembleDebug'
有关使用 Gradle 任务的详细信息,请参阅 Gradle 文档中的使用任务。 有关生成任务的详细信息,请参阅 Google Android 开发文档中的 生成调试 APK 和 配置生成变体。
对 Android 包 (APK) 进行签名并对齐
要在设备而不是模拟器上运行,必须对 Android 应用程序包 (APK) 进行签名。 Zipaligning 减少了应用程序消耗的 RAM。 如果你的生成尚未对 APK 进行签名和压缩对齐,请将 Android 签名任务添加到管道中。 有关详细信息,请参阅为移动应用签名。
为了安全起见,请将 jarsignerKeystorePassword
和 jarsignerKeyPassword
存储在机密变量中,并在管道中使用这些变量。
- task: AndroidSigning@2
inputs:
apkFiles: '**/*.apk'
jarsign: true
jarsignerKeystoreFile: 'pathToYourKeystoreFile'
jarsignerKeystorePassword: '$(jarsignerKeystorePassword)'
jarsignerKeystoreAlias: 'yourKeystoreAlias'
jarsignerKeyPassword: '$(jarsignerKeyPassword)'
zipalign: true
在 Android Emulator 中进行测试
要安装和运行 Android Emulator,请将 Bash 任务添加到管道,并粘贴以下代码。 该模拟器作为后台进程启动,可在后续任务中使用。 根据测试环境安排模拟器参数。
重要
如果使用 Microsoft 托管代理,请将 MacOS 代理映像与 Android 模拟器一起使用。 当前的 Android 仿真器需要硬件加速才能启动。 Azure DevOps 托管的 Ubuntu 代理不支持硬件加速。
- task: Bash@3
inputs:
targetType: 'inline'
script: |
#!/usr/bin/env bash
# Install AVD files
echo "y" | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --install 'system-images;android-35;google_apis;x86_64'
# Create emulator
echo "y" | $ANDROID_HOME/cmdline-tools/latest/bin/avdmanager create avd -n xamarin_android_emulator -d "Nexus 10" -k 'system-images;android-35;google_apis;x86_64' --force
echo "y" | $ANDROID_HOME/emulator/emulator -list-avds
echo "Starting emulator"
# Start emulator in background
nohup $ANDROID_HOME/emulator/emulator -avd xamarin_android_emulator -no-snapshot -no-window -no-audio -no-boot-anim -accel on > /dev/null 2>&1 &
# Fixed quoting around "\r"
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d "\r") ]]; do sleep 1; done; input keyevent 82'
$ANDROID_HOME/platform-tools/adb devices
echo "Emulator started"
在 Azure 托管的设备上进行测试
要在 Visual Studio App Center 中测试 Android 设备的托管实验室中的应用,请将 App Center 测试任务添加到管道。
此任务需要 App Center 免费试用版帐户,该帐户必须在 30 天后转换为付费帐户才能继续使用测试实验室。 在使用此任务之前注册 App Center 帐户。
警告
Visual Studio App Center 计划于 2025 年 3 月 31 日停用。 详细了解支持时间线和建议的替代方案。
以下示例运行 App Center 测试套件。 该任务使用必须设置的服务连接。
有关完整的任务语法和参考,请参阅 App Center 测试任务。 有关详细信息,请参阅使用 Azure DevOps 进行 UI 测试。
- task: AppCenterTest@1
inputs:
appFile: path/myapp.ipa
artifactsDirectory: '$(Build.ArtifactStagingDirectory)/AppCenterTest'
frameworkOption: 'appium'
appiumBuildDirectory: test/upload
serverEndpoint: 'My App Center service connection'
appSlug: username/appIdentifier
devices: 'devicelist'
将工件与生成记录一起保留
要将 APK 文件与生成记录一起存储或要在后续管道中进行测试并部署,请将复制文件和发布生成工件任务添加到管道中。 有关详细信息,请参阅发布和下载管道工件。
- task: CopyFiles@2
inputs:
contents: '**/*.apk'
targetFolder: '$(build.artifactStagingDirectory)'
- task: PublishBuildArtifacts@1
inputs:
pathToPublish: $(Build.ArtifactStagingDirectory)
artifactName: MyBuildOutputs
部署到 App Center
要将应用分发给一组测试人员或 beta 版用户,或将应用升级到 Intune 或 Google Play,请添加 App Center 分发任务。 该任务需要使用仍处于免费试用阶段的免费 App Center 帐户。
以下示例将应用分发给用户。 有关完整的任务语法和参考,请参阅 App Center 分发。 有关详细信息,请参阅使用 App Center 部署 Azure DevOps 生成。
- task: AppCenterDistribute@3
inputs:
serverEndpoint: 'AppCenter'
appSlug: '$(APP_CENTER_SLUG)'
appFile: '$(APP_FILE)' # Relative path from the repo root to the APK file you want to publish
symbolsOption: 'Android'
releaseNotesOption: 'input'
releaseNotesInput: 'Here are the release notes for this version.'
destinationType: 'groups'
安装 Google Play 扩展并部署到 Google Play
要自动化与 Google Play 的交互,请安装 Google Play 扩展,然后使用以下任务。 默认情况下,这些任务会使用你必须配置的服务连接对 Google Play 进行身份验证。
版本
要将新的 Android 应用版本发布到 Google Play 商店,请将 Google Play 发布任务添加到管道中。
- task: GooglePlayRelease@4
inputs:
apkFile: '**/*.apk'
serviceEndpoint: 'yourGooglePlayServiceConnectionName'
track: 'internal'
升级
要将以前发布的 Android 应用程序更新从一个轨道提升到另一个轨道(例如 alpha
→ beta
),请将 Google Play 提升任务添加到管道中。
- task: GooglePlayPromote@3
inputs:
packageName: 'com.yourCompany.appPackageName'
serviceEndpoint: 'yourGooglePlayServiceConnectionName'
sourceTrack: 'internal'
destinationTrack: 'alpha'
增大推出范围
要增大之前发布到 rollout
轨道的应用程序的推出百分比,请将 Google Play 增大推出任务添加到管道中。
- task: GooglePlayIncreaseRollout@2
inputs:
packageName: 'com.yourCompany.appPackageName'
serviceEndpoint: 'yourGooglePlayServiceConnectionName'
userFraction: '0.5' # 0.0 to 1.0 (0% to 100%)
更新状态
要更新之前发布到 rollout
轨道的应用程序的推出状态,请将 Google Play 状态更新任务添加到管道中。
- task: GooglePlayStatusUpdate@2
inputs:
authType: ServiceEndpoint
packageName: 'com.yourCompany.appPackageName'
serviceEndpoint: 'yourGooglePlayServiceConnectionName'
status: 'inProgress' # draft | inProgress | halted | completed
创建一个应用程序包
你可以使用内联脚本和安全文件生成应用程序包并对其进行签名。
下载密钥存储并将其作为安全文件存储在 Azure Pipelines 库中。
在你的 YAML 管道中:
添加下载安全文件任务以下载 app.keystore 安全文件。
- task: DownloadSecureFile@1 name: keyStore displayName: "Download keystore from secure files" inputs: secureFile: app.keystore
将 Bash 任务与 Bash 脚本配合使用以生成应用程序包,并对其进行签名。
- task: Bash@3 displayName: "Build and sign App Bundle" inputs: targetType: "inline" script: | msbuild -restore $(Build.SourcesDirectory)/myAndroidApp/*.csproj -t:SignAndroidPackage -p:AndroidPackageFormat=aab -p:Configuration=$(buildConfiguration) -p:AndroidKeyStore=True -p:AndroidSigningKeyStore=$(keyStore.secureFilePath) -p:AndroidSigningStorePass=$(keystore.password) -p:AndroidSigningKeyAlias=$(key.alias) -p:AndroidSigningKeyPass=$(key.password)
使用复制文件任务以复制应用程序包。
- task: CopyFiles@2 displayName: 'Copy deliverables' inputs: SourceFolder: '$(Build.SourcesDirectory)/myAndroidApp/bin/$(buildConfiguration)' Contents: '*.aab' TargetFolder: 'drop'
在这里,你可以使用发布生成工件任务创建并保存工件,或使用 Google Play 扩展 发布应用程序包。
相关扩展
- Codified Security (Codified Security)
- Google Play (Microsoft)
- 适用于 iOS 和 Android 的移动应用任务 (James Montemagno)
- 移动测试实验室 (Perfecto Mobile)
- React Native (Microsoft)