Hi all, trying to add multiple build environments ...
# android
c
Hi all, trying to add multiple build environments to my project via env files (containing api base urls, secrets and such). Coming from Javascript and unsure how to do this in in Kotlin for an android project.
a
You can use gradle flavors for this. Env can be read from the gradle build file as well and then exposed to the app using buildConfigField
c
Is there a link to anything explaining how to do this? It also sounds like the env would be written into the gradle build file? That’s something that I don’t want to do, as I want to keep the env on gitignore
c
Thanks Afzal for the help!
a
No problem!
c
Actually I have one more question after reading through everything, I might have missed it. How would I use flavors in conjunction with this, so that I could use one file for a dev environment, and another for staging, and one more for production?
a
you could use buildTypes for that: https://www.journaldev.com/21533/android-build-types-product-flavors Define a dev/debug, staging, and release buildType, then define the same buildConfigField in all three of them. You could do the same with flavors as well. define 3 flavors: dev, staging, prod. But then because you need debug and release build types anyway, you'll have 6 total build configurations: devDebug, stagingDebug, prodDebug, devRelease, stagingRelease, prodRelease. You can, however, prevent gradle for generating all 6 but that's slightly off-topic. Simplest way: 3 buildTypes + buildConfigField in each of them. It's not the prettiest though but since gradle files are just groovy files, you could use code to loop through the properties file or something.
c
I guess I’m just confused on how to tie this all together… I guess I sort of understand that I’d have to loop through the properties file to get all the env data? But I guess I don’t understand the how as in how is the app defining which env file to use? I guess I’m also struggling on creating the different builds as well then?
a
gradle.properties file are imported automatically in build.gradle files. For importing other files, you can use the Properties API. You're defining this in the build.gradle file. See how the
designer_news_client_id
is used here, defined in the gradle.properties file and then used in
build.gradle
https://github.com/android/plaid/search?q=designer_news_client_id Since the root
gradle.properties
file is usually checked into version control, you can create one in the app folder as well, and it will be imported automatically too.
c
I’m still confused. So I would make a different
grade.properties
file for each build?
a
No, you'd just make one gradle.properties to hold all variables. In your
gradle.properties
, you can have:
Copy code
STAGING_DOMAIN=<http://staging.com|staging.com>
PROD_DOMAIN=<http://prod.com|prod.com>
then you can access those in your
build.gradle
file to expose it to your app:
Copy code
android {
  //... other stuff

  buildTypes {
    staging {
      buildConfigField 'String', 'API_DOMAIN', "\"${STAGING_DOMAIN}\""
    }
    release {
      buildConfigField 'String', 'API_DOMAIN', "\"${PROD_DOMAIN}\""    
    }
}
Then you can access
BuildConfig.API_DOMAIN
everywhere in your app code. When you select the release variant, it will be the value of
PROD_DOMAIN
, when you select staging variant, it'll be the value of
STAGING_DOMAIN
hope that helps
c
would it be
gradle.properties
or
local.properties
?
local.properties
is defaulted into gitignore, but
gradle.properties
isn’t. If it’s
gradle.properties
then how would i ignore this?
or would I be able to make a
app/gradle.properties
and put that on ignore instead?
a
yeah you can do the latter. OR check the first link I sent you, that one uses a different properties file, but then you have to load it manually in build.gradle