Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to environment variables from Android Studio gradle build

In my Android Gradle build I need to get access to environment variables I set from .bash.profile. It works fine when I build from command line - Gradle script can see all the variables.

However, when I try to run my build from Android Studio - I don't have my environment variables anymore.

Here is a rough picture of what I'm facing:

1) Set custom environment variable via ~/.bash.profile:

export MY_CUSTOM_VAR='Hello World'

2) In build.gradle create task which prints this environment variable:

task printVar << {
    println System.getenv("MY_CUSTOM_VAR")
}

3) execute printVar from command line. Output is correct - env variable is set:

output: Hello World

4) execute printVar from Android Studio. Environment variable is not set. Output is empty

Common sense tells me that by doing export MY_CUSTOM_VAR='Hello World' I just make this variable available to shell process (and its subprocesses). And it would probable work if I launch my Android Studio from command line (so it would inherit my environment). But since I launch Android Studio from dock (i'm on Mac by the way) - it has its own environment which doesn't have any idea about my ~/.bash.profile.

Is there any way I can populate custom environment variables to Android Studio?

like image 380
Pavel Dudka Avatar asked May 08 '15 15:05

Pavel Dudka


People also ask

Do we need to set environment variables for Android Studio?

You can set environment variables for Android Studio and the command-line tools that specify things like where the SDK is installed and where user-specific data is stored. This page describes the most commonly used environment variables.

How do I enable environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.


1 Answers

Found an answer here: Environment variables in Mac OS X

Essentially, you need to also set environment variables used by launchd via launchctl - this way environment variable will be available for anything launched from MacOS UI

So I modified my ~/.bash_profile as follows:

export MY_CUSTOM_VAR='Hello World'
launchctl setenv MY_CUSTOM_VAR $MY_CUSTOM_VAR
like image 132
Pavel Dudka Avatar answered Oct 13 '22 20:10

Pavel Dudka