Jenkins - Cannot start appium using java-client-3.3.0 - The ANDROID_HOME environment variable is not set to the Android SDK root directory path

On Jenkins, I cannot start appium. Everything is working perfectly fine locally on my mac. What am I doing wrong? I am using java-client-3.3.0

The error message is “The ANDROID_HOME environment variable is not set to the Android SDK root directory path”, even though I have $ANDROID_HOME set properly on the Jenkins machine as below.

*** Error shown in the Jenkins console.

�[36minfo�[39m: [debug] Creating new appium session fc064d02-0807-4a25-b5a4-556ba951b7df
�[36minfo�[39m: Starting android appium
�[36minfo�[39m: [debug] Getting Java version
�[36minfo�[39m: Java version is: 1.8.0_65
�[36minfo�[39m: [debug] Checking whether adb is present
�[33mwarn�[39m: The ANDROID_HOME environment variable is not set to the Android SDK root directory path. ANDROID_HOME is required for compatibility with SDK 23+. Checking along PATH for adb.
�[36minfo�[39m: [debug] executing cmd: which adb
�[36minfo�[39m: [debug] Cleaning up android objects
�[36minfo�[39m: [debug] Cleaning up appium session
�[31merror�[39m: Failed to start an Appium session, err was: Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
�[36minfo�[39m: [debug] Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.

**** My Jenkins shell script

export PATH=$PATH:/usr/local/bin
export ANDROID_HOME=/usr/local/opt/android-sdk
export PATH=$PATH:/usr/local/bin:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools/
adb devices
echo $PATH
echo $ANDROID_HOME

They all show correct info!

*** My test
AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();

@BeforeTest
public void beforeTest() throws Exception {
service.start();

}

@AfterTest
public void afterTest() throws IOException {
doAfterMethod();
doTeardown();
service.stop();
}

*** My .bach_profile on the Jenkins machine.
jenkins$ cat ~/.bash_profile
export PATH="/usr/local/sbin:$PATH"
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
export PATH="/usr/local/bin:$PATH"
export ANDROID_HOME=/usr/local/opt/android-sdk
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools/

I figured out a solution myself after trying gazillion other things.

Jenkins has a checkbox named “Inject environment variables to the build process”. I checked the checkbox and entered ANDROID_HOME=/Users/jenkins/Library/Android/sdk.

Everything is working as expected on Jenkins now. Hope this helps if anyone else is having the same issue on Jenkins.