ckihm ckihm - 1 month ago 17
Android Question

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?

Answer

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.