Reputation: 3139
For now I've used the classic export
command to set an environment variable and tried to read this variable from my test with System.getenv()
function, but it doesn't seem to work.
Can someone give an example how to set and read a system variable in an Android application?
Upvotes: 5
Views: 14250
Reputation: 21
If you are familiar with using the NDK, simply create a native function in NDK space containing the line, or add it to an existing function.
setenv("ENVIROMENT_VARIABLE", "value", 1);
You can then use the java APIs System.getenv() to read it.
While this is cumbersome, it is necessary for using online code analysis tools like gcov (included with the android ndk) for code coverage.
Upvotes: 2
Reputation: 3139
My initial purpose was read external arguments in my Android application. So I finally used XML file as a configuration file. This still is not an answer to my question but someone might find it helpful. See how to load an XML configuration file here
Upvotes: 0
Reputation: 41
The env variable is only visible in processes that are descended from (or the same as) the process that sets the env variable value. (see http://en.wikipedia.org/wiki/Environment_variable#Synopsis)
This may be a factor in your apparent inability to see the value you set, if the process in which you want to read the value, is not a descendent of the process in which you set it.
So you need to examine the process hierarchy of your application(s) to determine where you should be setting the value.
Upvotes: 3
Reputation: 16439
To get and set a property you could do the following:
System.getProperty('property_name');
System.setProperty('property_name', 'value');
System.getenv() should work for returning a map of all available environment variables.
Can you post some of your code if it still doesn't work?
Upvotes: 3