Introduction
When working with mobile development tools like Cordova or PhoneGap, it’s crucial to have your environment variables correctly set up. This ensures that commands such as cordova platforms add android
work seamlessly without errors. One common error message you might encounter is: "Error: ANDROID_HOME is not set and ‘android’ command not in your PATH." This tutorial will guide you through setting the necessary Android environment variables on different operating systems to get your Cordova development up and running smoothly.
Understanding Environment Variables
Environment variables are dynamic values that affect the behavior of processes on a computer. For Android development, two essential environment variables are ANDROID_HOME
and updates to the system PATH
. ANDROID_HOME
points to the directory where the Android SDK is installed, while modifying the PATH
ensures that your terminal can find the required Android tools.
Why These Variables Are Important
- ANDROID_HOME: Specifies the installation path of the Android SDK. Tools and libraries rely on this variable to locate necessary files.
- PATH: By adding Android’s tool directories (e.g.,
tools
,platform-tools
) to your system’s PATH, you make it possible for commands likeadb
orandroid
to be recognized globally within your terminal sessions.
Setting Up Environment Variables
Windows
-
Find the SDK Installation Path:
- The default path is usually something like
C:\Users\<YourUsername>\AppData\Local\Android\Sdk
. You can find it from the Android Studio SDK Manager.
- The default path is usually something like
-
Set ANDROID_HOME and Update PATH:
- Open the Start Search, type in "env", and select "Edit the system environment variables".
- Click on the "Environment Variables" button.
- Under "System variables," click "New" to create a new variable named
ANDROID_HOME
, with the value set to your SDK path. - Locate the
Path
variable under "System variables" and edit it. Add two new entries:%ANDROID_HOME%\tools %ANDROID_HOME%\platform-tools
-
Verify:
- Open a new command prompt and type
echo %ANDROID_HOME%
. It should display your SDK path. - Check if
adb
is recognized by typingadb version
.
- Open a new command prompt and type
macOS
-
Find the SDK Installation Path:
- Default location:
/Users/<YourUsername>/Library/Android/sdk
. Alternatively, find it from Android Studio under "Preferences" > "Appearance & Behavior" > "System Settings" > "Android SDK".
- Default location:
-
Set Environment Variables in Terminal:
- Open Terminal and edit your shell configuration file (e.g.,
.bash_profile
,.zshrc
):nano ~/.bash_profile # or appropriate shell config file like .zshrc for zsh users
- Open Terminal and edit your shell configuration file (e.g.,
-
Add the following lines:
export ANDROID_HOME=/Users/<YourUsername>/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
-
Apply Changes:
- Source your configuration file with
source ~/.bash_profile
.
- Source your configuration file with
-
Verify:
- In the terminal, type
echo $ANDROID_HOME
and ensure it shows your SDK path. - Check that
adb
is recognized by executingadb version
.
- In the terminal, type
Linux
-
Find the SDK Installation Path:
- Commonly located at
/home/<YourUsername>/Android/Sdk
. Confirm from Android Studio’s SDK Manager.
- Commonly located at
-
Edit Shell Configuration File:
- Open Terminal and edit
.bashrc
,.profile
, or appropriate file for your shell:nano ~/.bashrc # Example for bash users
- Open Terminal and edit
-
Add Environment Variables:
export ANDROID_HOME=/home/<YourUsername>/Android/Sdk export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
-
Apply Changes:
- Run
source ~/.bashrc
to apply the changes immediately.
- Run
-
Verify:
- Use
echo $ANDROID_HOME
and check for the correct SDK path. - Verify
adb
availability by runningadb version
.
- Use
Additional Tips
- Always restart your terminal or command prompt after making these changes to ensure they take effect.
- If you face issues with recognizing Android commands, double-check paths and variable names for typos.
- For managing multiple versions of the JDK or other tools, consider using package managers like SDKMAN! on Linux.
Troubleshooting
If you encounter an error stating that a specific Android target is missing (e.g., "Please install Android target ‘android-19’"), use the Android SDK Manager to install the required platform:
- Open Android Studio.
- Navigate to "Configure" > "SDK Manager".
- Under "SDK Platforms", check and install the necessary API levels.
By following these steps, you can ensure that your development environment is correctly configured for using Cordova or other similar tools with Android projects.