11

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?

Diego Palomar
  • 6,958
  • 2
  • 31
  • 42
  • AFAIK, there is no `BuildConfig` in iOS anyway. My guess is that you would need to rig up build type-specific classes that code-generate something that you can use to determine the build type, akin to how the Android build tools code-generate `BuildConfig`. I don't know if there is something in the standard Kotlin Multiplatform build setup that does this automatically. – CommonsWare Jan 18 '19 at 13:54
  • I have the exact same question. Did you find any answer? – Archie G. Quiñones Nov 25 '19 at 16:06

2 Answers2

16

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:

actual val isDebug = BuildConfig.DEBUG

in iosMain:

actual val isDebug = Platform.isDebugBinary
josias
  • 586
  • 4
  • 9
0

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.

Artyom Krivolapov
  • 3,161
  • 26
  • 32