346

Could anybody post a working solution for setting ANDROID_HOME via the terminal?

My path to the Android-SDK is /Applications/ADT/sdk.

1
  • Setup ANDROID_HOME, JAVA_HOME in MAC
    – Samiran
    Dec 3, 2019 at 8:51

14 Answers 14

644

Where the Android-SDK is installed depends on how you installed it.

  1. If you downloaded the SDK through their website and then dragged/dropped the Application to your Applications folder, it's most likely here:

    /Applications/ADT/sdk (as it is in your case).

  2. If you installed the SDK using Homebrew (brew cask install android-sdk), then it's located here:

    /usr/local/Caskroom/android-sdk/{YOUR_SDK_VERSION_NUMBER}

  3. If the SDK was installed automatically as part of Android Studio then it's located here:

    /Users/{YOUR_USER_NAME}/Library/Android/sdk

Once you know the location, open a terminal window and enter the following (changing out the path to the SDK to be however you installed it):

export ANDROID_HOME={YOUR_PATH}

Once you have this set, you need to add this to the PATH environment variable:

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

Lastly apply these changes by re-sourcing .bash_profile:

source ~/.bash_profile

  1. Type - echo $ANDROID_HOME to check if the home is set.

echo $ANDROID_HOME

13
  • I have this and still the same error. export HOME="/Users/rover" export ANDROID_HOME="$HOME/Documents/Dev/Android/adt-bundle-mac-x86_64-20140702/sdk" export ANDROID_PLATFORM_TOOLS="$ANDROID_HOME/platform-tools" export PATH="$ANDROID_HOME:$ANDROID_PLATFORM_TOOLS:$ANDROID_HOME/build-tools:$PATH" export ANT_HOME="/usr/local/bin/ant" #export PATH="$PATH:$ANT_HOME/bin" $ which ant /usr/local/bin/ant $ ls /usr/local/bin/ant /usr/local/bin/ant $ ant -v Apache Ant(TM) version 1.9.4 $ cordova --version 4.0.0 error is: node_modules/q/q.js:126 throw e;
    – mylord
    Nov 6, 2014 at 15:14
  • see @kip2's updated answer below ... (uses tools not bin)
    – echo
    Jul 27, 2015 at 13:35
  • 4
    hey it keeps restarting everytime... how can I add it permanently on ubuntu? Sep 17, 2015 at 14:31
  • 7
    If using brew you have a generic link to android home if you set ANDROID_HOME=/usr/local/opt/android-sdk without using explicity version number in the path Dec 21, 2015 at 14:31
  • 1
    @user1735921 You can add the lines you want to run every time you start a new shell to your shell's config file. With the bash shell that would be e.g. ~/.bashrc . Jan 21, 2016 at 12:56
204

In Terminal:

nano ~/.bash_profile 

Add lines:

export ANDROID_HOME=/YOUR_PATH_TO/android-sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH

Check it worked:

source ~/.bash_profile
echo $ANDROID_HOME
3
  • 2
    Also for me. BTW, the 2nd and 3rd lines could be joined in export PATH=$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$PATH
    – Jose_GD
    Oct 6, 2015 at 22:52
  • For some reason when I run echo $PATH, it doesn't output the latest dirs I saved in .bash_profile. Nov 29, 2015 at 16:59
  • 1
    @Maid786 It's in the home folder, but it is invisible. You can show invisibles in Finder as discussed here macworld.co.uk/how-to/mac-software/… or use a text editor like TextWrangler that has an option to show invisible files in the file open dialog box. Mar 8, 2016 at 9:49
53

Adding the following to my .bash_profile worked for me:

export ANDROID_HOME=/Users/$USER/Library/Android/sdk
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
1
  • 5
    The important point to note that the installation path is /Users/yourname/Library/Android/sdk Apr 13, 2015 at 21:47
38

I am having MAC OS X(Sierra) 10.12.2.

