Folks , is there really no way to relay the curren...
# multiplatform
b
Folks , is there really no way to relay the current product flavor to the shared module ? In my android app's build gradle i have two flavors which decide what the base URL for ktor should be , and ktor obviously resides in shared module
Copy code
productFlavors {
    create("usa") {
        isDefault = true
        dimension = "exchange"
        applicationIdSuffix = ".usa"
        versionCode = gitCommitCount
        versionName = "3.0.${gitCommitCount}"
        buildConfigField("String", "BASE_URL", "\"<https://usa.xyz.com/api/>\"")
    }
    create("canada") {
        dimension = "exchange"
        applicationIdSuffix = ".canada"
        versionCode = gitCommitCount
        versionName = "3.0.${gitCommitCount}"
        buildConfigField("String", "BASE_URL", "\"<https://canada.xyz.com/api/>\"")
    }
}
I have been googling for hours and the only thing i got close to was BuildKonfig library but that too doesn't support flavors . I am finding difficult to believe that there isn't a solution for this out there ?
m
I think you'll find the solution is "flavour modules", ie add a module, put your flavour specific code in that, and then have values in your build files to select which "flavour" you want.
☝️ 1
💯 1
p
What Mark Stanton says, use gradle modules as flavors. Even in Android, flavors are great for things like resources, assets or configuration data. But it allows people to add code and that is where problems start. From re-running unit tests more than once due to running the tests on each flavor, to having to release and maintain multiple versions of a library (if you have internal libraries).
b
@MarkRS @Pablichjenkov thanks for the advice . However the suggestion though very descriptive still doesn't makes sense to me . I mean I dont know how would it look like practically . Any chance there is some repository on git where I can actually see this approach in play ?
p
Sure, check for example the usage of
themeFlavorProvider
in this project: https://github.com/pablichjenkov/macao-marketplace/blob/dev/composeApp%2Fbuild.gradle.kts#L14
If you only want to provide a url, it sounds a bit of an overkill. For only a url, I would use the BuildKonfig plugin mentioned above and pass the value when I build the App. I would have one build script for Canada and one build script for US and and would pass the corresponding value accordingly
b
@Pablichjenkov yeah it appears to be quite a lotta setup for my simple use case . When you say "I would use the BuildConfig plugin mentioned above" are you referring to the BuildKonfig library or ?
p
Correct yes, let me fix it
🙌 1
b
Hey how can i set the applicationsuffixid based on the variant i have selected in the build variants ? I read over the internet that we can do that by using product flavours implemented as modules. However this approach would require us to change the flavour from gradle.properties correct? I was looking for a way in which when i change the variant from the Build Variants in android studio and play the app , the suffix gets automatically applied @Pablichjenkov
p
The way you provide the configuration is up to you. In the project I linked above, these properties are set in gradle.properties as an example. Honestly gradle.propeeties is not the best place to do so. For example, I normally set a file
app-build-config.json
in the ci pipeline. Depending on the workflow/flavor to build, it will pick the right file for the given environment. If you don't have a pipeline. You can host a simple service that provides the file based on the flavor. You can set this in the App as part of a prepare build script . Or you can do the request programmatically in Gradle code
Now for regular development, I just place the properties in gradle.properties and just change it there, is simple. Just don't commit this file to the repo
1