2

In build.gradle, the following is under a product flavor:

buildConfigField 'String', 'API_URL', formString(System.getenv("DEV_API_URL"))

What does formString(System.getenv("DEV_API_URL")) mean?

I am used to seeing the formString as a static value (and I can reference it in code as BuildConfig.API_URL") but am having a hard time figuring out what this code means as well as where "DEV_API_URL" is defined. Guidance and links are appreciated!

Daiwik Daarun
  • 3,804
  • 7
  • 33
  • 60
  • 1
    `DEV_API_URL` is an [environment variable](https://en.wikipedia.org/wiki/Environment_variable). – CommonsWare Feb 09 '16 at 20:29
  • Where would an environment variable be set? – Daiwik Daarun Feb 09 '16 at 20:31
  • 1
    On your development machine. E.g., for Ubuntu, you can have them in `~/.bashrc`. – CommonsWare Feb 09 '16 at 20:32
  • In the context of Android development, this means theres a system level variable (on the computer Android Studio is being run on) called 'DEV_API_URL' that is being used to set the value? (I've inherited this project and am a bit confused) – Daiwik Daarun Feb 09 '16 at 20:34
  • 1
    That's a reasonable description. However, note that environment variables are only going to be available to you outside of Android Studio. Inside of Android Studio... well, to make a long story short, you won't have access to any environment variables. Whoever was building this project clearly expected at least this product flavor to be built in other ways: manually at the command line, via a CI server, etc. – CommonsWare Feb 09 '16 at 20:40
  • Okay this makes sense now! Thanks – Daiwik Daarun Feb 09 '16 at 20:49
  • `formString` is a custom function probably, and it certainly escape the `"` of your string I guess. – oldergod Oct 20 '16 at 06:08

1 Answers1

2

1) formString must be a custom function defined somewhere in your build.gradle as there is no such function in Groovy or Java. If you cant figure out where it is, use a text search tool like ag (https://github.com/ggreer/the_silver_searcher)

2) System.getenv is a call that retrieves an environment variable defined on your machine, more here: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getenv(java.lang.String).

Alex Fu
  • 5,509
  • 3
  • 31
  • 40
  • In the context of Android, is `System.getenv` retrieving a constant defined in Android, or a constant defined on the machine running Android studio – Daiwik Daarun Feb 09 '16 at 20:38
  • @DaiwikDaarun `System.getenv` is a Java method call. Nothing to do with Android – Alex Fu Feb 09 '16 at 20:39
  • Interesting, so because I inherited this project, this variable would not exist on my computer? If so, why would an Android developer do this in their gradle file? – Daiwik Daarun Feb 09 '16 at 20:42
  • @DaiwikDaarun correct, you wont have that environment variable unless you set it. There can be several reasons why someone would specify an API URL through an env variable but if you want to ensure that your custom build config variable always has a valid value, it's a good idea to provide a fallback. – Alex Fu Feb 09 '16 at 20:46