3

I looked all over Google and Stack Overflow for an answer, but I couldn't find the right one for me. I am learning Cordova and I'm trying to run the android emulator through terminal, and when I run 'sudo cordova emulate android' I get this message:

''Failed to find 'ANDROID_HOME' environment variable. Try setting it manually. Detected 'avdmanager' command at /Users/username/Library/Android/sdk/cmdline-tools/latest/bin but no 'tools/bin' directory found near. Try reinstall Android SDK or update your PATH to include valid path to SDK/tools/bin directory.''

But I set the variables in the ~./bash_profile file and it looks like this:

export ANDROID_HOME="/Users/username/Library/Android/sdk"

export ANDROID_SDK_ROOT=$ANDROID_HOME

export PATH=${PATH}:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools

export PATH=${PATH}:$ANDROID_HOME/tools/bin

export PATH="$PATH:$ANDROID_HOME/build-tools/30.0.0-rc2"

I am using a MacOS Mojave, does anyone know what the issue is? Thanks in advance

  • Does this answer your question? [Setting ANDROID\_HOME enviromental variable on Mac OS X](https://stackoverflow.com/questions/19986214/setting-android-home-enviromental-variable-on-mac-os-x) – Reza Dehnavi Mar 23 '20 at 20:56
  • Have you ever seen [this post](https://stackoverflow.com/questions/19986214/setting-android-home-enviromental-variable-on-mac-os-x) – Reza Dehnavi Mar 23 '20 at 20:56
  • @RezaDehnavi thanks for the help, it still doesn't work, I'll try it on Windows. – Leon Grubisic Mar 26 '20 at 09:42

2 Answers2

2

Apparently, in the latest version of Android Studio, the old SDK tools (under /tools, which Cordova seems to expect) are already considered obsolete. There is a solution documented here on how to install the old version of SDK tools.

  • Open Android Studio
  • Open Tools -> Sdk Manager
  • Click on the Sdk Tools Tab
  • Uncheck Hide Obsolete Packages
  • Check Android Sdk Tools (Obsolete)
  • Click Apply

This should download the old SDK tools under the correct folder.

However, I think Cordova also needs to keep up with the newest Android development tools. There's another change I needed to make after this:

I had to delete the tools/emulator and tools/emulator-check binaries, and add $ANDROID_HOME/emulator to my $PATH, since the current emulator binary lives under emulator/emulator instead of path/emulator. See this post.

Then I was finally able to launch the app directly via Cordova.

xji
  • 7,341
  • 4
  • 40
  • 61
1

TL;DR : don't set ANDROID_HOME at all.

I encountered this error in 2023, and downgrading the JDK or SDK tools would give me a whole bunch of other issues with gradle etc.

cordova seemed to need ANDROID_SDK_ROOT to be set, but ANDROID_SDK_ROOT clashed with ANDROID_HOME and resulted in the above error.

I've changed my .zshrc to completely unset ANDROID_HOME; note that this is backwards since ANDROID_SDK_ROOT is deprecated in favour of ANDROID_HOME.

My .zshrc file now has the following :

# cordova-android was complaining it couldn't find the tools bin, for now I have to use ...
#  ... ANDROID_SDK_ROOT and ensure ANDROID_HOME is not set
export ANDROID_SDK_ROOT="/Users/me/Library/Android/sdk"
export PATH="${PATH}:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/platform-tools:${ANDROID_SDK_ROOT}/emulator:${ANDROID_SDK_ROOT}/tools/bin:${ANDROID_SDK_ROOT}/tools"
unset ANDROID_HOME
cobberboy
  • 5,598
  • 2
  • 25
  • 22