Adam Brown
04/30/2023, 9:40 PMorangy
04/30/2023, 9:57 PMAdam Brown
04/30/2023, 9:58 PMAdam S
04/30/2023, 10:02 PMAdam Brown
04/30/2023, 11:17 PMephemient
05/01/2023, 12:29 AMmap
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:
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