"Unable to locate a Java Runtime" error when building for Android on Mac

The ‘run android’ command kept failing on my Mac (running Big Sur) with the following error:

Running Gradle build - failed!
[capacitor] [error] The operation couldn’t be completed. Unable to locate a Java Runtime.

I struggled for a while with it so just wanted to share what fixed it for me:

  • Find the SDK path in Android Studio (Android Studio > Preferences… > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK)
  • Set JAVA_HOME to that path in your .zshrc or .bashrc file. For example by adding the line:
    export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home
  • Quit terminal, reopen and try running again

Hope this helps someone :slight_smile:

1 Like

Thank you for posting it. You saved my time :slight_smile: