Skip to content

Instantly share code, notes, and snippets.

@bradobro
Last active December 10, 2018 22:30
Show Gist options
  • Save bradobro/83ecfb75120f1535203666b67f144cf6 to your computer and use it in GitHub Desktop.
Save bradobro/83ecfb75120f1535203666b67f144cf6 to your computer and use it in GitHub Desktop.
getting `cordova build android` working on MacOS Dec 2018

Getting Cordova to Build Android on a Mac

This works in Dec 2018 on MacOS High-Sierra. If you find changes that work in a later time or on later releases, suggest an edit.

Java, Cordova, Android, Android Studio, MacOS--five moving targets that don't stay in sync with each other. Commands and paths change (e.g. android is out, sdkmanager is in, but it doesn't open a UI as some helps suggest.) Mac Java versions can get wonky, and the Android SDK is picky about versions too.

Setting Up Cordoba

Follow the first Cordova app guide.

Add some platforms: cordova platform add android; cordova platform add ios.

Run cordova requirements -d. If your system flunks (and it probably will if you haven't done mobile work on the install machine before), deal with Android and iOS builds according to the sections below.

iOS

Install Xcode from the app store if you don't already have it.

Start Xcode. If it asks to install more things, let it.

If the iOS section of cordova requirements -d fails or cordova build ios fails, web search for the error messages.

Android

Get the Right Java

Glance through Ionic's Android Installation Guide. See which version of Java they recommend.

Install that version of Java. This worked for me:

brew tap caskroom/versions
brew cask install android-sdk
brew cask install homebrew/cask-versions/java8

I tried the .dmg directly from Oracle, and I expect that's what the formula uses, but it didn't work for me.

Set Up Android Studio

Download and install Android Studio.

Create a new sample project. Android Studio presents a option to create a new HelloWorld app when you start it.

Add an Android Emulator (I used Pixel 2). Help search is your friend.

Build the sample app. Run it in the emulator. Power off the emulator (but do not close it).

Set Up Android SDK

Look in Android Studio's SDK settings: menu Android Studio | Preferences outline select Appearance & Behavior | System Settings | Android SDK

Notice the path it gives to the Android SDK, then add these system paths to your default shell. Make sure you prepend them to the path (instead of append). I added this to .bash_profile, then restarted the shell (Open a fresh terminal tab, close the previous, run printenv to check your new settings.):

# manually added for android sdk
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
export PATH=$ANDROID_SDK_ROOT/tools/bin:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/emulator:$PATH

Keep running cordova requirements -d and remedying any problems it reports in the Android build section. One tip is to use Android Studio's SDK settings to install requisites, such as build tools and Android targets. Brew did not seem to do the right thing in this realm.

Confirm the build: cordova build android.

When that works, try cordova emulate android. Don't expect to see more than a Cordova icon. If the emulator fails with authorization errors, try starting the emulator first in Android Studio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment