由于缺少 android SDK,Jenkins 构建失败

Jenkins build failed due to missing android SDK

正如标题所说,由于找不到 android sdk,我的 jenkins 构建失败,尽管它位于环境路径中。

这是控制台日志

Started by user anonymous
Building in workspace /Users/lmnapp/.jenkins/jobs/Android_app_master/workspace
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://bitbucket.org/xxxxx-xxx/xxxx-android-app.git # timeout=10
Fetching upstream changes from https://bitbucket.org/xxxx-mob/xxxx-android-app.git
 > git --version # timeout=10
using .gitcredentials to set credentials
 > git config --local credential.helper store --file=/var/folders/gj/xxxxxxxx/T/xxxxx.credentials # timeout=10

+ export ANDROID_HOME=/Users/lmnapp/android-sdk-macosx
+ ANDROID_HOME=/Users/lmnapp/android-sdk-macosx
+ export ANDROID_SDK=/Users/lmnapp/android-sdk-macosx/platforms
+ ANDROID_SDK=/Users/lmnapp/android-sdk-macosx/platforms
+ export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Users/lmnapp/android-sdk-macosx/tools:/Users/lmnapp/android-sdk-macosx/platform-tools:/Users/lmnapp/android-sdk-macosx
+ PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Users/lmnapp/android-sdk-macosx/tools:/Users/lmnapp/android-sdk-macosx/platform-tools:/Users/lmnapp/android-sdk-macosx
+ export AVD_NAME=jenkinsGoogleApi18_1
+ AVD_NAME=jenkinsGoogleApi18_1
+ echo displayPath
displayPath
+ echo ....
....
+ echo ....
....
+ echo /usr/bin:/bin:/usr/sbin:/sbin:/Users/lmnapp/android-sdk-macosx/tools:/Users/lmnapp/android-sdk-macosx/platform-tools:/Users/lmnapp/android-sdk-macosx
/usr/bin:/bin:/usr/sbin:/sbin:/Users/lmnapp/android-sdk-macosx/tools:/Users/lmnapp/android-sdk-macosx/platform-tools:/Users/lmnapp/android-sdk-macosx
+ adb start-server
[Gradle] - Launching build.
[workspace] $ /Users/lmnapp/.jenkins/jobs/Android_app_master/workspace/gradlew assembleProductionRelease
Failed to notify ProjectEvaluationListener.afterEvaluate(), but primary configuration failure takes precedence.
java.lang.RuntimeException: SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.
    at com.android.build.gradle.internal.SdkHandler.getAndCheckSdkFolder(SdkHandler.java:103)
    at com.android.build.gradle.internal.SdkHandler.getSdkLoader(SdkHandler.java:113)
    at com.android.build.gradle.internal.SdkHandler.initTarget(SdkHandler.java:87)
    at com.android.build.gradle.BasePlugin.ensureTargetSetup(BasePlugin.groovy:467)
    at com.android.build.gradle.BasePlugin.access[=11=](BasePlugin.groovy)
    at com.android.build.gradle.BasePlugin$_createTasks_closure9.doCall(BasePlugin.groovy:372)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:278)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
    at groovy.lang.Closure.call(Closure.java:423)
    at org.gradle.listener.ClosureBackedMethodInvocationDispatch.dispatch(ClosureBackedMethodInvocationDispatch.java:40)
    at org.gradle.listener.ClosureBackedMethodInvocationDispatch.dispatch(ClosureBackedMethodInvocationDispatch.java:25)
    at org.gradle.listener.BroadcastDispatch.dispatch(BroadcastDispatch.java:83)
    at org.gradle.listener.BroadcastDispatch.dispatch(BroadcastDispatch.java:31)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy12.afterEvaluate(Unknown Source)
    at org.gradle.configuration.project.LifecycleProjectEvaluator.notifyAfterEvaluate(LifecycleProjectEvaluator.java:79)
    at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:65)
    at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:504)
    at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:83)
    at org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:47)
    at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:129)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:80)
    at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)
    at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:36)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)
    at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:51)
    at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:171)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
    at org.gradle.launcher.Main.doAction(Main.java:33)
    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:30)
    at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:127)
    at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:55)

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/lmnapp/.jenkins/jobs/Android_app_master/workspace/project/build.gradle' line: 120

