简体   繁体   English

如何设置Android环境变量让app继承?

[英]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 . 我使用adb shell来调试app,并设置一些像这个export TESTVAR=test env变量,然后我在同一个shell中启动应用程序am start -n my.app.package ,奇怪的是进程看不到TESTVAR (检查/proc/pid/environ )因为app进程的父进程是zygote How to set the env for such app processes? 如何为这样的应用程序进程设置env?

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. 但是,如果您正在连接并使用JNI库并希望将环境变量用作这些库的指令,那么我不确定它是否可用。

In API 21, Google introduced Os class to expose system-level access to developers. 在API 21中,Google引入了Os类来公开对开发人员的系统级访问。 I haven't tried it so far, but it's supposed to be the interface between Android app and underlying Linux kernel. 到目前为止我还没有尝试过,但它应该是Android应用程序和底层Linux内核之间的接口。 Which is, hopefully, what you need. 希望这是你需要的。 There is, of course, setenv method which delegates the parameters you set to Linux's setenv method. 当然,有setenv方法将您设置的参数委托给Linux的setenv方法。

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. 就像我写的那样,我不确定这在生产中是否可行且可靠,而且这个类需要API级别21,而且我不知道你所针对的是哪个Android版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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