Share via


建置、測試及部署Android應用程式

Azure DevOps Services

您可以設定管線來自動建置、測試及部署 Android 應用程式。

必要條件

您必須具有下列專案:

  • GitHub 帳戶。 如果您沒有 GitHub 帳戶, 請立即建立一個帳戶。
  • Azure DevOps 專案。 如果您沒有專案, 請立即建立一個專案。

設定管線

執行下列工作來設定範例 Android 應用程式的管線。

  1. 將下列存放庫派生至您的 GitHub 帳戶,以取得簡單 Android 應用程式的程式代碼。

    https://github.com/MicrosoftDocs/pipelines-android
    
  2. 登入您的 Azure DevOps 組織,並移至您的專案。

  3. 選取 [管線>建立管線] 或 [新增管線]。

  4. 選取 [GitHub ] 作為原始程式碼的位置。

    Screenshot showing list of repositories to select from.

    系統可能會將您重新導向至 GitHub 以進行登入。 若是如此,請輸入 GitHub 認證。

  5. 選取您先前分岔的 -android 存放庫。

  6. 選取 [核准],並在 後續畫面上安裝 。

    Azure Pipelines 會產生管線的 YAML 檔案。

  7. 選取執行

  8. 直接認可至主要分支,然後選擇 [再次執行 ]。

  9. 等待執行完畢。

您的存放庫中有一個可運作的 YAML 檔案 (azure-pipelines.yml),可供您自定義。

提示

若要變更 YAML 檔案,請在 [管線] 頁面中選取管線,然後編輯azure-pipelines.yml檔案。

使用 Gradle 建置

Gradle 是用於建置 Android 專案的通用建置工具。 如需選項的詳細資訊,請參閱 Gradle 工作。

# https://learn.microsoft.com/azure/devops/pipelines/ecosystems/android
pool:
  vmImage: 'macOS-latest'

steps:
- task: Gradle@2
  inputs:
    workingDirectory: ''
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx3072m'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    tasks: 'assembleDebug'

調整建置路徑

  • 如果您的gradlew檔案不在存放庫的根目錄中,請調整 workingDirectory 值。 目錄值應該類似於存放庫的根目錄,例如 AndroidApps/MyApp$(system.defaultWorkingDirectory)/AndroidApps/MyApp

  • 如果您的gradlew檔案不在存放庫的根目錄中,請調整 gradleWrapperFile 值。 檔案路徑值應該類似於存放庫的根目錄,例如 AndroidApps/MyApp/gradlew$(system.defaultWorkingDirectory)/AndroidApps/MyApp/gradlew

調整 Gradle 工作

針對您偏好的組建變體調整工作值,例如 assembleDebugassembleRelease。 如需詳細資訊,請參閱下列 Google Android 開發檔:

簽署並對齊 Android 套件 (APK)

如果您的組建尚未簽署 APK,請將 Android 簽署工作新增至 YAML。 APK 必須經過簽署,才能在裝置上執行,而不是模擬器。 Zipaligning 可減少應用程式所耗用的 RAM。

重要

我們建議將下列每一個 密碼儲存在秘密變數中。

- task: AndroidSigning@2
  inputs:
    apkFiles: '**/*.apk'
    jarsign: true
    jarsignerKeystoreFile: 'pathToYourKeystoreFile'
    jarsignerKeystorePassword: '$(jarsignerKeystorePassword)'
    jarsignerKeystoreAlias: 'yourKeystoreAlias'
    jarsignerKeyPassword: '$(jarsignerKeyPassword)'
    zipalign: true

Test

在Android模擬器上測試

建立Bash工作,並複製下列程式代碼以安裝和執行模擬器。 別忘了排列模擬器參數以符合您的測試環境。 模擬器會以背景進程的形式啟動,並在稍後的工作中提供。

#!/usr/bin/env bash

# Install AVD files
echo "y" | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --install 'system-images;android-27;google_apis;x86'

# Create emulator
echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n xamarin_android_emulator -k 'system-images;android-27;google_apis;x86' --force

$ANDROID_HOME/emulator/emulator -list-avds

echo "Starting emulator"

# Start emulator in background
nohup $ANDROID_HOME/emulator/emulator -avd xamarin_android_emulator -no-snapshot > /dev/null 2>&1 &
$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 裝載的裝置上測試

新增 App Center 測試工作,以在 iOS 和 Android 裝置的託管實驗室中測試應用程式。 需要 App Center 免費試用,稍後必須轉換為付費。

請先向 App Center 註冊。

