Start the appium server from code with AppiumDriverLocalService object

I am trying to start the Appium server programmatically as follows:

AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
service.start();

Seems that the service is starting
Then I try to initiate the android driver as follows:
driver = new AndroidDriver(service.getUrl(),capabilities);

Now The execution is failing with the below =log.
Any idea what am I missing?

ENV:
Appium 1.6.1
mac osx 10.12.1
java-client 4.1.1
Eclipse

[TestNG] Running:
/Users/sgmacbookpro/Appium/Projects/SG_Android_Appium/src/resources/Testng.xml

[Appium] Welcome to Appium v1.6.1 (REV ee51200036dac07568da29b8aa88c0335ea6a514)
[Appium] Appium REST http interface listener started on 0.0.0.0: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.6.1”,“revision”:“ee51200036dac07568da29b8aa88c0335ea6a514”}}
[HTTP] <-- GET /wd/hub/status 200 26 ms - 121
[HTTP] --> POST /wd/hub/session {“desiredCapabilities”:{“app”:"/Users/sgmacbookpro/Appium/Android/SkyGiraffe.apk",“newCommandTimeout”:1200,“appiumVersion”:“1.6.1”,“platformVersion”:“5.0”,“browserName”:“android”,“platformName”:“Android”,“version”:"",“deviceName”:“Samsung”,“platform”:“ANDROID”}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{“app”:"/Users/sgmacbookpro/Appium/Android/SkyGiraffe.apk",“newCommandTimeout”:1200,“appiumVersion”:“1.6.1”,“platformVersion”:“5.0”,“browserName”:“android”,“platformName”:“Android”,“version”:"",“deviceName”:“Samsung”,“platform”:“ANDROID”},null,null,null,null]
[Appium] Creating new AndroidDriver session
[Appium] Capabilities:
[Appium] app: ‘/Users/sgmacbookpro/Appium/Android/SkyGiraffe.apk’
[Appium] newCommandTimeout: 1200
[Appium] appiumVersion: ‘1.6.1’
[Appium] platformVersion: ‘5.0’
[Appium] browserName: ‘android’
[Appium] platformName: ‘Android’
[Appium] version: ‘’
[Appium] deviceName: ‘Samsung’
[Appium] platform: ‘ANDROID’
[debug] [AndroidDriver] AndroidDriver version: 1.10.35
[BaseDriver] The following capabilities were provided, but are not recognized by appium: appiumVersion, version, platform.
[AndroidDriver] The desired capabilities should generally not include both an app and a browser
[BaseDriver] Session created with session id: 23b3342f-fc2b-49a1-8fd4-3e0793371cde
[debug] [AndroidDriver] Getting Java version
[AndroidDriver] Java version is: 1.8.0_40
[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 (lib/logger.js:60:13)
at ADB.callee$0$0$ (lib/tools/system-calls.js:85: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)
Error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at Object.wrappedLogger.errorAndThrow (lib/logger.js:60:13)
at ADB.callee$0$0$ (lib/tools/system-calls.js:85: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)
[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 (lib/logger.js:60:13)
at ADB.callee$0$0$ (lib/tools/system-calls.js:85: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)
[HTTP] <-- POST /wd/hub/session 500 281 ms - 246

That’s the problem. You need to set the environment variable ANDROID_HOME in either the shell you are using or the IDE, possibly both.

Thanks!
I have run the following commands in the terminal:
JAVA_HOME=$(/usr/libexec/java_home)
export PATH=${JAVA_HOME}/bin:$PATH
Is this what you meant by “set the environment variable ANDROID_HOME in the shell” ? (seems that after I run it, the appium doctor state that it was set however closing & reopening the terminal cause the appium doctor to state that java home wasn’t set )

Can you explain how to do it on the eclipse?
Thanks

Hi,
According the appium doctor:
:heavy_check_mark: ANDROID_HOME is set to: /Users/sgmacbookpro/Appium/sdk

So doesn’t it mean that the Android home was set?

Eclipse doesn’t always read environment variables, unfortunately. You may want to read through this:

http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_run_env.htm

Thanks Wreed!

Indeed after adding the ANDROID_HOME variable to the java project it worked!

Thanks!

ANDROID_HOME
/Users/sgmacbookpro/Appium/sdk

===============================
The solution for the below error:
[ADB] The ANDROID_HOME environment variable is not set to the Android SDK root directory path.

Open the Configuration for you project by right click–>Run As–>Run Configuration

Add the ANDROID_HOME variable & it’s path to the android sdk on your machine as follows:

1 Like