Lovjit Bedi Lovjit Bedi - 1 month ago 10x
Linux Question

Error: Failed to find 'ANDROID_HOME' environment variable when building apk using ionic

I am trying to build android apk using ionic framework.
When I enter the following command sudo ionic build android , I get the following error "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. "

But when I check ANDROID_HOME variable by typing "echo $ANDROID_HOME" , I get the valid SDK path( e.g. /home/ttnd/android-sdk-linux ) .

Find below the configuration that I have included in the bashrc file ,

export ANDROID_HOME=/home/ttnd/android-sdk-linux

export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

I have checked all the possible links available on web but still I am unable to find the issue.


The problem is that the execution environment for the sudo command is different than the calling one. In other words, your environment variables don't get passed in.

You may be able to fix that by adding the -E option to the sudo command. There are some possible security concerns with using that option and your particular system may block you from using it. Here's a blurb from my version of the sudo man page:

The -E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment.

Another way around it would be to to include the environment variables as part of the sudo command. Something like this:

sudo ANDROID_HOME=$ANDROID_HOME PATH=$PATH ionic build android

A third way would be to run the sudo visudo and add the variables that you'd like to be included in the sudo run environment.