skip to Main Content

I need help understanding how to create different application versions in the Kotlin Multiplatform project using the common codebase. In a ‘classic’ Android project I would create productFlavours e.g. applicationA and applicationB, appropriate sourceSet directories, and then put product-flavour-specific files in the source sets.

enter image description here

Every product flavor has its own Application class, where different libraries are initialized, MainActivity class, where a different navigation graph is created, and also own resource files (colors.xml, settings.xml), and in particular most important google-services.json file for the configuration with a desired Firebase project.

enter image description here

How can I achieve the same result in the Kotlin Multiplatform project? Is it ever possible? If you have an idea how to resolve such a case I and many other people will be grateful for the detailed description.

2

Answers


  1. This post might give you some ideas how to achieve "flavor" like functionality in multiplatform project. You can find there link to this example repository.

    There is library BuildKonfig which lets you create BuildConfig file in multiplatform project.

    This might help you to further customize your runtime.

    buildkonfig {
        packageName = "com.your.packagename"
        objectName = "BuildConfig"
        exposeObjectWithName = "BuildConfig"
    
        defaultConfigs {
            buildConfigField(STRING, "ApiUrl", "http://localhost:9668")
            buildConfigField(STRING, "apiKey", "apiKey")
            buildConfigField(STRING, "secret", "secret")
        }
    
        defaultConfigs("debug") {
            buildConfigField(STRING, "ApiUrl", "http://localhost2:9668")
            buildConfigField(STRING, "apiKey", "apiKey2")
            buildConfigField(STRING, "secret", "secret2")
        }
    
        defaultConfigs("release") {
            buildConfigField(STRING, "ApiUrl", "http://192.168.66.90")
            buildConfigField(STRING, "apiKey", "apiKey3")
            buildConfigField(STRING, "secret", "secret3")
        }
    }
    

    To build you project just use:

    ./gradlew proguardDesktop --project-prop buildkonfig.flavor=release
    
    Login or Signup to reply.
  2. One can as well define productFlavors in app_android/build.gradle.kts.
    This only works for Android, but one can at least cover different vendors.
    There’s even a "Kotlin Multiplatform Wizard": https://kmp.jetbrains.com.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search