Robolectric: Setting ANDROID_HOME for All Tests (MacOS/Eclipse)

I just stumbled over the somewhat annoying issue that when running Robolectric tests in Eclipse, the library must see the ANDROID_HOME environment variable, or resource lookups in the android namespace will fail (you’ll see a warning about this on the console).

Now, you can of course fix that by adding ANDROID_HOME to every single Eclipse Run configuration for your tests, but that would be cumbersome. It would be better to have this variable set for Eclipse so that all Java processes forked from it will inherit it, including your test runs. Unfortunately MacOS X doesn’t seem to have a straight forward solution to that. I tried modifying Eclipse’s plist file, to define LSEnvironment, but that did not have any effect.

However, what you can do is have all MacOS processes inherit it by setting it in /etc/launchd.conf. This is somewhat quick and dirty, since you only really need it in Eclipse and Terminal, but it does the job:

$sudo vim /etc/launchd.conf

Now add this line:

setenv ANDROID_HOME /path/to/android-sdk

Reboot, and rejoice.