Diego Palomar
Diego Palomar

Reputation: 7061

How to determine build type in kotlin-multiplatform project

I’m working on a multiplaform project, iOS and JVM (I’m not targeting Android directly). Depending on the build type (debug or release) I want to configure the logging level (i.e. to print only errors in release). Since there is no a BuildConfig class available, how can I know from commonMain the build type?

Upvotes: 14

Views: 5861

Answers (2)

josias
josias

Reputation: 771

Not a direct answer to the question, but for android/ios one can define a property like this:

in commonMain:

expect val isDebug: Boolean

in androidMain:

import com.yourPackage.BuildConfig

actual val isDebug = BuildConfig.DEBUG

in iosMain:

import kotlin.native.Platform

actual val isDebug = Platform.isDebugBinary

Note: for Android BuildConfig, you may need to add this in your app's Build.Config

android {
    buildFeatures {
        buildConfig = true
    }
}

Upvotes: 33

Art Shendrik
Art Shendrik

Reputation: 3471

It is not possible right now for all possible targets. Build type is resolved only for android.

But you can create two versions of the module e.g. module and module-debug.

Then declare in module:

const val IS_DEBUG = false

in module-debug:

const val IS_DEBUG = true

Finally in the resulting application or module gradle configuration you can declare dependency on what you need. E.g.:

if (DEBUG_ENV)  // you need to set DEBUG_ENV from property or environment variable
    implementation(project(":module-debug"))
else
    implementation(project(":module"))

or for android:

debugImplementation(project(":module-debug"))
releaseImplementation(project(":module"))

This way you can change logic using IS_DEBUG constant for every target or can create even completely different implementations of something in debug and release modules.

Upvotes: 2

Related Questions