4

This question is specific to using cmake as part of Android Studio build process.

I'm using Android Studio 2.2 preview 7 on linux (ubuntu) Inside the CMakeLists.txt I am able to access the Android NDK path using: ${ANDROID_NDK}

But how can I access:

  • Any environment variable ?
  • If not possible, at least the Android SDK path ?

I already tried to used $ENV{name_of_the_environment_variable_here} but it's always empty, even if the environment variable exist. I guess that when gradle invoke cmake it "hide" the env var somehow.

Sistr
  • 1,175
  • 10
  • 26
  • Possible duplicate of [How to retrieve a user environment variable in CMake (Windows)](http://stackoverflow.com/questions/690223/how-to-retrieve-a-user-environment-variable-in-cmake-windows) – usr1234567 Aug 04 '16 at 11:15
  • I'm quite used to cmake, but the way Android Studio encapsulated cmake and call it is quite unique. There is a lot of interaction between cmake and gradle, and as a matter of fact I found it more reliable to send the env var from gradle to cmake than to get it from cmake directly, in that context. – Sistr Aug 07 '16 at 08:54
  • Hey, did you find a solution for this? – Evgeni Roitburg Apr 30 '18 at 11:47
  • Well, apart from the answer that I posted below (that got downvoted -_-) no ... – Sistr May 01 '18 at 13:29
  • 6 year later and still no answer – Andra Jan 22 '22 at 22:56

1 Answers1

0

I don't think you can use $ENV, it's just an example of a variable because they're environment variables. However, you should be able to type env and hit enter for a list of the variables you currently have set. Then, the ones you see in the list, you can invoke by typing $VARIABLE_NAME, using a command before them to get them to do something. E.g. echo $VARIABLE will echo your variable to stdout.

I'm not sure how $ANDROID_SDK was set, if it was part of an install process, etc. but generally you would set user environment variables in .profile, .bash_profile or .bashrc configuration files. These files are read by the shell in that order. System-wide variables are set in /etc/environment, /etc/profile, and /etc/bash.bashrc, but you probably don't want to mess with those (most distros encourage making ancillary additions in /etc/profile.local, but that's a story for another answer).

It doesn't particularly matter which one of these files you use, unless what you're trying to do interacts with the order in which they are loaded. Generally, I look for where the variables have been set by either the OS or other stuff I've added and put them near those. You can find where environment variables are set by typing:

% for i in .profile .bash_profile .bashrc; do grep -H PATH $HOME/$i; done

(% is the prompt, don't type %)

.. and this will loop through the 3 files and show you if a user $PATH is set in any of them.

Bash uses the export ENV command as opposed to set ENV, which is from the original sh, which AFAIK is only default on FreeBSD and derivatives like pfSense anymore. Almost all other OS use Bash by default, except MacOS which recently moved to zsh and also uses export, and OpenBSD which uses ksh (nobody uses OpenBSD).

If you want to verify which shell you are using, type echo $SHELL, or echo $0 and hit enter, and it should let you know.

You can add the environment variable ephemerally by typing this command in your bash terminal and pressing enter:

% export ANDROID_SDK_ROOT=/home/username/AndroidSDK

To be clear, this is an example path, so it'd be best to use the actual path in which your android SDK files reside. However, this example was a default install location Android Studio tried to use when I installed it recently, so if you're not sure where they are, it's probably a good place to check.

To have a more permanent setting of your environment variable, open a text editor and add the line above to one of the configuration files I mentioned in the first paragraph (they'll be in your $HOME folder). Or, you can run this from the prompt and it'll add it to your file automatically:

% echo 'export ANDROID_SDK_ROOT=/home/username/AndroidSDK' >> $HOME/.bashrc

Take care to use two angle brackets and not one, as one angle bracket will overwrite the entire file with the single line.

How can I access Any environment variable ?

If you're not sure which folder is $HOME, try typing cd $HOME and hitting enter - that'll take you there. That's how you access environment variables - use a command with the invocation of the variable and it should act as if you had typed out the entire thing.

To access environment variables, type echo $NAME_OF_VARIABLE and it should echo it to the screen. If you want to search your three config files I mentioned in the beginning for where an environment variable is set, you can use grep as I did earlier, just changing the search string for whatever you're looking for. E.g. (while in $HOME):

% grep SDK_ROOT .bashrc .profile .bash_profile

Or you can type env to list all the currently set variables and filter them by piping the output to the grep command:

% env | grep SDK

If you want to just list all of the set variables and root around the entire thing, just type env instead of piping it to grep (grep's a filter).

Lastly, I'll give you an example of my $ANDROID_SDK_ROOT $ANDROID_SDK and $SDK_ROOT variables in my .bashrc - I noticed while installing these tools, they use all three (isn't that fun?):

% grep ANDROID .bashrc
   export ANDROID_SDK=$HOME/development/Android/SDK
   export ANDROID_SDK_ROOT=$HOME/development/Android/SDK
export PATH=$PATH:$ANDROID_SDK:$JAVA_HOME:$ANDROID_SDK/cmdline-tools/latest/bin:$ANDROID_SDK/build-tools/32.0.0:$ANDROID_SDK/emulator:$ANDROID_SDK/emulator/bin64:$ANDROID_SDK/tools:$ANDROID_SDK/tools/bin:$ANDROID_SDK/extras:$ANDROID_SDK/platform-tools:$HOME/development/AndroidStudio/bin
export ANDROID_STUDIO=$HOME/development/AndroidStudio

% grep SDK_ROOT .bashrc
   export SDK_ROOT=$HOME/development/Android/SDK
   export ANDROID_SDK_ROOT=$HOME/development/Android/SDK

Hope that answers some questions, sorry it took so long to give you a response.

AveryFreeman
  • 1,061
  • 2
  • 14
  • 23