Android set(get) environmental variables in Java - android

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.

Related

How can I set and preserve an environment variable?

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

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.

Xamarin Android - Turn off Mono Logs

This might be a nit-picky thing, but in Xamarin when running an Android app, it dumps tons of lines in the console that start with [Mono]
Is there any way to disable these logs?
Thanks in advance
This can be done by changing the state of Monos execution environment on the device; which is just a set of environment variables that alters Monos behaviour (be it garbage collection, logging etc). In this case, to alter the logging behavior we need to modify the values stored in the environment variables MONO_LOG_LEVEL and MONO_LOG_MASK.
Xamarin.Android offers 2 mechanisms developers can use to change the execution environment:
1. Using adb shell setprop debug.mono.env. This can be done as a post build action.
2. Using an environment build file to change the execution environment state per project.
I prefer to use method 2 as it's easier to edit a text file than changing build actions. Do this using the steps outlined below.
Adding An Environment File
Add a plain text file called environment.txt to the root path of your Xamarin.Android project.
Right click on environment.txt and set its build action to AndroidEnvironment.
The environment file is series of key=value pairs seperated by newlines. For logging, we can set the following variables:
MONO_LOG_LEVEL
debug
info
message
warning
critical
error
MONO_LOG_MASK
asm
dll
cfg
all
type
gc
For example, we can ignore most messages by filtering MONO_LOG_LEVEL by error:
environment.txt
MONO_LOG_LEVEL=error
Background reading:
Android Environment
Mono - Logging Runtime Events
What I do is below. It's still imperfect as the window holds 10k logs, including the hidden ones, so mine eventually disappear. Additionally, I can't seem to copy from the window.
1) Use a tag of "AAA" for all my logs.
2) View the output in Tools>Android>Device Log
3) Sort alphabetically by Tag.
4) I find that the outputs beneath mine are still distracting. I can click the "filter" for the Tag and uncheck everything but mine. Annoyingly, I have to repeat this step periodically as new tags are not filtered by default.

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