[C#] "Could not find adb please set ANDROID_HOME" only happens using code to start appium server instead of opening appium manually (desktop or commond line)

Hi guys,

I have set all path in bash_profile.

export ANDROID_HOME=/Users/xxx/Library/Android/sdk
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=${JAVA_HOME}/bin:$PATH
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platforms-tools
export PATH=${PATH}:${ANDROID_HOME}/build-tools/27.0.1
export PATH=${PATH}:${JAVA_HOME}

When I open Appium desktop and start the server at first, everything is fine and enable to detect adb device. Also, using the command line like (appium -p 4723) no issues.

However, if I directly run in visual studio without opening appium server at first,

        AppiumServiceBuilder appiumServiceBuilder = new AppiumServiceBuilder()
            .UsingAnyFreePort();

        AppiumLocalService appiumLocalService = appiumServiceBuilder.Build();
        if (!appiumLocalService.IsRunning)
            appiumLocalService.Start();
        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.SetCapability("deviceName", "EM1");
        capabilities.SetCapability("platformVersion", "6.0");
        capabilities.SetCapability("platformName", "Android");
        capabilities.SetCapability("appPackage", "com.thlonline.telme");
        capabilities.SetCapability("appActivity", "com.thlonline.telme.MainActivity");
    
        AndroidDriver<AndroidElement> androidDriver = new AndroidDriver<AndroidElement (appiumLocalService, capabilities);

the error occurs as follows:

[ADB] 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.

The full log as follows:

[Appium] Welcome to Appium v1.7.2
[Appium] Non-default server args:
[Appium] address: 127.0.0.1
[Appium] Appium REST http interface listener started on 127.0.0.1:4723
[HTTP] → GET /wd/hub/status {}
[debug] [MJSONWP] Calling AppiumDriver.getStatus() with args:
[debug] [MJSONWP] Responding to client with driver.getStatus() result: {“build”:{“version”:“1.7.2”,“revision”:null}}
[HTTP] ← GET /wd/hub/status 200 15 ms - 72
[HTTP] → POST /wd/hub/session {“desiredCapabilities”:{“deviceName”:“EM1”,“platformVersion”:“6.0”,“platformName”:“Android”,“appPackage”:“com.thlonline.telme”,“appActivity”:“com.thlonline.telme.MainActivity”}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{“deviceName”:“EM1”,“platformVersion”:“6.0”,“platformName”:“Android”,“appPackage”:“com.thlonline.telme”,“appActivity”:“com.thlonline.telme.MainActivity”},null,null]
[debug] [BaseDriver] Event ‘newSessionRequested’ logged at 1520906109271 (14:55:09 GMT+1300 (NZDT))
[Appium] Creating new AndroidDriver (v1.37.0) session
[Appium] Capabilities:
[Appium] deviceName: EM1
[Appium] platformVersion: 6.0
[Appium] platformName: Android
[Appium] appPackage: com.thlonline.telme
[Appium] appActivity: com.thlonline.telme.MainActivity
[BaseDriver] Session created with session id: 930fe437-057c-4667-9f42-f09d025b201f
[debug] [AndroidDriver] Getting Java version
[AndroidDriver] Java version is: 1.8.0_131
[ADB] Checking whether adb is present
[ADB] 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.
[ADB] Error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at Object.wrappedLogger.errorAndThrow (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/logging.js:69:13)
at ADB.callee$0$0$ (/usr/local/lib/node_modules/appium/node_modules/appium-adb/lib/tools/system-calls.js:127:9)
at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
Error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at Object.wrappedLogger.errorAndThrow (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/logging.js:69:13)
at ADB.callee$0$0$ (/usr/local/lib/node_modules/appium/node_modules/appium-adb/lib/tools/system-calls.js:127:9)
at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
[debug] [AndroidDriver] Shutting down Android driver
[debug] [AndroidDriver] Called deleteSession but bootstrap wasn’t active
[MJSONWP] Encountered internal error running command: Error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at Object.wrappedLogger.errorAndThrow (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/logging.js:69:13)
at ADB.callee$0$0$ (/usr/local/lib/node_modules/appium/node_modules/appium-adb/lib/tools/system-calls.js:127:9)
at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at
[HTTP] ← POST /wd/hub/session 500 265 ms - 246

I investigated the whole day, looks like in Java needs to set ANDROID_HOME in the IDE, but I don’t know how to do this in visual studio using C#. Please help, many thanks!

Hey @galenyjl

Do below Steps

1.Select “Run” in Eclpse Menubar
2.Select Run->RunConfigurations->Environment Tab->Click new and Give Name “Path” and Paste the Entire path

You can get entire Path by typing below command in terminal

echo $PATH

Hope this resolves the issue. Please let me know the if u face any issue