[android] Ionic android build Error - Failed to find 'ANDROID_HOME' environment variable

I am trying to build android for ionic in linux but its showing me an error like this

     [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.]
      ERROR building one of the platforms: Error: /home/kumar/myapp/platforms/android/cordova/build: Command failed with exit code 2
      You may not have the required environment or OS to build this project
      Error: /home/kumar/myapp/platforms/android/cordova/build: Command failed with exit code 2
at ChildProcess.whenDone (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:139:23)
at ChildProcess.EventEmitter.emit (events.js:98:17)
at maybeClose (child_process.js:743:16)
at Process.ChildProcess._handle.onexit (child_process.js:810:5)

and i've added this in my bashrc file

export ANT_HOME="/usr/bin/ant"
export PATH="$PATH:$ANT_HOME/bin"
export HOME="/home/kumar"
export ANDROID_HOME="$HOME/android-sdk-linux/tools"
export ANDROID_PLATFORM_TOOLS="$HOME/android-sdk-linux/platform-tools"
export PATH="$ANDROID_HOME:$ANDROID_PLATFORM_TOOLS:$PATH"

I'm thinking i've given the path but i dont know why its showing me this error .. Please help....

This question is related to android linux cordova ionic-framework ionic

The answer is


You only have to edit your profile file like this:

sudo su
vim ~/.profile

and put this at the end of the file:

export ANDROID_HOME=/home/(user name)/Android/Sdk
export PATH=$PATH:/tools
export PATH=$PATH:/platform-tools

Save and close the file and do:

cd ~
source .profile

now if you do:

echo $ANDROID_HOME

it should show you something like this:

/home/(user name)/Android/Sdk

Windows - set environment variable permanently:

setx ANDROID_HOME "C:\Program Files\Android\android-sdk"

or

setx ANDROID_HOME "C:\Program Files (x86)\Android\android-sdk"


Download the android SDK http://developer.android.com/sdk/installing/index.html

You only export the path of SDK folder.

export ANDROID_HOME="YOUR_PATH/sdk/"


For OSX

Great tutorial here: https://www.youtube.com/watch?v=kY22NSBwV_s

No need to do any bash commands/symlinks:

  1. Install Java 6 runtime: https://support.apple.com/kb/DL1572?locale=de_DE
  2. Install Java JDK: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

To add ANDROID_HOME value permanently,

gedit ~/.bashrc

and add the following lines

export ANDROID_HOME=/root/Android/Sdk
PATH=$PATH:$ANDROID_HOME/tools

Save the file and you need not update ANDROID_HOME value everytime.


Setup for Linux/Ubuntu/Mint

  1. download Android Studio or SDK only
  2. install
  3. set PATH

3.1) Open terminal and edit ~/.bashrc

sudo su
vim ~/.bashrc

3.2) Export ANDROID_HOME and add folders with binaries to your PATH

Common default install folders:

  • /root/Android/Sdk
  • ~/Android/Sdk

Example .bashrc

export ANDROID_HOME=/root/Android/Sdk
PATH=$PATH:$ANDROID_HOME/tools
PATH=$PATH:$ANDROID_HOME/platform-tools

3.3) Refresh your PATH

source ~/.bashrc

4) Install correct SDK

When ionic build android still fails it could be because of wrong sdk version. To install correct versions and images run android from command line. Since it is now in your PATH you should be able to run it from anywhere.


For OSX

  • into ~/.bash_profile add:

    export ANDROID_HOME="/path/to/android-sdk-macosx"
    export PATH="$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$PATH"
    

    and then execute it in terminal to take effect immediately;

  • into /etc/sudoers add: (you can use console: sudo visudo)

    Defaults        env_keep += "ANDROID_HOME"
    

    since the building process has to start with sudo and Node's process.env get the respective variables.


The procedure is already well explained in the above answers. But if add the ANDROID_HOME and PATH to the .bashrc or .zshrc present in /home/username/ and try to run the ionic command with sudo, you may get this error again.

The reason is, it may look for the ANDROID_HOME and PATH in the .zshrc file of root user instead of currently logged in user. So you shouldn't do that unless you add that in root user's .bashrc or .zshrc files.


In my Case: I have put the correct path for Android and java but still getting the error.

The problem was that I have added the Android platform using sudo command.sudo ionic cordova platform android.

To solve my problem: First I have removed the platform android by running command

sudo ionic cordova platform rm android

then add the android platform again with out sudoionic cordova platform add android but i get the error of permissions.

To resolve the error run command

sudo chmod -R 777 {Path-of-your-project}

in my case sudo chmod -R 777 ~/codebase/IonicProject Then run command

ionic cordova platform add android

or

ionic cordova run android


Case: using SO Windows, try:

set ANDROID_HOME=C:\\android-sdk-windows
set PATH=%PATH%;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools

more in: http://spring.io/guides/gs/android/

Case: you don't have platform-tools:

cordova platforms list
cordova platforms add <Your_platform, example: Android>

I had this problem when running sudo ionic run android as root as these environmental variables were not persisted. Fixed by running sudo -E ionic run android.


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 linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to cordova

Why am I seeing net::ERR_CLEARTEXT_NOT_PERMITTED errors after upgrading to Cordova Android 8? Xcode couldn't find any provisioning profiles matching Cordova app not displaying correctly on iPhone X (Simulator) JAVA_HOME is set to an invalid directory: ionic 2 - Error Could not find an installed version of Gradle either in Android Studio cordova Android requirements failed: "Could not find an installed version of Gradle" 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) Cordova : Requirements check failed for JDK 1.8 or greater Can't accept license agreement Android SDK Platform 24

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

Examples related to ionic

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 How to get the version of ionic framework?