in the Android gradle plugin it's possible to defi...
# multiplatform
a
in the Android gradle plugin it's possible to define constants that will be compiled into your code, so you can pass things like version numbers into the app. Is this possible with multiplatform gradle plugin?
o
Are you looking for something like https://github.com/gmazzo/gradle-buildconfig-plugin ?
a
that's exactly what I'm looking for, thanks!
a
if you’d like to do it in pure Gradle, here’s a guide :) https://stackoverflow.com/a/74771876/4161471
a
oh thanks!
e
IMO that answer is more complicated than necessary
there is no need to
map
the property, since Gradle automatically gets the output from the task. also you can do all of this with just ad-hoc tasks, and even make it more general and track input properties without much work:
Copy code
import groovy.json.StringEscapeUtils.escapeJava

val generateBuildConfig by tasks.registering {
    inputs.property("PROJECT_VERSION", provider { project.version.toString() })
    val outputDir = layout.buildDirectory.dir("generated/source/buildConfig")
    outputs.dir(outputDir).withPropertyName("outputDir")
    doLast {
        outputDir.get().file("my/package/BuildConfig.kt").asFile.apply { parentFile.mkdirs() }.printWriter().use { writer ->
            writer.println("package my.`package`")
            writer.println()
            writer.println("public object BuildConfig {")
            for ((key, value) in inputs.properties) {
                writer.println("""public const val $key: String = "${escapeJava(value.toString())}"""")
            }
            writer.println("}")
        }
    }
}

kotlin {
    sourceSets {
        commonMain {
            kotlin.srcDir(generateBuildConfig)
        }
    }
}
of course, making a custom task type would allow you to make it cacheable, but that may only be worthwhile if it's reused