30

As the title says my jenkins build fails due to not finding the android sdk dispite it being located in the environments path.

Here is the console logs

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$0(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

As u can see, the path clearly has android_home set correctly.

I am trying to build this via gradle in jenkins.

This is teh pre executed shell commands i have configured this job to do

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
Jono
  • 17,341
  • 48
  • 135
  • 217

7 Answers7

68

Fixed it.

  1. Go to Jenkins > Manage Jenkins > Configure System
  2. Check "Environment variables"
  3. add name: ANDROID_HOME, value -> your android sdk dir
  4. click "add"
  5. SCROLL DOWN CLICK SAVE
Jono
  • 17,341
  • 48
  • 135
  • 217
6

The problem is that you are exporting the ANDROID_HOME environment variable in an "Execute shell" step. Once the shell step has completed, that environment is gone — the variables will not be carried over to subsequent build steps.

If you were executing everything, including Gradle, in a single build step, it would work, e.g.

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

But since you're using the Gradle plugin as a separate build step, the environment variables are no longer available.

Exporting the environment variable globally via the Jenkins config as you have done in your own answer is one way to solve this, though you can also use the EnvInject plugin to inject environment variables during a build. Variables set up using this plugin will be available to all build steps.


For a build machine, the best practice would be to minimise the amount of manual setup required, like installing the Android SDK and hardcoding environment variables into the server config.

I can recommend adding the Android SDK Manager plugin to your Gradle build — it will automatically install the Android SDK plus any build dependencies for you.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • I first tried configuring ANDROID_HOME but it did not solved error "local.properties (Le fichier spécifié est introuvable). I then tried your idea by calling export and build in the same call but it did not solved the problem. Any advice ? – Seynorth Sep 26 '17 at 08:59
3

I fixed it using:

  1. Copy your Android folder \Users{yourUser}\Library\Android\
  2. Paste in the folder \Users\Shared\Jenkins\Library
  3. Go to Jenkins -> Manage Jenkins -> Configure System
  4. Check "Environment variables"
  5. add name: ANDROID_HOME, value -> \Users\Shared\Jenkins\Library\Android\sdk
  6. Add
  7. Save
MMSousa
  • 411
  • 3
  • 12
0

You can use EnvInject Plugin add an ANDROID_HOME environment variable in build configuration.

  1. Install EnvInject Plugin if you haven't installed it(usually have done after Jenkins' configuring).

  2. Go to Jenkins->your build->Configuration.

  3. Scroll down to Build Environment.

  4. Find "Inject environment variables to the build process" and check it.

  5. Type "ANDROID_HOME=/you/Android/Sdk/location" in "Properties Content" input field.

  6. Don't forget to save it.

JiajiaGu
  • 1,279
  • 14
  • 10
0

I fixed this issue by adding permission to your android sdk dir

chmod 777 /Users/USER_NAME/Library/Android

Pritesh Patel
  • 678
  • 17
  • 35
0

@Jonathan is mostly right, just add a tip, your SDK path should put in somewhere that Jenkins has permission.
I was stuck when I put my sdk under /root/sdk, in this case even I set ANDROID_HOME, it still doesn't work
After I move the sdk to /usr/local/sdk it works

Kael luo
  • 79
  • 1
  • 5
-1

I imagine that you checked in your local.properties file to version control that contained the path to your local machines SDK file (which is done automatically when setting up a project or opening a project). Change your local.properties file to the default SDK path (so that it will automatically handle the change based on the local machine):

sdk.dir=/Applications/android_sdk

Check that change into version control, and then put local.properties in your .gitignore file (see here for help).

Community
  • 1
  • 1
  • I diddnt check in the local.properties file – Jono Mar 24 '15 at 10:12
  • Just saw your own answer, and thats what I was going to suggest since I thought the pre-script doesn't globally set the environment or live long enough for gradle to access it. I have usually just had the local.properties in my repo when I build so that usually handles the SDK path. Also don't for get to accept your own answer so others see it! – Kurt Bonatz Mar 24 '15 at 13:06