Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variable in settings.gradle not working with Android Studio

I do have a multi-module project with a library project in a different root path. As illustration you can imagine something like this:

/projects_home/projects/app_root
   |--app/
   |   |--build.gradle
   |--build.gradle
   |--settings.gradle

/libraries_home/libraries
   |--libA
       |--build.gradle

In my settings.gradle file I am able to set the absolute path to the library project utilizing the projectDir attribute. This works just fine within the console as well as with Android Studio.

But if I try to use an environment variable it stops working with Android Studio. The settings.gradle for the example above would look like this:

include ':app'
include ':libA'

project(':libA').projectDir = new File("$System.env.LIB_ROOT", '/libraries/libA')

If I build with the graddle wrapper from the console, it still works. But AS stops working with the following error msg:

Gradle 'app' project refresh failed:
Configuration with name 'default' not found.

If I unset the environment variable, the build on console fails with the same msg:

* What went wrong:
A problem occurred configuring project ':app'.
> Configuration with name 'default' not found.

Therefore I guess that AS is somehow not be able to access the environment variables set with my ~/.bashrc

Does somebody of you maybe know a way how I can make AS aware of my environment?

like image 382
ckihm Avatar asked Jan 16 '14 22:01

ckihm


People also ask

How do I set environment variables in Gradle?

If you are using an IDE, go to run, edit configurations, gradle, select gradle task and update the environment variables. See the picture below. Alternatively, if you are executing gradle commands using terminal, just type 'export KEY=VALUE', and your job is done.

How do I see environment variables in Gradle?

Gradle can easily read environment variables which you have defined on a system level (e.g. via . pam_environment, . zshrc and similar). However you can also define environment variables just for gradle on a more granular level by means of a local.

How are environment variables used in Gradle properties?

Gradle can also set project properties when it sees specially-named system properties or environment variables. If the environment variable name looks like ORG_GRADLE_PROJECT_prop=somevalue , then Gradle will set a prop property on your project object, with the value of somevalue .


3 Answers

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.

like image 157
changyuheng Avatar answered Oct 16 '22 01:10

changyuheng


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.
like image 12
manroe Avatar answered Oct 16 '22 02:10

manroe


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.

like image 7
Scott Barta Avatar answered Oct 16 '22 02:10

Scott Barta