I set ANDROID_HOME to work on React Native(for Android apps) by following the following steps.

  • Open Terminal (press Command+SpaceBar, type Terminal, Hit ENTER).

  • Add the following 3 lines to ~/.bash_profile.

      export ANDROID_HOME=$HOME/Library/Android/sdk/
      export PATH=$PATH:$ANDROID_HOME/tools
      export PATH=$PATH:$ANDROID_HOME/platform-tools
    
  • Finally execute the below command (or RESTART the system to reflect the changes made).

    source ~/.bash_profile

That's it.

6
  • 2
    @SteveLuck you can create it if it doesn't exist yet.
    – Jeff LaFay
    Sep 22, 2017 at 14:46
  • 1
    @SteveLuck If you don't find then just create it using touch ~/.bash_profile then finally add your content here. You can also create it directly using vi/vim ~/.bash_profile then paste the lines. Most of the times you will only find ~/.profile, you can add here too.
    – hygull
    Sep 23, 2017 at 5:07
  • 3
    @SteveLuck ~ is a shortcut for your home folder. If you do not know where is your home folder, type cd on the terminal. Typing cd will take you to your home folder. then you can do vi .bash_profile or nano .bash_profile to open/edit the file.
    – Mav55
    Nov 15, 2017 at 17:32
  • Thank you for the update of what you tried and got. It supports the answer.
    – hygull
    Apr 23, 2019 at 18:07
  • 1
    What is Command+R supposed to do? Which app do you expect to be in? In Finder that shortcut maps to "Show Original" for an alias? Did you mean Command+Space?
    – uliwitness
    Jan 13, 2021 at 13:46
36

quoting @user2993582's answer

export PATH=$PATH:$ANDROID_HOME/bin

The 'bin' part has changed and it should be

export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
26

I'm using React Native with Catalina mac os and zsh shell

1- touch ~/.zshrc

2- open ~/.zshrc

3- according to React Native android setup copy and past

export ANDROID_HOME=$HOME/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

to the opened text file then save and close the file.

4- run source ~/.zshrc and make sure to restart your terminal.

5- run adb you will get something like

Android Debug Bridge version 1.0.41 Version 30.0.0-6374843

thanks for this documented

update1 16/2/2021

this solution works with Big Sur as well.

3
  • 1
    This is the correct answer for macOS Big Sur as well. Catalina and later uses zsh instead of bash for new accounts.
    – uliwitness
    Jan 13, 2021 at 13:47
  • @uliwitness thank you, right I update the answer for Big Sur as well. Feb 16, 2021 at 14:20
  • Thanks. this solution works with Monterey too Mar 15, 2022 at 10:00
12

Could anybody post a working solution for doing this in the terminal?

ANDROID_HOME is usually a directory like .android. Its where things like the Debug Key will be stored.

export ANDROID_HOME=~/.android 

You can automate it for your login. Just add it to your .bash_profile (below is from my OS X 10.8.5 machine):

$ cat ~/.bash_profile
# MacPorts Installer addition on 2012-07-19 at 20:21:05
export PATH=/opt/local/bin:/opt/local/sbin:$PATH

# Android
export ANDROID_NDK_ROOT=/opt/android-ndk-r9
export ANDROID_SDK_ROOT=/opt/android-sdk
export JAVA_HOME=`/usr/libexec/java_home`
export ANDROID_HOME=~/.android

export PATH="$ANDROID_SDK_ROOT/tools/":"$ANDROID_SDK_ROOT/platform-tools/":"$PATH"

According to David Turner on the NDK Mailing List, both ANDROID_NDK_ROOT and ANDROID_SDK_ROOT need to be set because other tools depend on those values (see Recommended NDK Directory?).

After modifying ~/.bash_profile, then perform the following (or logoff and back on):

source ~/.bash_profile
1
11

To set ANDROID_HOME, variable, you need to know how you installed android dev setup.

If you don't know you can check if the following paths exist in your machine. Add the following to .bashrc, .zshrc, or .profile depending on what you use

If you installed with homebrew,

export ANDROID_HOME=/usr/local/opt/android-sdk

Check if this path exists:

If you installed android studio following the website,

export ANDROID_HOME=~/Library/Android/sdk

Finally add it to path:

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

If you're too lazy to open an editor do this:

echo "export ANDROID_HOME=~/Library/Android/sdk" >> ~/.bashrc
echo "export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools" >> ~/.bashrc
6

The ANDROID_HOME environment is the same as the ANDROID_SDK_ROOT environment, this means it defines the path to the SDK installation directory.

Click here to about more.

I set up the Android SDK separate from android studio, it gives me more control of where things are.

cd $HOME/Downloads
curl https://dl.google.com/android/repository/commandlinetools-mac-7302050_latest.zip --output android-sdk.zip
  • Second, I unzip the file in the Downloads directory, this process will generate a directory called cmdline-tools.
unzip android-sdk.zip
  • Third, I create a directory called android in the local directory.

    (/usr/local system-wide, read-only files installed by the local administrator, usually you)

sudo mkdir /usr/local/android
  • Fourth, I move the directory generated by decompression to the android directory renaming it to sdk. (In the future I will add the ndk directory beside the sdk)
sudo mv cmdline-tools /usr/local/android/sdk
  • Fifth, I manually add the environment variables to the .zshrc file in my personal directory.
nano $HOME/.zshrc
# ...

# Set environment variables for Android SDK
export ANDROID_SDK_ROOT=/usr/local/android/sdk
export ANDROID_HOME=$ANDROID_SDK_ROOT

# Insert executable file paths in PATH environment variable
export PATH=$PATH:$ANDROID_HOME/bin
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
2

A lot of correct answers here. However, one item is missing and I wasn't able to run the emulator from the command line without it.

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$PATH:$JAVA_HOME/bin

export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator       # can't run emulator without it
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools

So it's a compilation of the answers above plus a solution for this problem.

And if you use zsh (instead of bash) the file to edit is ~/.zshrc.

1

Setup ANDROID_HOME , JAVA_HOME enviromental variable on Mac OS X

Add In .bash_profile file

export JAVA_HOME=$(/usr/libexec/java_home)

export ANDROID_HOME=/Users/$USER/Library/Android/sdk

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

For Test

echo $ANDROID_HOME
echo $JAVA_HOME
1

Paying it forward for the next lazy / time-crunched person.

I wanted to be lazy and find a Bash script here; there wasn't. So I decided it was time to learn and now here it is.

#! /bin/bash

# This script will download the Android command line toools to a path of your choosing.
#
# Syntax  : <cmd> <zip> <path>
#
# Example : run with ./get-cli.sh commandlinetools-mac-8512546_latest.zip /usr/local/android
#
# For the latest .zip matching your system visi:
#
#   https://developer.android.com/studio#command-tools 
#
# Note: supplying no args will default to the example
# 
# Respectfully,
#   -Tricknology
#

# command-line-tools zip
cmd_line_tools=$1

# directory to add it to 
android_home_dir=$2

# use defaults if none specified
if [[ cmd_line_tools=="" ]]; then
    #default to aarch64
    cmd_line_tools='commandlinetools-mac-8512546_latest.zip'
fi

if [[ android_home_dir=="" ]]; then
    #statements
    android_home_dir='/usr/local/android'
fi

url=https://dl.google.com/android/repository/"$cmd_line_tools"

###############  gist #########################
# create dir /usr/local/android
# sudo mkdir /usr/local/android

## move tools to usr/local/
# sudo mv cmdline-tools /usr/local/android/sdk

## Set environment variables for Android SDK
# export ANDROID_SDK_ROOT=/usr/local/android/sdk
# export ANDROID_HOME=$ANDROID_SDK_ROOT

## Insert executable file paths in PATH
# export PATH=$PATH:$ANDROID_HOME/bin
# export PATH=$PATH:$ANDROID_HOME/tools
# export PATH=$PATH:$ANDROID_HOME/tools/bin
# export PATH=$PATH:$ANDROID_HOME/platform-tools
################ end gist #######################

# Create directory 
# going to ask for password to create this
echo
echo "-----------------------------------------------------"
echo creating directories
echo "-----------------------------------------------------"
echo
    [[ -f "$cmd_line_tools" ]] && { 
        echo creatig dir:  $android_home_dir; 
            sudo mkdir $android_home_dir;
        } || echo $android_home_dir exists;
