4

I'm writing a C/C++ Android app. One of the libraries I'm using prints additional debugging information depending on whether an environment variable is set. Is there's any way to set one?

I was hoping that the launch target could be configured with an environment variable in Android Studio. Under Launch Options, there's an option to provide flags to the am start call but Activity Manager doesn't provide any way of setting an environment variable prior to launch.

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
  • Either start the app with an intent which provides the environment variable value(s) or [Define and use a system property](https://stackoverflow.com/q/3750109/295004) or perhaps I'm not understanding how you expect the debug flag values to be passed to the library. – Morrison Chang Mar 23 '18 at 07:18
  • @MorrisonChang I don't believe environment variables and Java system properties are the same thing. See https://stackoverflow.com/questions/7054972/java-system-properties-and-environment-variables#answer-7054981. System properties are settable using `System.setProperties()` but environment variables can only be read within Java, using `System.getenv()`. – spaaarky21 Mar 23 '18 at 21:40
  • So the library doesn't have an alternate way of enabling the debugging information other than environment variable given 'For these reasons, environment variables are more likely to have unintended side effects. It is best to use system properties where possible.' from https://developer.android.com/reference/java/lang/System.html#getenv(java.lang.String) – Morrison Chang Mar 23 '18 at 21:58
  • There doesn't seem to be, no. – spaaarky21 Mar 24 '18 at 04:22

1 Answers1

5

Include a wrap.sh file in your APK: https://developer.android.com/ndk/guides/wrap-script.html

This only works for debuggable APKs, but given that you want it for logging that's probably for the best anyway.

Dan Albert
  • 10,079
  • 2
  • 36
  • 79
  • The API 26+ requirement is a little unfortunate but I'm glad Android added this. It's exactly what I needed. – spaaarky21 Mar 28 '18 at 16:29