Imagine you’d like to reclaim space on your Mac’s main hard drive by moving your Android Studio Virtual Devices (the phones and tablets you run in the Android Emulator to test your applications). It’s possible, but if you’re lucky like I am, you might run into some hurdles.
It’s easy enough to find out that you should modify either the ANDROID_SDK_ROOT
, ANDROID_EMULATOR_HOME
, or ANDROID_AVD_HOME
environment variables (or even ANDROID_HOME
, if you’re unlucky enough to find some really outdated documents). But if you try to set them under Appearance & Behavior > Path Variables in the Android Studio Preferences, because you think it would be a reasonable thing to do, then you might waste a lot of time, because it does not work. And of course, the thousands of documents about how to do it in Windows are of no help at all, and the few that are about the Mac are really, really outdated.
What we want to achieve here is to define the ANDROID_AVD_HOME
environment variable in a way that makes it available to applications launched from the Finder. Starting from Mac OS 10.10 (and working at least until 10.14, at the time of this writing), it can be done with launchctl setenv
:
launchctl setenv ANDROID_AVD_HOME /Volumes/LargeHDD/Android/avd
To test it without starting Android Studio, you can use Apple’s Script Editor and run this script (however, keep in mind that you must relaunch Script Editor after calling launchctl setenv
, in order for it to pick up the changes):
do shell script "echo $ANDROID_AVD_HOME"
Now is a good time to move your Android Virtual Device (AVD) files from ~/.android/avd
to their new location (e.g. /Volumes/LargeHDD/Android/avd
).
However, you also need to update the .ini
files in that folder, because they reference an absolute path to the old location, and if you don’t update it, Android Studio will complain that the INI file is corrupted. For example if you have a file Nexus_5X_API_26.ini
in your new /Volumes/LargeHDD/Android/avd
folder, edit it (the path
property) from:
avd.ini.encoding=UTF-8
path=/Users/olivier/.android/avd/Nexus_5X_API_26.avd
path.rel=avd/Nexus_5X_API_26.avd
target=android-26
to
avd.ini.encoding=UTF-8
path=/Volumes/LargeHDD/Android/avd/Nexus_5X_API_26.avd
path.rel=avd/Nexus_5X_API_26.avd
target=android-26
You can now launch Android Studio and open the AVD Manager to see that your devices are still there, but we still need to make sure this environment variable is set every time you log in. For that, we are going to create a script, and start it at login using Launchd.
Create the following script at /Users/[YOUR_USER_NAME]/.android_studio_env_vars
:
export ANDROID_AVD_HOME='/Volumes/LargeHDD/Android/avd'
launchctl setenv ANDROID_AVD_HOME "$ANDROID_AVD_HOME"
Then create the following PList file at ~/Library/LaunchAgents/user.AndroidStudioEnvVars.plist
(you can name it however you like):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>user.AndroidStudioEnvVars</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/Users/[YOUR_USER_NAME]/.android_studio_env_vars</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
We could just execute launchctl setenv ANDROID_AVD_HOME /Volumes/LargeHDD/Android/avd
from the PList, but a script allows us to define other variables, like ANDROID_SDK_ROOT
if needed.
Finally, load the PList with:
launchctl load ~/Library/LaunchAgents/user.AndroidStudioEnvVars.plist
The next time you log in or restart, the environment variable should be automatically set, which you can check with:
launchctl getenv ANDROID_SDK_ROOT
# -> /Volumes/LargeHDD/Android/sdk
Top comments (6)
Wow, thanks! A macOS quirk I hadn't expected. This is something Android Studio could have worked around, if it had just loaded
ANDROID_AVD_HOME
from my default shell's~/.zshrc
config. It seems like VSCode with the Flutter extension does this, and that works much more intuitively.Thank you for your post.
I am able to change AVD folder location. After restarting Androidstudio new .ini and device folder was created. But AVD device is not loading ,refer attached image :(
Yo!! Can you make a video demonstrating this. I don't understand this "/Users/[YOUR_USER_NAME]/.android_studio_env_vars". I can't create a folder like this.
/Users/[YOUR_USER_NAME]
is the macOS home directory of your currently logged in user. You don't need to create it, it already exists!You need to create the hidden text file
.android_studio_env_vars
within your home directory. You can find your home folder with a bunch of tutorials on the internet (like this one). To create the text file, you could:.android_studio_env_vars
to your home folder.Thank you for the great finding, saved me hours on search on the Internet.
It is PITA to deal with Android Studio env variables
Never thought of the "launchctl"
export ANDROID_AVD_HOME='/Volumes/LargeHDD/Android/avd'
launchctl setenv ANDROID_AVD_HOME "$ANDROID_AVD_HOME"
That worked for me.
Also I had to manually change all ini files.