[android] Failed to find 'ANDROID_HOME' environment variable

I am trying to build an ionic-android project and i have android sdk installed.

SDK manager installed packages

The name of my project is myApp.I have successfully added android platform to myApp. But when i tries to build the project

~/myApp$ sudo ionic build android

result is

Running command: /home/hari/myApp/hooks/after_prepare/010_add_platform_class.js /home/hari/myApp
  add to body class: platform-android
  ERROR building one of the platforms: Failed to find 'ANDROID_HOME'  environment variable. Try setting setting it manually.
  Failed to find 'android' command in your 'PATH'. Try update your 'PATH' to include path to valid SDK directory.
You may not have the required environment or OS to build this project
Error: Failed to find 'ANDROID_HOME' environment variable. Try setting setting it manually.
Failed to find 'android' command in your 'PATH'. Try update your 'PATH' to include path to valid SDK directory.

see the ANDROID_HOME and PATH variable

echo $ANDROID_HOME
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/hari/Android/Sdk

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/hari/Android/Sdk/tools:/home/hari/Android/Sdk/platform-tools:/usr/local/bin:/opt/gradle/bin

I have seen almost the same kind of questions on stack overflow, but none works for me. Is there anything wrong with my android-sdk configuration? How can i build this project?

This question is related to android ionic-framework

The answer is


These are the steps that you need to follow to successfully set up your Ionic Project to work with android emulator:

  1. Create an Ionic Project: ionic start appName tabs (for tab theme)
  2. cd appName
  3. ionic setup sass
  4. 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!


I would like to share an answer that also demonstrates approach using the Android SDK provided by the Ubuntu repository:

Install Android SDK

sudo apt-get install android-sdk

Export environmental variables

export ANDROID_HOME="/usr/lib/android-sdk/"
export PATH="${PATH}:${ANDROID_HOME}tools/:${ANDROID_HOME}platform-tools/"

Came here from google looking for same issue and wasted 4 hours to figure out what could be wrong. And now I feel really stupid while posting this answer. In my case SDK, JDK, JRE, Ant and everything else was installed and working a day before.

But just one particular project was giving me this issue. This one was under "C:\Users\Name\Documents" location

Soon I realized that I was running cmd as normal user, as soon as I choose "Run as Administrator" everything started working.

Tip: Always consider project location carefully!


To set ANDROID_HOME environment on ubuntu 20.04 visit Ubuntu's snap store and install Android studio and then open

vim $HOME/.bashrc

And set the environment variables as follows:

export ANDROID_HOME=${HOME}/Android/Sdk
export PATH=${ANDROID_HOME}/tools:${PATH}
export PATH=${ANDROID_HOME}/emulator:${PATH}
export PATH=${ANDROID_HOME}/platform-tools:${PATH}

With recent versions of Android studio replace ANDROID_HOME with ANDROID_SDK_ROOT otherwise builds will complain that ANDROID_HOME is deprecated.


For those having a portable SDK edition on windows, simply add the 2 following path to your system.

F:\ADT_SDK\sdk\platforms
F:\ADT_SDK\sdk\platform-tools

This worked for me.


I experienced this issue on windows7 computer: the computer shutdown while ionic serve was running (I'm assuming that was the issue that corrupted everything)

Remove node COMPLETELY and reinstall everything on a fresh node copy


I had this issue before.
You need to add sdks\tools and sdks\build-tools to your environment path.


Execute: sudo gedit ~/.bashrc add

JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export JAVA_HOME
PATH=$PATH:$JAVA_HOME
export PATH
export ANDROID_HOME=~/Android/Sdk 
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

and

source ~/.bashrc

In Windows, If you are running this command from VS code terminal and Even after setting up all the environment variable (i.e.build-tools, platform-tools, tools) it is not working trying running the same command from external cmd terminal. In my case even after starting a new VS code terminal, it was not able to take the updated Environment path.

It worked when I ran the same command from Windows cmd.


I experienced same issue on MAC catalina 10.15 what you add in .bash_profile is not recognised when you echo it on terminal, it is stored temporarily. It is recommended to use .zprofile for permanent and add all environment variables in it.After trying for 5hours i found out this solution. Hope it will be useful for someone.

 vi .zprofile
 insert(press i)

export ANDROID_HOME=/Users/mypc/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:${PATH}

after adding environment variables press esc
then enter :wq!
try echo $ANDROID_HOME in new tab(it will not be empty) path will be 
printed

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


April 11, 2019

None of the answers above solved my problem so I wanted to include a current solution (as of April 2019) for people using Ubuntu 18.04. This is how I solved the question above...

  1. I installed the Android SDK from the website, and put it in this folder: /usr/lib/Android/
  2. Search for where the SDK is installed and the version. In my case it was here:

    /usr/lib/Android/Sdk/build-tools/28.0.3

    Note: that I am using version 28.0.3, your version may differ.

  3. Add ANDROID_HOME to the environment path. To do this, open /etc/environment with a text editor:

    sudo nano /etc/environment

    Add a line for ANDROID_HOME for your specific version and path. In my case it was:

    ANDROID_HOME="/usr/lib/Android/Sdk/build-tools/28.0.3"

  4. Finally, source the updated environment with: source /etc/environment

    Confirm this by trying: echo $ANDROID_HOME in the terminal. You should get the path of your newly created variable.

    One additionally note about sourcing, I did have to restart my computer for the VScode terminal to recognize my changes. After the restart, the environment was set and I haven't had any issues since.


This solved my problem. Add below to your system path

PATH_TO_android\platforms

PATH_TO_android\platform-tools


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to ionic-framework

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory in ionic 3 Xcode couldn't find any provisioning profiles matching No provider for Http StaticInjectorError Error: Cannot find module '../lib/utils/unsupported.js' while using Ionic Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK - Android Ionic 2: Cordova is not available. Make sure to include cordova.js or run in a device/simulator (running in emulator) Error: Node Sass does not yet support your current environment: Windows 64-bit with false Failed to find 'ANDROID_HOME' environment variable Ionic android build Error - Failed to find 'ANDROID_HOME' environment variable ionic build Android | error: No installed build tools found. Please install the Android build tools