Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 979
STeN Avatar asked Sep 29 '11 12:09

STeN


4 Answers

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.

like image 114
richq Avatar answered Nov 08 '22 23:11

richq


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

like image 44
DavisNT Avatar answered Nov 08 '22 21:11

DavisNT


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.

like image 5
Abhinav Chauhan Avatar answered Nov 08 '22 23:11

Abhinav Chauhan


I couldn't make it work with System.getEnv("VAR"), the solution is to add it in the build.gradle(:app) to latter use:

defaultConfig {
    ...
    buildConfigField "String", "API_KEY", "\"${System.env.API_KEY}\""
}

Access it in other classes:

println(BuildConfig.API_KEY)

Check environment variable is already added to your system.

like image 2
dianakarenms Avatar answered Nov 08 '22 22:11

dianakarenms