Environment variable in settings.gradle not working with Android Studio

On Macs, Android Studio does not read environment variables for use in Gradle apparently. I believe this is the cause for confusion in the answers here - maybe it does on Windows.

In order to get Android Studio to read environment variables, I run the application from the command line:

> /Applications/Android\ Studio.app/Contents/MacOS/studio 

The other answers here offer solutions other than using environment variables. For my situation, I'm using a library I didn't write that requires the use of an environment variable, and I'd rather not edit their code so it's easier to update later.

EDIT: And, I have a dock icon to launch Android Studio this way:

  • OSX: Add Dock icon for dedicated Terminal command explains how.

Despite the answer from Scott Barta is correct, I realized there is a way to solve my problem and wan't to share this in case somebody else has the same requirement.

I am now using the gradle.properties file do define and use gradle properties instead of system properties. The documentation of this feature can be fined in the user guide

The solution to my original question now looks like this:

$USER_HOME/.gradle/gradle.properties:

LIB_ROOT=/libraries_home

The settings.gradle file has to be modified to use the gradle property instead of the system property:

include ':app'
include ':libA'

project(':libA').projectDir = new File(LIB_ROOT, '/libraries/libA')

This works fine for me, headless as well as with AS.

Some more words regarding the fact that I am working with modules which are not placed underneath one project root. Till now it looks like AS is not complaining about this. But I just started working with this structure and it may be that I will run into problems later. What I like about this is the more flat representation in AS which is more like I am used to have it with Eclipse.

What is also described in the user guide, is to set system properties with the gradle.properties file. I tried this also, but I did run into the same problems with AS using environment variables.


Android Studio does read the environment variables. You can prove it by launching Android Studio from the shell in which those env. variables being specified instead of from X-window dash board.

The reason you did not have those variables is the X-window environment you were using did not read $HOME/.bashrc which contained those variables. This makes sense because bashrc is for Bash not X.

Assuming you are using GNOME or Unity, to launch Android Studio with those environment variables being specified, just modify the .desktop file of Android Studio (e.g. ~/.local/share/applications/android-studio.desktop):

Find this line:

Exec="/home/username/tools/android/android-studio/bin/studio.sh" %f

Change it to:

Exec=env LIB_ROOT=/libraries_home "/home/username/tools/android/android-studio/bin/studio.sh" %f

Note:

This modification just prepend env LIB_ROOT=/libraries_home to the original command. You must replace username with your own user name.

Update

If you have any questions, please leave a comment instead of editing the answer directly.


Android Studio doesn't read environment variables, so this approach won't work. Also, using the projectDir scheme in settings.gradle will probably cause problems. Android Studio has a limitation that all of its modules need to be located underneath the project root. If you have libraries that are used in multiple projects and they can't be placed under a single project root, the best advice is to have them publish JARs or AARs to a local Maven repository that individual projects can pick up.