Failed to find 'ANDROID_HOME' environment variable
Solution 1:
You may want to confirm that your development environment has been set correctly.
Quoting from spring.io:
Set up the Android development environment
Before you can build Android applications, you must install the Android SDK. Installing the Android SDK also installs the AVD Manager, a graphical user interface for creating and managing Android Virtual Devices (AVDs).
From the Android web site, download the correct version of the Android SDK for your operating system.
Unzip the archive to a location of your choosing. For example, on Linux or Mac, you can place it in the root of your user directory. See the Android Developers web site for additional installation details.
Configure the
ANDROID_HOME
environment variable based on the location of the Android SDK. Additionally, consider addingANDROID_HOME/tools
, andANDROID_HOME/platform-tools
to your PATH.Mac OS X
export ANDROID_HOME=/<installation location>/android-sdk-macosx export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
Linux
export ANDROID_HOME=/<installation location>/android-sdk-linux export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
Windows
set ANDROID_HOME=C:\<installation location>\android-sdk-windows set PATH=%PATH%;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools
The Android SDK download does not include specific Android platforms. To run the code in this guide, you need to download and install the latest SDK platform. You do this by using the Android SDK and AVD Manager that you installed in the previous section.
Open the Android SDK Manager window:
android
Note: If this command does not open the Android SDK Manager, then your path is not configured correctly.
Select the Tools checkbox.
Select the checkbox for the latest Android SDK.
From the Extras folder, select the checkbox for the Android Support Library.
Click the Install packages... button to complete the download and installation.
Note: You may want to install all the available updates, but be aware it will take longer, as each API level is a large download.
Solution 2:
In Linux
First of all set ANDROID_HOME in .bashrc file
Run command
sudo gedit ~/.bashrc
set andoid sdk path where you have installed
export ANDROID_HOME=/opt/android-sdk-linux
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
to reload file run command
source ~/.bashrc
Now check installed platform, run command
ionic platform
Output
Installed platforms:
android 6.0.0
Available platforms:
amazon-fireos ~3.6.3 (deprecated)
blackberry10 ~3.8.0
browser ~4.1.0
firefoxos ~3.6.3
ubuntu ~4.3.4
webos ~3.7.0
if android already installed then need to remove and install again
ionic platform rm android
ionic platform add android
If not installed already please add android platform
ionic platform add android
Please make sure you have added android platform without sudo command
if you still getting error in adding android platfrom like following
Error: EACCES: permission denied, open '/home/ubuntu/.cordova/lib/npm_cache/cordova-android/6.0.0/package/package.json'
Please go to /home/ubuntu/ and remove .cordova folder from there
cd /home/ubuntu/
sudo rm -r .cordova
Now run following command again
ionic platform add android
after adding platform successfully you will be able to build andoid in ionic.
Thanks
Solution 3:
These are the steps that you need to follow to successfully set up your Ionic Project to work with android emulator:
- Create an Ionic Project: ionic start appName tabs (for tab theme)
- cd appName
- ionic setup sass
- To start the app on web: ionic serve
To add android platform:
Priori Things
First you need to setup the environment variables. For this you need to consider 3 files:
1. ~/.profile (For setting up the variables every time terminal opens or computer boots up):
//Code that you need to append at the last
set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"
export ANDROID_HOME='/home/<user_name>/Android/Sdk' <Path to android SDK>
export PATH=$PATH:$ANDROID_HOME/bin
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools
2. /etc/environment (to set the environment variables):
//All the content of the file
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
ANDROID_HOME="/home/<user_name>/Android/Sdk" <Path to android SDK>
3. /etc/profile:
//Code that you need to add at the last
JAVA_HOME=/usr/lib/jvm/java-8-oracle <Path where Java is installed>
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
For loading the above changes made to the file you need to run the following command:
source ~/.profile
source /etc/environment
source /etc/profile
Posteori Things
1. Add platform: ionic platform add android (Note that you need to run this command without sudo)
2. If you are still getting error in the above command then do the following: (here appName = helloWorld)
cd ..
sudo chmod -R 777 helloWorld
cd helloWorld
ionic platform add android
If you are still getting the error then remove ".cordova" folder from the home directory of your PC.
3. To run the app in your android emulator: ionic run android
Thanks!
Solution 4:
I had this issue before.
You need to add sdks\tools
and sdks\build-tools
to your environment path.