Whats the recommended way in KMP projects to injec...
# gradle
j
Whats the recommended way in KMP projects to inject server config, like server domain? Like localhost vs actual domain in debug vs production. In Android often used build types or Build config, but that doesnt exist really in KMP from what I know of. Is there a good way of dealing with this?
g
There is a couple of third party libs for this
m
I find myself using env variables most of the times
βž• 1
j
Yes, any of them recommendd to use? πŸ™‚ I want to use the best tooling for this. Ideally would ofc also using things like Vault to store things and automatic inject them both in Gradle step but also in things like CI pipelines.
m
It's easy to set in containerized environments and on my local machine and doesn't require flavors
What's different between your local and prod environment?
j
Yeah I will use environment variables or gradle properties to get dynamic values in my setup, but I still need to propegate those values into the mobile app code, to be able to create my server setup based on that dynamic server values. Like localhost on debug build or if setup gradle property A use that, and if prod build then always use prod environment ofc.
local using localhost and prod well prod?
local can ofc setup prod as well if want to, but need to be able get the full range of setup dynamic in a nice way. This is easy in Android but not in KMP world πŸ˜„
m
Oh for your client apps? I've used
com.github.gmazzo.buildconfig
But really most of the times I try to avoid this altogether and use a debug menu that allows me to switch instead
j
Ah well in server side I want to know as well actually, how decide if its in production or not πŸ˜„ But ok yeah client side thanks, thats the library I took as well, I noticed Wharton heavy contributor to it, so seems legit πŸ˜„
Debug menu, any ideas how to solve that nice in KMP projects without build types? I dont want debug menus in production code πŸ˜„
m
> without build types At some point you will need 2 binaries. This is the cumbersome part. Whether using
com.github.gmazzo.buildconfig
or something else is an implementation detail
j
Yeah I just like src is not included at all, like debug vs release build src folders in Android world, so if adding code in debug its not existing in production at all. And using debugImplementation in Gradle for it. Otherwise exposing debug can cause major issues in production.
If using BuildCOnfig only for the server value, then at least only defined server in compile time and can change it in runtime.
m
You need to create different Kotlin sourceSets/compilations I guess
Or you "just" tweek your build based on an environment variable. In your CI, do
Copy code
PRODUCTION=true ./gradlew desktopDistribution # or whatever the name of the binary building task is
Probably what I would do if I really didn't want the debug menu in production code (but unless it's a very large app, I would probably don't mind too much either)
j
Yeah but there is no built in thinks in KMP/Gradle for this? Like adding custom build types for different source sets?
With my custom "flavour"
In my case I will start with buildConfig, and then I will see if need something more extensive πŸ™‚
m
Yea, no
variants
in KMP, which I personally like. This is the cause of so much complexity
c
Personally, I use environment variables for the server, and the client asks the server whether it's development or production during the first handshake
m
But you still need a switch for the base url, right?
To decide if the handshake happening on
<http://api.example.com|api.example.com>
or
<http://preprod.example.com|preprod.example.com>
c
Ah, I'm doing web, so the base URL is always "the URL from where I'm being served"
πŸ‘ 1
But if you're on Android, you still have environment variables, no? So you could have the AS run configuration add some specific config to tell it it's local
m
Yea, the question is whether you want
variants
, i.e. duplicate your task graph. Or just an env variable that tweaks your task graph in 2 different invocations
j
Yeah manually change environment variable between localhost/dev, staging, production etc and also custom urls under development is painful. Easiest if can change that inside gradle.properties or such, or predefined variables in SDK like VAult or such, so I can use same things in all places not only for developmenet, but also CI pipeleines etc.
with Variants I get complexity for each combination I want to be able to use πŸ˜„
Maybe I want some stuff in staging to be changed to dev tokens but some not, then need to change into another variant.
m
If you have
variants
, you can reuse CC between your debug and prod builds, this is the only advantage I can see
j
I am on KMP btw, where Android is one of the targets. I just refer to Android as easier in that
often want to have like server.url = localhost:8080 or if emulator on Android need localhost:forwardPort, sometimes https and sometimes http, sometimes want staging, sometimes want production, depending what I need. SOmetimes I need to use production server but debug values for everything else, to debug proiduction server etc.
But yeah seems like doesnt exist any good tool for this in KMP then.
Copy code
buildConfig {
    buildConfigField("APP_SERVER", project.providers.gradleProperty("app.server")
        .orElse("<https://productionserver>"))
}
I am adding it like this btw, feels ok πŸ™‚ SO can optionally override if want to.