首页 > 解决方案 > Azure DevOps 管道 android 构建设置 sdk 位置

问题描述

我正在尝试使用 yaml 管道构建一个 android 应用程序。gradle build 任务抱怨它找不到android sdk root。我已经尝试了 local.properties 文件和环境变量 ANDROID_SDK_ROOT,但都不起作用。这是我的构建管道:

# Android
# Build your Android project with Gradle.
# Add steps that test, sign, and distribute the APK, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/android

trigger:
- master
- dev

pool:
 name: 'Default'

steps:

- task: UseDotNet@2
  inputs:
    version: '2.2.x'
    packageType: runtime
    
- task: GitVersion@5
  displayName: GitVersion
  inputs:
    useConfigFile: true
    configFilePath: GitVersion.yml

# trying to set the env variable doesn't work.
# - task: PowerShell@2 
#   inputs:
#     targetType: 'inline'
#     script: |
#       # Write your PowerShell commands here.
      
#       [Environment]::SetEnvironmentVariable("ANDROID_SDK_ROOT", "C:\Users\Administrator\AppData\Local\Android\Sdk", "User")

- task: file-creator@6
  inputs:
    filepath: '$(System.DefaultWorkingDirectory)/local.properties'
    filecontent: 'sdk.dir=C:/Users/Administrator/AppData/Local/Android/Sdk'
    fileoverwrite: true

# trying to execute gradlew manually does not work
# - task: CmdLine@2
#   inputs:
#     script: |
#       gradlew.bat assembleDebug
#     workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: Gradle@2
  inputs:
    workingDirectory: '$(System.DefaultWorkingDirectory)'
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx3072m'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    tasks: 'assembleDebug'

这是输出:

Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/gradle
==============================================================================
SYSTEMVSSCONNECTION exists true
C:\Windows\system32\cmd.exe /D /S /C "C:\VSTS\_work\8\s\gradlew.bat -p C:\VSTS\_work\8\s assembleDebug"
Starting a Gradle Daemon, 1 incompatible and 4 stopped Daemons could not be reused, use --status for details

> Configure project :app
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at 'C:\VSTS\_work\8\s\local.properties'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 16s
Error: The process 'C:\VSTS\_work\8\s\gradlew.bat' failed with exit code 1
    at ExecState._setResult (C:\VSTS\_work\_tasks\Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4\2.176.0\node_modules\azure-pipelines-task-lib\toolrunner.js:816:25)
    at ExecState.CheckComplete (C:\VSTS\_work\_tasks\Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4\2.176.0\node_modules\azure-pipelines-task-lib\toolrunner.js:799:18)
    at ChildProcess.<anonymous> (C:\VSTS\_work\_tasks\Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4\2.176.0\node_modules\azure-pipelines-task-lib\toolrunner.js:721:19)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:920:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:230:5)
##[error]Error: The process 'C:\VSTS\_work\8\s\gradlew.bat' failed with exit code 1
Finishing: Gradle

如果我 rdp 进入构建代理并使用命令行中生成的 local.properties 文件手动运行 gradlew ,那么构建确实会成功,所以我不确定在管道中运行它的问题是什么。

标签: androidgradleazure-devops

解决方案


您可以通过在变量部分中定义它来设置 Azure 管道中的环境变量。您可以将以下内容添加到您的 yaml 管道中。

variables:
  ANDROID_SDK_ROOT: 'C:\Users\Administrator\AppData\Local\Android\Sdk'

您还可以使用脚本task.setvariable中的日志记录命令设置环境变量 ANDROID_SDK_ROOT 。对于下面的例子。

steps:

  - powershell: echo "##vso[task.setvariable variable=ANDROID_SDK_ROOT]C:\Users\Administrator\AppData\Local\Android\Sdk"

推荐阅读