# App Center test v1
# Test app packages with Visual Studio App Center.
- task: AppCenterTest@1
  inputs:
    appFile: # string. Alias: app. Required. Binary application file path. 
    artifactsDirectory: '$(Build.ArtifactStagingDirectory)/AppCenterTest' # string. Alias: artifactsDir. Required. Artifacts directory. Default: $(Build.ArtifactStagingDirectory)/AppCenterTest.
  # Prepare Tests
    #prepareTests: true # boolean. Alias: enablePrepare. Prepare tests. Default: true.
    frameworkOption: 'appium' # 'appium' | 'espresso' | 'calabash' | 'uitest' | 'xcuitest'. Alias: framework. Required when enablePrepare = true. Test framework. Default: appium.
    #appiumBuildDirectory: # string. Alias: appiumBuildDir. Required when enablePrepare = true && framework = appium. Build directory. 
    #espressoBuildDirectory: # string. Alias: espressoBuildDir. Optional. Use when enablePrepare = true && framework = espresso. Build directory. 
    #espressoTestApkFile: # string. Alias: espressoTestApkPath. Optional. Use when enablePrepare = true && framework = espresso. Test APK path. 
    #calabashProjectDirectory: # string. Alias: calabashProjectDir. Required when enablePrepare = true && framework = calabash. Project directory. 
    #calabashConfigFile: # string. Optional. Use when enablePrepare = true && framework = calabash. Cucumber config file. 
    #calabashProfile: # string. Optional. Use when enablePrepare = true && framework = calabash. Profile to run. 
    #calabashSkipConfigCheck: false # boolean. Optional. Use when enablePrepare = true && framework = calabash. Skip Configuration Check. Default: false.
    #uiTestBuildDirectory: # string. Alias: uitestBuildDir. Required when enablePrepare = true && framework = uitest. Build directory. 
    #uitestStorePath: # string. Optional. Use when enablePrepare = true && framework = uitest. Store file. 
    #uiTestStorePassword: # string. Alias: uitestStorePass. Optional. Use when enablePrepare = true && framework = uitest. Store password. 
    #uitestKeyAlias: # string. Optional. Use when enablePrepare = true && framework = uitest. Key alias. 
    #uiTestKeyPassword: # string. Alias: uitestKeyPass. Optional. Use when enablePrepare = true && framework = uitest. Key password. 
    #uiTestToolsDirectory: # string. Alias: uitestToolsDir. Optional. Use when enablePrepare = true && framework = uitest. Test tools directory. 
    #signInfo: # string. Optional. Use when framework = calabash || framework = uitest. Signing information. 
    #xcUITestBuildDirectory: # string. Alias: xcuitestBuildDir. Optional. Use when enablePrepare = true && framework = xcuitest. Build directory. 
    #xcUITestIpaFile: # string. Alias: xcuitestTestIpaPath. Optional. Use when enablePrepare = true && framework = xcuitest. Test IPA path. 
    #prepareOptions: # string. Alias: prepareOpts. Optional. Use when enablePrepare = true. Additional options. 
  # Run Tests
    #runTests: true # boolean. Alias: enableRun. Run tests. Default: true.
    credentialsOption: 'serviceEndpoint' # 'serviceEndpoint' | 'inputs'. Alias: credsType. Required when enableRun = true. Authentication method. Default: serviceEndpoint.
    #serverEndpoint: # string. Required when enableRun = true && credsType = serviceEndpoint. App Center service connection. 
    #username: # string. Required when enableRun = true && credsType = inputs. App Center username. 
    #password: # string. Required when enableRun = true && credsType = inputs. App Center password. 
    appSlug: # string. Required when enableRun = true. App slug. 
    devices: # string. Required when enableRun = true. Devices. 
    #series: 'master' # string. Optional. Use when enableRun = true. Test series. Default: master.
    #dsymDirectory: # string. Alias: dsymDir. Optional. Use when enableRun = true. dSYM directory. 
    localeOption: 'en_US' # 'da_DK' | 'nl_NL' | 'en_GB' | 'en_US' | 'fr_FR' | 'de_DE' | 'ja_JP' | 'ru_RU' | 'es_MX' | 'es_ES' | 'user'. Alias: locale. Required when enableRun = true. System language. Default: en_US.
    #userDefinedLocale: # string. Optional. Use when enableRun = true && locale = user. Other locale. 
    #loginOptions: # string. Alias: loginOpts. Optional. Use when enableRun = true && credsType = inputs. Additional options for login. 
    #runOptions: # string. Alias: runOpts. Optional. Use when enableRun = true. Additional options for run. 
    #skipWaitingForResults: false # boolean. Alias: async. Optional. Use when enableRun = true. Do not wait for test result. Default: false.
  # Advanced
    #cliFile: # string. Alias: cliLocationOverride. App Center CLI location. 
    #showDebugOutput: false # boolean. Alias: debug. Enable debug output. Default: false.

使用組建記錄保留成品

新增複製 檔案發佈組建成品工作 。 您的 APK 會隨著組建記錄或測試一起儲存,並在稍後的管線中部署。 如需詳細資訊,請參閱 成品

- task: CopyFiles@2
  inputs:
    contents: '**/*.apk'
    targetFolder: '$(build.artifactStagingDirectory)'
- task: PublishBuildArtifacts@1

部署

