简体   繁体   中英

How to set Android environment variables for app to inherit?

I use adb shell to debug app, and set some env variable like this export TESTVAR=test , then I start app in the same shell am start -n my.app.package , strangely the process cannot see TESTVAR (examine /proc/pid/environ ) because the app process's parent is zygote . How to set the env for such app processes?

If you're using environment variables for testing purposes, and do not have functionalities that will depend on them in production, this answer may be helpful. However, if you're interfacing and using JNI libraries and want to use environment variables as a directive for those libraries then I'm not sure if it's usable.

In API 21, Google introduced Os class to expose system-level access to developers. I haven't tried it so far, but it's supposed to be the interface between Android app and underlying Linux kernel. Which is, hopefully, what you need. There is, of course, setenv method which delegates the parameters you set to Linux's setenv method.

Like I wrote, I'm not sure if this is viable and dependable in production, and this class requires API level 21 and I don't know which Android version you're targeting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM