在 mac 中设置 ANDROID_HOME 环境变量

我是使用 Salesforce SDK 开发本地应用程序的新手。我尝试使用 forcedroid 工具从命令行创建 ANDROID 项目,但是在设置名为 ANDROID _ HOME 的环境变量时遇到了问题。

但我不知道如何设置这个变量。

我附上截图,以正确描述我的问题。

enter image description here

223222 次浏览

Open the terminal and type :

export ANDROID_HOME=/Applications/ADT/sdk

Add this to the PATH environment variable

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

If the terminal doesn't locate the added path(s) from the .zshrc, please run this command

source ~/.zshrc

Hope it works to you!

To make it permanent on your system and the variable keep working after close the terminal, ou after a restart use:

nano ~/.bash_profile

Add lines:

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

Reopen terminal and check if it worked:

source ~/.bash_profile
echo $ANDROID_HOME

The above answer is correct. Works really well. There is also quick way to do this.

  1. Open command prompt
  2. Type - echo export "ANDROID_HOME=/Users/yourName/Library/Android/sdk" >> ~/.bash_profile

    Thats's it.

  3. Close your terminal.

  4. Open it again.

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

source ~/.bash_profile
export ANDROID_HOME=/YOUR_PATH_TO/android-sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH

Firstly, get the Android SDK location in Android Studio : Android Studio -> Preferences -> Appearance & Behaviour -> System Settings -> Android SDK -> Android SDK Location

Then execute the following commands in terminal

export ANDROID_HOME=Paste here your SDK location

export PATH=$PATH:$ANDROID_HOME/bin

It is done.

As a noob I was struggling a lot with setting up the variable.I was creating copies of .bash_profile files, the text in file was not saving etc ..😳

So I documented the the steps that worked for me. It is simple and foolproof( but little lengthy ) way to do it ✌🏼

Step1. Go to Finder >Go(on top) , click on users, then your user account you will see something like this :

{https://i.stack.imgur.com/8e9qX.png}

Step2. Now hold down ⌘ + ⇧ + . (dot) , this will now display hidden files and folders. It will look something like this:

{https://i.stack.imgur.com/apOoO.png}

PS: If the ⌘ + ⇧ +. does not work, please look up the keyboard shortcut relevant for your Mac Operating system name

Step3.

Scenario A :If .bash_profile already exists

Step3.A.1 :Double click the .bash_profile. It should open up with TextEdit ( or alternatively right click >open with >TextEdit)

Step3.A.2 : Paste the variable text in the .bash_profile file using ⌘ + V

Step3.A.3 :Save the .bash_profile file using ⌘ + S

Scenario B :If .bash_profile does NOT exist This is kind silly way of doing it , but it worked perfectly for noob like me

Step3.B.1 : Download free BBEdit text editor which is pretty light weight. Whats special about this editor is that it lets you save file that starts with ". "

Step3.B.2 : Create a new file

Step3.B.3 : Save the file in your account folder . A warning will pop up , which looks something like this:

{https://i.stack.imgur.com/KLZmL.png}

Click Use"." button. Then the blank .bash_profile file will be saved

Step3.B.4 : Paste the variable text in the .bash_profile file using ⌘ + V

Step3.B.5 :Save the .bash_profile file using ⌘ + S

Step 4: Last and final step is to check if the above steps worked. Open the bash and type echo $ANDROID_HOME

Your ANDROID_HOME variable should be now set.🤜🏻

MacOS

add this string in file ~/.bashrc or ~/.zshrc

export ANDROID_HOME="/Users/<userlogin>/Library/Android/sdk"

Here are the steps:

  1. Open Terminal
  2. Type touch .bash_profile and press Enter
  3. Now Type open .bash_profile and again press Enter
  4. A textEdit file will be opened
  5. Now type export ANDROID_HOME="Users/Your_User_Name/Library/Android/sdk"
  6. Then in next line type export PATH="${PATH}:/$ANDROID_HOME/platform-tools:/$ANDROID_HOME/tools:/$ANDROID_HOME/tools/bin"
  7. Now save this .bash_profile file
  8. Close the Terminal

To verify if Path is set successfully open terminal again and type adb if adb version and other details are displayed that means path is set properly.

try with this after add ANDROID_HOME on your Environment Variable, work well on my mac

flutter config --android-sdk ANDROID_HOME

The only solution worked for me was @Tiago Gouvêa answer, after adding it android and emulator commands worked fine, but sdkmanager command was not working so I have added one extra line to his solution:

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
export PATH=$ANDROID_HOME/tools/bin:$PATH

Open Terminal

nano ~/.bash_profile




export ANDROID_HOME=/Users/qss/Library/Android/sdk
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH


Control+S to save
Control+X to exit
Y to save changes
Update changes in terminal
source ~/.bash_profile


Validate Path:
echo $PATH


Confirm if all okay:
adb devices

solved my problem on mac 10.14 brew install android-sdk

Set the path of ANDROID_HOME > You have to open terminal and enter the below cmd.

touch ~/.bash_profile; open ~/.bash_profile

After that paste below paths in base profile file and save it

export ANDROID_HOME=/Users/<username>/Library/Android/sdk
export PATH="$JAVA_HOME/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH"

If you are setting up the ANDROID_HOME environment variable on macOS Catalina, .bash_profile is no longer Apple's default shell, and it won't persist your path variables. Use .zprofile instead and follow the environment setup instructions in react-native documentation or others. .bash_profile will keep creating new file which won't make the path permanent or persist on closing the terminal on your system path.

To create a new path just do this in macOS Big Sur:

  1. sudo touch ~/.zshrc

  2. sudo nano ~/.zshrc

  3. export all the path

  4. crtl + x and save

  5. source ~/.zshrc

  6. Check with echo $PATH

If some one is still finding difficulties, i have made a video on this

https://www.youtube.com/watch?v=tbLAHKhjjI4

Because the new version of Apple does not support bash shell so i have explained in details how do we set variables in 2020.

If you try to run "adb devices" OR any other command and it says something like

zsh: command not found adb

It tells that you are using zsh shell and /.bash_profile won't work as it should. You will have to execute bash_profile everytime with source ~/.bash_profile command when you open terminal, and it isn't permanent.

To fix this run

nano ~/.zshrc

Terminal image

and then paste following commands at the end of the file

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

NOTE: You can find Android Home url from Android Studio > Preferences System Settings > Android SDK > Android SDK Location textbox

To save it, hit Ctrl + X, type Y to save and then enter to keep the file name as it is.

Restart the terminal and try your commands again.

Based on Abraham Mayowa Oluwasina answer provided above is the correct one. MacOS Catalina and later, .bash_profile is no longer apple's default shell and it won't persist in your path variables instead use .zprofile instead.

In root directory,

touch .zprofile
open -e .zprofile

Add the following to file, Android:

export ANDROID_HOME=/Users/<username>/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/platform-tools

Java:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.15.jdk/Contents/Home
export PATH=$PATH:$JAVA_HOME/bin

Save and exit

To activate

source ~/. zprofile

echo $ANDROID_HOME

For mac OS monterey 12.0.1

Default terminal is .zsh

Step 1: open terminal then type this command.

nano ~/.zsh

Step 2: copy below command and past it.

export ANDROID_HOME=/{your sdk path}/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
export PATH=$ANDROID_HOME/emulator:$PATH`

Step3: Press command ^X then press Y then press enter

Stpe4: Close terminal and reopen it Type below command:

echo $ANDROID_HOME

you can find your path, That's it !