echo
echo "-----------------------------------------------------"
echo downloading from: $url
echo               to: $android_home_dir
echo "-----------------------------------------------------"
     curl $url --output $cmd_line_tools
echo 
echo done
echo unzipping       : $cmd_line_tools
    unzip $cmd_line_tools
echo done
echo 
echo moving to       : "$android_home_dir"/sdk

    [ -d "$android_home_dir"/sdk ] &&
    {   echo "$android_home_dir"/sdk already exists!;
        echo cleaning up : $cmd_line_tools;
            sudo rm $cmd_line_tools;
        echo cleaning up : cmdline-tools;
            sudo rm -rf "cmdline-tools";
        echo exiting!;
        exit -1;
    };
    sudo mv cmdline-tools "$android_home_dir"/sdk
echo
echo done
echo "-----------------------------------------------------"
echo " Setting Environment Variables (Global)"
echo "-----------------------------------------------------"
echo
    echo setting ANDROID_SDK_ROOT=$android_home_dir/sdk
        export ANDROID_SDK_ROOT=$android_home_dir/sdk
        echo $(export ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT)
    echo setting ANDROID_HOME=$ANDROID_SDK_ROOT
        export ANDROID_HOME=$ANDROID_SDK_ROOT
        echo $(export ANDROID_HOME=$ANDROID_HOME)
echo
echo "-----------------------------------------------------"
echo current path: $PATH
echo "-----------------------------------------------------"
echo
    ah_bin=$ANDROID_HOME/bin
    ah_tools=$ANDROID_HOME/tools
    ah_tools_bin=$ANDROID_HOME/tools/bin
    ah_plat_tools=$ANDROID_HOME/platform-tools
    [ $(grep -q "$PATH" $ah_bin) ]         && echo $ah_bin exists          || export PATH=$PATH:$ah_bin
echo $PATH
echo
    [ $(grep -q "$PATH" $ah_tools) ]       && echo $ah_tools exists        || export PATH=$PATH:$ah_tools
echo $PATH
echo
    [ $(grep -q "$PATH" $ah_tools_bin) ]   && echo $ah_tools_bin exists    || export PATH=$PATH:$ah_tools_bin
echo $PATH
echo
    [ $(grep -q "$PATH" $ah_plat_tools) ]  && echo $ah_plat_tools exists   || export PATH=$PATH:$ah_plat_tools
echo $PATH
echo
echo "-----------------------------------------------------"
echo setting path to  : $PATH
echo "-----------------------------------------------------"
echo
     export PATH=$PATH
echo done
echo 
echo "-----------------------------------------------------"
echo cleaning up 
echo "-----------------------------------------------------"
echo
echo removing $cmd_line_tools
     sudo rm $cmd_line_tools
echo done
echo
echo removing "cmdline-tools"
     sudo rm -rf "cmdline-tools"
echo done
echo
echo "-----------------------------------------------------"
echo
echo finished!
echo let ne show you the PATH $(echo $PATH) 
3cho
3cho congratulations on your new ANDROID_HOME=$(echo $ANDROID_HOME)
exit 0
EOF
0

People, note that if you will use ~/.bash_profile then it will edit not your user's bash profile, but global. Instead go to your users directory (/Users/username) and edit it directly:

vim .bash_profile

And insert following two lines with respect to your Username and SDK directory

export PATH=$PATH:/Users/<username>/Library/Android/sdk/tools
export PATH=$PATH:/Users/<username>/Library/Android/sdk/platform-tools
3
  • 2
    You are wrong. A file path starting with ~/ means that it is a file in the user's home directory.
    – hmijail
    Jul 30, 2017 at 19:13
  • ~ means current user home. So technically this answer is wrong.
    – Alexus
    Nov 27, 2018 at 18:35
  • Just erase this answer. For proof, go to any directory on your machine, then type cd ~; ls and see what that prints.
    – ajacian81
    Dec 27, 2018 at 5:09
0

1) Open base profile :

open  ~/.bash_profile

2) Add below line in base profile :

export PATH=${PATH}:/Users/<username>/Library/Android/sdk/build-tools/27.0.3

Save and close base profile.

For me 27.0.3 working great.

Not the answer you're looking for? Browse other questions tagged or ask your own question.