In this tutorial, you will learn, how to setup Android Studio on Mac OS Sierra and how to create a simple Ionic 2 project. And how to generate the Android APK file.

Prerequisite
1. Java
2. Android Studio

You must first install Java before installing the Android Studio.

Install Java

Download Java 1.8 using the below link

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Download the dmg file and double click to install the Java. Once you finished the installation, go to terminal and type

java -version
javac -version

The above two command will return the installed Java version. If the above command is not working, then the Java is not installed properly or Java is not configured in the PATH. However, most of the times it won't fail.

Install Android Studio

Download the Android studio using the below link

https://developer.android.com/studio/index.html

It is also dmg file. Double click and finish the installation.

Add Android Studio Path

After installing Android Studio, you must include it in the PATH. Execute the below command in the terminal.

export ANDROID_HOME=/Your_Dir_Location/Android/sdk
export PATH=$PATH:$ANDROID_HOME/bin

Update Android Studio SDK

Open Android Studio and click SDK manager


Install the latest SDK tools like 5,6,7 versions. It will take some time depend on your internet connection.

Create Ionic 2 Project

You need the Ionic framework, before creating the Ionic 2 project. If you have not installed Ionic Framework, then visit the below link to learn, how to install the Ionic framework.

https://ampersandacademy.com/tutorials/ionic-framework-version-2/installing-ionic-2-framework-on-mac-os-sierra

Create a new Ionic 2 project using

ionic start app_name blank --v2

After creating Ionic 2 project, add Android platform to the Ionic project using

ionic platform add android

Then generate APK  using the below command

ionic build android

After generating the APK successfully, the terminal will display the APK generated location.

That's all. You were done.

Please note. Now you can install the APK in your Android phone by enabling the Install from Unknown Source in settings. However You are not able to publish this APK to Google Play store. To publish to the Google Play Store, you need to do some extra work. I write a separate post soon for how to publish your APK to the Google Play Store using Ionic 2 Framework.

While generating the APK in Mac OS Sierra, sometimes you will face the below error message

Error: Could not find Gradle wrapper within Android SDK. Might need to update your Android SDK - Android

If you encounter that error, then follow this solution.

cd ~/Library/Android/sdk
# download latest tools
curl -O https://dl.google.com/android/repository/tools_r25.2.3-macosx.zip
# overwrite existing tools folder without prompting
unzip -o tools_r25.2.3-macosx.zip
# clean up
rm tools_r25.2.3-macosx.zip