4

I'm trying to run ionic cordova build --release android, but that is getting this error.:

CordovaError: Failed to find 'ANDROID_HOME' environment variable. Try setting it manually.
Failed to find 'android' command in your 'PATH'. Try update your 'PATH' to include path to valid SDK directory.

I'm using Windows 10, but am using the linux subsystem (so am doing all the linux commands etc...).

I went in to my system variables and put in the Android SDK stuff for Windows as seen here:

enter image description here

But when I go back into bash/shell, when I do echo $ANDROID_HOME, it doesn't show the path I set. Should I instead install Android SDK for linux and have it point to that? Or is there someway I can have this ANDROID_HOME path point to my C:/ drive, as I don't think there is a C:/ drive when I"m on the linux subsystem

George Udosen
  • 906
  • 1
  • 13
  • 28
Mike K.
  • 543
  • 3
  • 14
  • 46
  • 2
    did you restart your cmd after adding environmental variable? – Suraj Rao Apr 11 '18 at 04:59
  • Yes I restarted both 'bash for ubuntu' on my Windows 10 box, and restarted Webstorm (which has bash built in). yet both times the `echo $ANDROID_HOME` was blank – Mike K. Apr 11 '18 at 13:26
  • havent worked with windows for quite a while.. https://stackoverflow.com/questions/8685699/windows-7-environment-variable-not-working-in-path any of the answers help? – Suraj Rao Apr 11 '18 at 13:29
  • Probably an issue with converting the windows env var parth (`C:\ `) to a linux path (`\mnt\c`). See [here](https://blogs.msdn.microsoft.com/commandline/2017/12/22/share-environment-vars-between-wsl-and-windows/) for more info. I would recommend either switching to the windows cmd or PowerShell or installing Java again on your WSL. – David Apr 17 '18 at 07:33
  • if you have linux sub system then please refer this link https://stackoverflow.com/questions/48178898/ionic-3-deploy-to-device-set-android-home/48181995#48181995 – Mustafa Lokhandwala Apr 17 '18 at 12:57
  • can you verify that the windows cmd prompt can pickup your changes? – user1506104 Apr 21 '18 at 04:05

2 Answers2

11

That is because the linux sub-system environment variables are separated from Windows environment variables, except for PATH which is shared by default starting from Creator Update.

In that case, you have to add ANDROID_HOME to the linux environment variable. One way to do it is to add it into ~/.bashrc

nano ~/.bashrc

add following code to the end of the file

export ANDROID_HOME="/mnt/c/Users/<user_name>/AppData/Local/Android/sdk"
export PATH="$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools"

save, exit and run source

source ~/.bashrc

Now you will be able to run android sdk tool from linux subsystem, although there is a caveat where you have to add .exe for each tool. For example:

adb --> won't work
adb.exe --> works
pradithya aria
  • 657
  • 5
  • 10
1

So I had written a big answer on this and for some reason I can't find it. I don't know if it didn't get posted or what happened. So re-writing what I written earlier

Windows and Windows Sub System Linux do not by default share any environment variables as such. But there is a way to automatically share variables between the two as explained in below article

https://blogs.msdn.microsoft.com/commandline/2017/12/22/share-environment-vars-between-wsl-and-windows/

By default your c:\ gets mapped to /mnt/c. Now you have two options

Shell Environments

You can update your ~/.bashrc or ~/.bash_profile (whichever you use) and export the variables

export ANDROID_HOME="/mnt/c/Android/sdk"
export PATH="$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools"

Shared Environment

Windows created a special environment variable named WSLENV. This environment variable can be used to share the variable between two environments and it will also auto translates paths. Before we dive in some special flags

  • /p: This flag indicates that a path should be translated between WSL paths and Win32 paths.
  • /l: This flag indicates the value is a list of paths. In WSL, it is a colon-delimited list. In Win32, it is a semicolon-delimited list.
  • /u: This flag indicates the value should only be included when invoking WSL from Win32. In the example below, we set FORWSL from cmd and it will show up in WSL.
  • /w: This flag indicates the value should only be included when invoking Win32 from WSL.

Now if you set the environment like below

WSLENV=ANDROID_HOME/p

Above says the ANDROID_HOME should be shared between WSL and Windows and the /p indicates that it should be shared automatically. This is the recommended way of doing it to keep everything in sync

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • `export WSLENV=ANDROID_HOME/p` in `.bashrc` did wonders.. had to `ls -s /mnt..` some of the key binaries tho into `/usr/bin/..` – Oussama L. Nov 24 '22 at 15:07