新增App Center

新增 App Center 散發工作,將應用程式散發給測試人員或 Beta 使用者群組,或將應用程式升階至 Intune 或 Google Play。 需要免費的 App Center 帳戶(不需要付款)。

# App Center distribute v3
# Distribute app builds to testers and users via Visual Studio App Center.
- task: AppCenterDistribute@3
  inputs:
    serverEndpoint: # string. Required. App Center service connection. 
    appSlug: # string. Required. App slug. 
    appFile: # string. Alias: app. Required. Binary file path. 
    #buildVersion: # string. Build version. 
    releaseNotesOption: 'input' # 'input' | 'file'. Alias: releaseNotesSelection. Required. Create release notes. Default: input.
    releaseNotesInput: # string. Required when releaseNotesSelection = input. Release notes. 
    #releaseNotesFile: # string. Required when releaseNotesSelection = file. Release notes file. 
    #isMandatory: false # boolean. Require users to update to this release. Default: false.
    destinationType: 'groups' # 'groups' | 'store'. Required. Release destination. Default: groups.
    #distributionGroupId: # string. Alias: destinationGroupIds. Optional. Use when destinationType = groups. Destination IDs. 
    #destinationStoreId: # string. Required when destinationType = store. Destination ID. 
    #isSilent: # boolean. Optional. Use when destinationType = groups. Do not notify testers. Release will still be available to install. 
  # Symbols
    #symbolsOption: 'Apple' # 'Apple' | 'Android' | 'UWP'. Alias: symbolsType. Symbols type. Default: Apple.
    #symbolsPath: # string. Optional. Use when symbolsType == AndroidNative || symbolsType = Windows. Symbols path. 
    #appxsymPath: # string. Optional. Use when symbolsType = UWP. Symbols path (*.appxsym). 
    #symbolsDsymFiles: # string. Alias: dsymPath. Optional. Use when symbolsType = Apple. dSYM path. 
    #symbolsMappingTxtFile: # string. Alias: mappingTxtPath. Optional. Use when symbolsType = Android. Mapping file. 
    #nativeLibrariesPath: # string. Optional. Use when symbolsType == Android. Native Library File Path. 
    #symbolsIncludeParentDirectory: # boolean. Alias: packParentFolder. Optional. Use when symbolsType = Apple. Include all items in parent folder.

安裝Google Play

安裝 Google Play 延伸模組,並使用下列工作來自動化與 Google Play 的互動。 根據預設,這些工作會使用 您設定的服務連線 向Google Play進行驗證。

版本

新增 Google Play Release 工作,將新的 Android 應用程式版本發行至 Google Play 商店。

- task: GooglePlayRelease@4
  inputs:
    apkFile: '**/*.apk'
    serviceEndpoint: 'yourGooglePlayServiceConnectionName'
    track: 'internal'

促銷

新增 Google Play 升級工作,將先前發行的 Android 應用程式更新從一個曲目升級為另一個曲目,例如 alphabeta

- task: GooglePlayPromote@3
  inputs:
    packageName: 'com.yourCompany.appPackageName'
    serviceEndpoint: 'yourGooglePlayServiceConnectionName'
    sourceTrack: 'internal'
    destinationTrack: 'alpha'

增加首度推出

新增 Google Play 增加推出工作,以增加先前發行至rollout曲目的應用程式推出百分比。

- task: GooglePlayIncreaseRollout@2
  inputs:
    packageName: 'com.yourCompany.appPackageName'
    serviceEndpoint: 'yourGooglePlayServiceConnectionName'
    userFraction: '0.5' # 0.0 to 1.0 (0% to 100%)

狀態更新

新增 Google Play 狀態更新工作,以更新先前發行至rollout曲目的應用程式推出狀態。

  - task: GooglePlayStatusUpdate@2
    inputs:
      authType: ServiceEndpoint
      packageName: 'com.yourCompany.appPackageName'
      serviceEndpoint: 'yourGooglePlayServiceConnectionName'
      status: 'inProgress' # draft | inProgress | halted | completed

常見問題集

問:如何? 建立應用程式套件組合?

答:您可以使用內嵌腳本和安全檔案來建置並簽署應用程式套件組合。 若要這樣做,請先下載密鑰存放區,並將其 儲存為連結庫中的安全檔案。 然後,在變數群組中建立 keystore.passwordkey.aliaskey.password變數。

接下來,使用下載安全檔案和Bash工作來下載金鑰存放區並建置並簽署您的應用程式套件組合。

在此 YAML 檔案中,下載 app.keystore 安全檔案,並使用 bash 腳本來產生應用程式套件組合。 然後,使用 複製檔案 來複製應用程式套件組合。 從該處建立並儲存具有 發佈組建成品成品,或使用Google Play延伸模組 發佈。

- task: DownloadSecureFile@1
  name: keyStore
  displayName: "Download keystore from secure files"
  inputs:
    secureFile: app.keystore

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