Error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported

Hi, I am facing issue with appium 2.0.1. Appium inspector is unable to connect to appium server with below error.
Encountered internal error running command: Error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported

My env command shows :

And my ~/.zshrc has

export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.19)
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:${PATH}
export ANDROID_HOME=/Users/siddharth/Library/Android/sdk
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH

I have tried reinstalling appium but no luck. Please help.

Appium server Logs:

[debug] [Appium] Requiring driver at /Users/siddharth/.appium/node_modules/appium-uiautomator2-driver
[AppiumDriver@10c5] Appium v2.0.1 creating new AndroidUiautomator2Driver (v2.29.3) session
[AppiumDriver@10c5] Checking BaseDriver versions for Appium and AndroidUiautomator2Driver
[AppiumDriver@10c5] Appium's BaseDriver version is 9.3.16
[AppiumDriver@10c5] AndroidUiautomator2Driver's BaseDriver version is 9.3.16
[debug] [AndroidUiautomator2Driver@92f9] Creating session with W3C capabilities: {
[debug] [AndroidUiautomator2Driver@92f9]   "alwaysMatch": {
[debug] [AndroidUiautomator2Driver@92f9]     "platformName": "Android",
[debug] [AndroidUiautomator2Driver@92f9]     "appium:automationName": "UiAutomator2",
[debug] [AndroidUiautomator2Driver@92f9]     "appium:newCommandTimeout": 3600,
[debug] [AndroidUiautomator2Driver@92f9]     "appium:connectHardwareKeyboard": true
[debug] [AndroidUiautomator2Driver@92f9]   },
[debug] [AndroidUiautomator2Driver@92f9]   "firstMatch": [
[debug] [AndroidUiautomator2Driver@92f9]     {}
[debug] [AndroidUiautomator2Driver@92f9]   ]
[debug] [AndroidUiautomator2Driver@92f9] }
[AndroidUiautomator2Driver@92f9] The following capabilities were provided, but are not recognized by Appium:
[AndroidUiautomator2Driver@92f9]   connectHardwareKeyboard
[AndroidUiautomator2Driver@92f9 (b02c1dff)] Session created with session id: b02c1dff-96f3-4269-91a8-0b488a5c4288
[AndroidUiautomator2Driver@92f9 (b02c1dff)] Neither 'app' nor 'appPackage' was set. Starting UiAutomator2 without the target application
[debug] [AndroidUiautomator2Driver@92f9 (b02c1dff)] Deleting UiAutomator2 session
[debug] [AppiumDriver@10c5] Event 'newSessionStarted' logged at 1690992668486 (21:41:08 GMT+0530 (India Standard Time))
[debug] [AppiumDriver@10c5] Encountered internal error running command: Error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. Read https://developer.android.com/studio/command-line/variables for more details
[debug] [AppiumDriver@10c5]     at requireSdkRoot (/Users/siddharth/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-adb/lib/helpers.js:82:11)
[debug] [AppiumDriver@10c5]     at Function.createADB (/Users/siddharth/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-adb/lib/adb.ts:71:39)
[debug] [AppiumDriver@10c5]     at Object.createBaseADB (/Users/siddharth/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/android-helpers.js:133:20)
[debug] [AppiumDriver@10c5]

Also, appium-doctor when run as standalone, gives below result:

It looks as though your server log output is from some kind of IDE. Do you think it’s important to add the detail of what IDE you are using to get help with this problem?

Hi, The logs are not from an IDE. I am running it from cmd line. Please see below screenshot.

Ok, my apologies. I do see you are using sudo to run appium, you may need to do something special to keep environment variables available for such a scenario:

https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo

Thanks for the solution. The problem was indeed with user permissions.