141

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?

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55
  • 2
    Your ANDROID_HOME looks more like a PATH. When you echo, it should only return `/home/hari/Android/Sdk` (assuming that's correct) – OneCricketeer Mar 24 '16 at 13:30
  • See my answer on [This Question ](http://stackoverflow.com/questions/35110403/failed-to-find-android-home-environment-variable-try-setting-setting-it-manua/40061081#40061081) it will fix it for you – Ramy hakam Oct 15 '16 at 15:51
  • I have to enter this everytime I open the terminal, how do I persist it? – Luke Flournoy Sep 29 '17 at 20:30
  • @LukeFlournoy See the "Priori Things" (steps 1 and 2) of [this answer](https://stackoverflow.com/a/41206148/2646969) – Diego Somar Mar 03 '19 at 10:02

17 Answers17

216

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).

  1. From the Android web site, download the correct version of the Android SDK for your operating system.

  2. 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.

  3. Configure the ANDROID_HOME environment variable based on the location of the Android SDK. Additionally, consider adding ANDROID_HOME/tools, and ANDROID_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.

  1. 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.

  2. Select the Tools checkbox.

  3. Select the checkbox for the latest Android SDK.

  4. From the Extras folder, select the checkbox for the Android Support Library.

  5. 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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Brad W
  • 2,540
  • 2
  • 18
  • 28
  • 1
    in no 3 (configure ANRDOID_HOME variable): In which file should this be? – suMi Nov 16 '16 at 07:38
  • @suMi you run the export command from your terminal based on your OS and change the PATH that you set `ANDROID_HOME` equal to based on where you saved the SDK. See http://stackoverflow.com/questions/19986214/setting-android-home-enviromental-variable-on-mac-os-x for more details. – Brad W Nov 19 '16 at 15:21
  • @suMI If you want the changes permanent, add those lines to your .bashrc if you're on linux or mac. – brt Dec 11 '16 at 17:36
  • if anyone running into this problem building from teamcity, you need to create env parameters on teamcity for ANDROID_HOME. – Laurence Dec 05 '17 at 23:22
  • What is that `/android-sdk-linux`, `tools` and `platform-tools`? I don't see such directories in my `/opt/android-studio`. My android Studio is running but i have no those directories. – Dzintars Nov 09 '18 at 22:26
  • @Dzintars those files are found in the main Android directory not the Android Studio directory – Peter Meadley Jun 11 '19 at 11:09
  • Now it's recommended to use ANDROID_SDK_ROOT instead of ANDROID_HOME as the later one is deprecated. – Balu Nov 07 '22 at 17:30
41

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

Dineshaws
  • 2,065
  • 16
  • 26
30

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!

Community
  • 1
  • 1
nikhil
  • 531
  • 5
  • 11
  • Thanks Nikhil, it worked for me too. After trying lots of things - it worked with those above steps you provided :-) – Rajeev Jayaswal Dec 07 '17 at 12:56
  • I ended up having a 'login loop' issue after adding the Java and android variables to the /etc/environment. Luckily I was able to remove that from the terminal login (ctrl+alt+F3) and the login is normal now. – Harry Apr 26 '19 at 14:54
9

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

sschrass
  • 7,014
  • 6
  • 43
  • 62
Jalaleddin Hosseini
  • 2,142
  • 2
  • 25
  • 28
8

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.

BoCyrill
  • 1,219
  • 16
  • 17
7

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.

bsheps
  • 1,438
  • 1
  • 15
  • 26
5

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
Diego Salguero
  • 301
  • 4
  • 7
2

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/"
Muhammad Numan
  • 23,222
  • 6
  • 63
  • 80
2

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.

Hamfri
  • 1,979
  • 24
  • 28
1

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!

Hitin
  • 442
  • 7
  • 21
1

This solved my problem. Add below to your system path

PATH_TO_android\platforms

PATH_TO_android\platform-tools

PKInd007
  • 388
  • 2
  • 7
0

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

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
DudeOfLayers
  • 130
  • 2
  • 10
0

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.

himanshu goyal
  • 363
  • 2
  • 9
0

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
0

Spent almost hours without sleeping searching everything to install apk on my android device for debugging, but couldn't be able to install the apk due to ADP ENONET.

"adb reverse": spawnSync Users/shanu/Library/Android/sdk/platform-tools/adb ENOENT

The solution was very simple, literally, I've added variables into .bash_profile, .zshrc, environment, ~/.profile etc and almost tried every solution but couldn't be able to solve it.

The mistake was minor.

Instead of adding like this (Correct) in my bash_profile file.

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

I was adding something like this which was wrong in my case.

  export ANDROID_HOME=Users/shanu/Library/Android/sdk
  export PATH=$PATH:$ANDROID_HOME/emulator
  export PATH=$PATH:$ANDROID_HOME/tools
  export PATH=$PATH:$ANDROID_HOME/tools/bin
  export PATH=$PATH:$ANDROID_HOME/platform-tools

Might be helpful: Inside package.json add like this for yarn android command

"android": "source ~/.bash_profile&&react-native run-android"
Shanu Singh
  • 365
  • 3
  • 16
0

After updating your path and environment variables as recommended in the other answers, sometimes all you need do is simply restart your machine. It took me over 24 hours until I ran out of options and restarted.

Cedric Ipkiss
  • 5,662
  • 2
  • 43
  • 72
0

I too faced The same issue , I resolved it this way.

Open Terminal

cd to the folder where eclipse.app is located E.g cd /Users/Shared/eclipse/jee-2020-09

Type open Eclipse.app/

Eclipse will now open and will be able to access the system environment variables as well.

Check it using the code:

System.getenv().forEach((k, v) -> {
    System.out.println("ENV : " + k + ":" + v);
});