Android environment variables & macOS environment variable configuration

About the author: CSDN content partner and technical expert, built an APP with tens of millions of daily users from scratch.
Focus on sharing original series of articles in various fields, specializing in java backend, mobile development, business realization, artificial intelligence, etc. I hope everyone will support me.

Directory

  • 1. Introduction
  • 2. Overview
    • macOS basics
  • 3. Set environment variables
    • 3.1 Settings in terminal window and shell script
      • Windows
      • macOS and Linux
        • Update shell init script
        • Update the PATH environment variable
    • 3.2 Commonly used variables
  • 4. Recommended reading

1. Introduction

We continue to summarize and learn Basic knowledge of Android, review the past and learn the new.

Today we talk about the configuration of Android environment variables, which is relatively simple.

2. Overview

Many tools will read the ANDROID_HOME variable to determine the Android SDK installation directory. We can configure the behavior of Android Studio and command line tools by setting environment variables.
Set the command search path environment variable to include ANDROID_HOME/tools, ANDROID_HOME/tools/bin, and ANDROID_HOME/platform-tools.

macOS basic knowledge

  • View all shells in the current system in macos
cat /etc/shells
  • Of course, in macos, we can also choose different shells as follows:
1. Select Apple menu ? > "System Settings (Preferences)", then click "Users & Groups".
2. In the user list on the left, hold down the Control key and click your username, then select "Advanced Options"
3. Choose a shell from the Login Shell menu, then click OK to save the changes.

Or use the command to modify

Modify the system default shell to zsh
chsh -s /bin/zsh

Environment variables under Mac system

  • Environment variables under Mac system-bash
  1. /etc/profile: Full drama configuration, which will be executed once when all users log in.
  2. /etc/paths: similar to /etc/profile
  3. ~/.bash_profile: Single-user personal configuration, executed once when the user logs in, and .bashrc is executed for the environment variables set by the user. Like /etc/profile, it also needs to be restarted to take effect. The difference is that /etc/profile is for all users; ~/.bash_profile is for the current single user
  4. ~/.bash_login: Execute this file every time you log in to the system (exit the bash shell)
  5. ~/.profile: For individuals, what is read is the personal configuration file. A series of operations. “If the user is logged in, read the .bash_profile file. If .bash_profile does not exist, read .bash_login. If the first two do not exist, finally Just read ~/.profile”
  6. ~/.bashrc: A shell file exclusive to the current user. It will be read when logging in or opening a shell window, so there is no need to restart the shell window.
  • Environment variables under Mac system-zsh
  1. /.zprofile: similar to /.bash_profile, runs when logging in, and allows SSH
  2. ~/.zshrc: similar to ~/.bashrc, for each “terminal” shell window

We can directly view these files in the system folder. These are hidden files and can be displayed by pressing the shortcut key:

command + shift + .

If you find that there are no two files circled in red above, you can create them directly in the ~ directory.

Open the configuration file, then modify, save and exit (press the ESC key, then enter :wq and press Enter). Environment variables need to be separated by colons (:).

cd
vi.zshrc

export PATh=$PATH: The path of the software environment to be configured


Press esc key
Input: wq
Press enter key

After making the changes, directly source .zshrc to make the file take effect, or you can reopen a command line window

The difference between ${PATH} and $PATH

Window’s is relatively simple and easy to remember, so I won’t write it down.

3. Set environment variables

3.1 Settings in terminal window and shell script

The variable settings in the terminal window are only effective when the window is open. Please note this. Below we list the commonly used terminal setting commands.

Windows

grammar

set VARIABLE_NAME=<new-value>

For example:

set HTTP_PROXY=myserver:1981

macOS and Linux

The exact method of setting environment variables depends on the shell used, so we first need to know which shell we are using. This can be seen through the command. Enter the following command

echo $0

The following is the setting syntax for different shells

  • Shells such as Gnu Bash or Zsh
export VARIABLE_NAME=<new-value>
  • Other shells (such as TCSH)
setenv VARIABLE_NAME <new-value>
Update shell initialization script

To avoid setting it every time, we can add these commands to the shell init script to set the variables every time we run a new shell instance
The location of the shell initialization script depends on the shell being used.
For Gnu Bash, the script location is generally ~/.bash_profile.
Zsh, the script location is generally ~/.zprofile.
TCSH, the script location is generally ~/.cshrc

Update PATH environment variable

We can also update the PATH environment variable to include the tool location

  • Shells such as Gnu Bash or Zsh
export ANDROID_HOME= ~/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools
  • Other shells (such as TCSH)
setenv ANDROID_HOME=~/Library/Android/sdk
setenv PATH=$PATH\:$ANDROID_HOME/tools\:$ANDROID_HOME/tools/bin\:$ANDROID_HOME/platform-tools

3.2 Commonly used variables

  • ANDROID_HOME
    Set the path to the SDK installation directory. Once set, the value typically does not change and can be shared by multiple users on the same computer. ANDROID_SDK_ROOT also points to the SDK installation directory, but is deprecated.
    If you continue to use it, Android Studio and the Android Gradle plugin will check that the old and new variables are consistent.

  • ANDROID_USER_HOME
    Sets the path to the user preference directory for tools included in the Android SDK. Defaults to $HOME/.android/.

  • STUDIO_JDK
    Sets the location of the JDK where Android Studio runs. When you start Android Studio, it checks the STUDIO_JDK, JDK_HOME, and JAVA_HOME environment variables.

  • ANDROID_AVD_HOME
    Sets the path to the directory containing all AVD specific files, most of which contain very large disk images. The default location is $ANDROID_EMULATOR_HOME/avd/. If there is insufficient disk space in the default location, you may need to specify a new location.

  • HTTP_PROXY
    Contains HTTP/HTTPS proxy settings for the global HTTP proxy. Use a colon ( separator) between the host and port. For example, set HTTP_PROXY=myserver:1981.

This is equivalent to specifying the -http-proxy proxy parameter from the command line when running the emulator.

Wait, there are some others, which may not be used much, so they are not listed.

4. Recommended reading

Java column

SQL Column

Data Structures and Algorithms

Android learning column

ddd