Cordova fails to find ANDROID_HOME environment variable

Viewed 1048

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

1 Answers

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.

Related