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_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 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.
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
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
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