Have you ever had the need to read an external variable like an environment variable from your Android build.gradle file?
On a recent project of mine we needed to enable our QA engineers to specify different login urls (for dev, staging, production, etc) to run our test automation against. The idea is that they would set a specific environment variable, ‘ANDROIDDOMAIN’ in this case, and our build script would read in this variable and add it to the BuildConfig class, which we then reference via our test.
Below is the code that reads in the environment variable:
As you can see, here we look for the environment variable named ‘ANDROIDDOMAIN’, and if it is present we print it out and return it from this method.
The next code snippet shows how to add the result of the ‘getAndroidDomain()’ call to the BuildConfig class (in this case we add to the BuildConfig debug class:
Now in our test class we can access the ‘TEST_ANDROID_DOMAIN‘ field on this class, like so:
Do you have a better way of doing this? Let me know!