DEV Community

Olivier “Ölbaum” Scherler
Olivier “Ölbaum” Scherler

Posted on • Updated on

Move Android Studio Virtual Devices to Another Hard Drive on a Mac

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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:

Enter fullscreen mode Exit fullscreen mode


Enter fullscreen mode Exit fullscreen mode

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'

Enter fullscreen mode Exit fullscreen mode

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" "">
<plist version="1.0">
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (6)

th0rgall profile image
Thor Galle

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.

gaurav__ead0f62a2b9 profile image
Gaurav Sharma

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 :(

Image description

dhineshvenkat profile 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.

th0rgall profile image
Thor Galle

/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:

  1. Open TextEdit
  2. Press Command+Shift+T to switch to plain text mode
  3. Copy-paste the example, and change it as needed.
  4. Save the file with the name .android_studio_env_vars to your home folder.
charlesc_ai profile image

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"