DEV Community

loading...

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

oscherler profile image Olivier “Ölbaum” Scherler Updated on ・3 min read

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:

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

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
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'

launchctl setenv ANDROID_AVD_HOME "$ANDROID_AVD_HOME"
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" "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>
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

Discussion (3)

pic
Editor guide
Collapse
dhineshvenkat profile image
Dhinesh

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.

Collapse
charlesc_ai profile image
CharlesC.ai

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"

Collapse
1v5 profile image
1v25

Damn I was looking for Windows method