* What went wrong:
A problem occurred evaluating project ':project'.
> SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

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

BUILD FAILED

如您所见,路径显然 android_home 设置正确。

我正在尝试通过 gradle 在 jenkins 中构建它。

这是预先执行的 shell 命令,我已将此作业配置为执行

export ANDROID_HOME=/Users/lmnapp/android-sdk-macosx
export ANDROID_SDK=/Users/lmnapp/android-sdk-macosx/platforms
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$ANDROID_HOME
export AVD_NAME=jenkinsGoogleApi18_1

echo displayPath
echo ....
echo ....

echo $PATH

我想您已将 local.properties 文件签入包含本地机器 SDK 文件路径的版本控制(在设置项目或打开项目时自动完成)。将您的 local.properties 文件更改为默认 SDK 路径(以便它会根据本地机器自动处理更改):

sdk.dir=/Applications/android_sdk

检查版本控制中的更改,然后将 local.properties 放入您的 .gitignore 文件 (see here for help)。

已修复。

  1. 转到 Jenkins > 管理 Jenkins > 配置系统
  2. 勾选"Environment variables"
  3. 添加名称:ANDROID_HOME,值 -> 您的 android sdk 目录
  4. 点击"add"
  5. 向下滚动点击保存

问题是您在 "Execute shell" 步骤中导出 ANDROID_HOME 环境变量。 shell 步骤完成后,该环境就消失了——变量将不会转移到后续构建步骤。

如果您在单个构建步骤中执行所有内容,包括 Gradle,它会起作用,例如

export ANDROID_HOME=/foo/bar/sdk
./gradlew clean assembleProductionRelease

但是由于您将 Gradle 插件用作单独的构建步骤,因此环境变量不再可用。

像您在自己的答案中所做的那样通过 Jenkins 配置全局导出环境变量是解决此问题的一种方法,但您也可以使用 EnvInject plugin 在构建期间注入环境变量。使用此插件设置的变量将可用于所有构建步骤。


对于构建机器,最佳做法是尽量减少所需的手动设置,例如安装 Android SDK 并将环境变量硬编码到服务器配置中。

我可以建议将 Android SDK Manager plugin 添加到您的 Gradle 构建中 — 它会自动为您安装 Android SDK 以及任何构建依赖项。

我使用以下方法修复了它:

  1. 复制您的 Android 文件夹 \Users{yourUser}\Library\Android\
  2. 粘贴到文件夹\Users\Shared\Jenkins\Library
  3. 转到 Jenkins -> 管理 Jenkins -> 配置系统
  4. 勾选"Environment variables"
  5. 添加名称:ANDROID_HOME,值 -> \Users\Shared\Jenkins\Library\Android\sdk
  6. 添加
  7. 保存

您可以使用 EnvInject 插件在构建配置中添加一个 ANDROID_HOME 环境变量。

  1. 如果你还没有安装 EnvInject 插件(通常在 Jenkins 配置之后已经完成)。

  2. 转到 Jenkins->你的构建->配置。

  3. 向下滚动到构建环境。

  4. 找到"Inject environment variables to the build process"并检查。

  5. 在 "Properties Content" 输入字段中键入 "ANDROID_HOME=/you/Android/Sdk/location"。

  6. 别忘了保存哦

我通过向您的 android sdk 目录添加权限解决了这个问题

chmod 777 /Users/USER_NAME/Library/Android

@Jonathan 基本上是对的,只需添加一个提示,您的 SDK 路径应该放在 Jenkins 有权访问的地方。
当我把我的sdk放在/root/sdk下时,我被卡住了,在这种情况下,即使我设置了ANDROID_HOME,它仍然不起作用
在我将 sdk 移至 /usr/local/sdk 后,它工作了