How can I set and preserve an environment variable? - android

I want a variable that every process on the system can read. I don't know whether an environment variable is the right thing for this in Android, but
in Linux, I can set an environment variable with
VARIABLE=value
export VARIABLE
I can make it persistent by appending these two lines to the file .bashrc (or whatever the file is called, depending on the shell). Then after a restart, every process can read the variable.
How can I do the same thing on the Android system?

You cannot set environment variables in android what you can do is use shared preference which can be accessed through out your app later

Related

Bitrise default environment variables

Where I can find full list of Bitrise default environment variables with actual values?
I can see list of variables in Select variable dialog:
but there's no possibility to check their values. I don't want to print all of them in command line using echo and check their values.
Would be nice to have this information in bitrise.io documentation, which is missing at the moment.
I've found some bitrise* repositories in github, and in the source code of steps I was able to check some default values for environment variables. It's still not the desired result.
Why we don't have docs for the values: because it can change any time. The point of Environment Variables is that you can reference things where the value might change. The variable's meaning remains the same, but the value might change.
For example, $BITRISE_SOURCE_DIR refers to the main working directory, which (by default) is the code directory (where your code is git cloned). On the OS X stack it's usually /Users/vagrant/git, on the Android Docker stack it's usually /bitrise/src; but we don't guarantee that the location won't change in the future, and you can change this location as well.
Another important thing is, if you use the bitrise CLI to run your automation on your own Mac, the $BITRISE_SOURCE_DIR environment variable will be set by bitrise to the directory you call bitrise from, which is usually the same directory as on bitrise.io VMs, the one with your code.
Why is this important? If you use the $BITRISE_SOURCE_DIR environment in your script instead of a hardcoded value, it'll refer to the same thing (the source code directory path), no matter which environment you use to run your automation.

U-Boot: Loop Variable through to user space

during the execution of th MLO I create a variable, whose value I want
to make accessible to user space applications in Android. How can this
be achieved?
One way would be to write the contents of the variable to external
memory and let it read by the user space process. However, I would need
to make sure that during boot no other process is overwriting the address.
Do you know of any other ways, ATAGs? If ATAGs can be used, how would one do this? Is it necessary to develop a kernel module?
Cheers
From linux userspace, you can get info from U-Boot environment variables using "fw_printenv" application. During U-Boot execution you would "setenv variablename value", then saveenv.
Your U-Boot MLO would need CONFIG options set to enable the env commands. MLO usually wants (and needs) small code footprint, env commands will make bigger code footprint, that could be an obstacle.
At the linux side, you would need "fw_printenv" configured for your particular target's memory. That can be done at runtime, see fw_env.config. You can get the target executable built in u-boot/tools/env/. This assumes that android carries over the linux mechanisms in this area; I am not familiar with android platform details.

The meaning of "variable" with respect to configuring eclipse

Several times I have seen documentation that makes statements like "set the XXX variable to YYY", but I am not at all sure how/where these "variables" get set. Sometimes it looks like they may be old-style DOS environment variables (which I know how to set), but other times not.
So for example, looking at this SO question, the accepted answer states "NDK_MODULE_PATH must be in your path" - would that mean the DOS-environment-path? Or some other path? And in this document, it says "When you compile, you need to set the NDK_MODULE_PATH variable to the directory above...". What kind of "variable" is that? A DOS-environment variable? Some other kind? How/where would I set it?
EDIT: Just a thought - if I go to Window > Preferences > C/C++ > Build > Build Variables, I see I can add things there - do you think that's what was meant in the two examples?

Android set(get) environmental variables in Java

I have experimented little with Android OS and I tried to call System.getenv() to get environmental variables. It works e.g. for $PATH, but I was not able to define own variable, which can be accessible in this way... Is it possible?
I have tried to set and export variables from adb shell as a shell user but it does not work - no matter if I started the application from the phone menu or when I used the adb shell am command.
Can the Runtime.getRuntime().exec() help there? Will it help if I will have root access to the phone?
Thanks
Environment variables are only visible in a process that sets the variable, and child processes launched after setting the variable. When you set the environment variable from the adb shell you are not in the parent process of the process that launches the Android application, so the application cannot see the variable you set.
In Java (and Android) there is no System.setenv(), but if you need to set an environment variable for your own program to read there are always better ways. One such way is setting and getting Properties instead.
Setting environment variables in Java is not really possible (well, it is, but you don't want to do it). You can use ProcessBuilder if you want to set a variable that another process should read, but that's if the process is launched from a Java/Android program.
Think about what problem you're trying to solve, and if it can be done without using environment variables. They're not a good fit in Java, and are even worse on Android.
It is possible to set environment variables in Android applications. However, as #richq said, those variables will be only visible in processes launched from the application that has set environment variable (and JNI libraries used by the application).
See this post for regarding setting environment variables from Android application:
https://stackoverflow.com/a/22315463/927592
Android API 21 provides a way to set the environment variables. To set an environment variable, invoke Os.setenv.
See this android.system.Os documentation and this setenv(3) documentation.
Each process has its own environment, which is copied from the parent process's environment. So the environment variables are per-process.

What is an appropriate directory in which to install Android SDK?

I don't use java much so what are the common (best practice) locations to install components like this. I want to put them in a system directory so multiple users have access.
Common locations are /opt, /srv, and /usr/local. I tend to lean toward /usr/local.
Note that the Android SDK doesn't really require you to install much, it's mostly self-contained. All you need to do is tell Eclipse where it is. You may also want to add the tools and/or platform-tools directories to the system-wide PATH so that your users can use adb and other tools.
See http://developer.android.com/sdk/installing.html.
On Linux, I typically use /usr/local/android-sdk, but anywhere that makes sense and that won't get clobbered by your system works. Just be aware that it may actually make sense to put the SDK in a per-user location, since it requires write access to create a VM image and to download SDK updates.
To be more concise and allow user writing for things like sdk installations and etc, you could put it somewhere inside the ~/.local directory as per the XDG file system hierarchy like ~/.local/lib/arch-id/android-sdk
Also remember to set the ANDROID_SDK_ROOT env variable to that directory as the ANDROID_HOME variable was deprecated.
It doesnt matter where you put the sdk. Just put it in any folder where all user's can access it...

Categories

Resources