How to pass a parameter to the Java code in run/debug configuration from Android Studio
android, android-studio, environment-variables, run-configuration
Solution
I suggest using productFlavors. Each flavor can contain environment specific settings. I simply have a class called 'Environment' which contains all the `public static final String`s that I need and each product flavor includes an different version of this class with the values set for the environment.
Problem
My android app does some http requests to my server. However sometimes I am debugging the new api code that runs on my development machine. I would like to be able to pass something (like an environment variable) so in my code, if it's present I would be able to use that as the hostname for the api requests from the android emulator. So I'm looking for a way to pass something like: ``` API_SERVER=http://10.0.2.2/myapp/ ``` and in my code I would use it somehow, for example: ``` final static String API_SERVER_REAL = "http://example.com/"; final String apiServerOverride = System.getenv("API_SERVER"); final String API_SERVER = (null != apiServerOverride && !apiServerOverride.isEmpty() ? apiServerOverride : API_SERVER_REAL); ```