Hi. I need to pass some information from gradle in...
# multiplatform
f
Hi. I need to pass some information from gradle into multiplatform tests (mainly environment values such as hostnames that depend on where the tests are running). For the JVM platform, this could be achieved via the classic environment variables method. Probably for the nodejs platform a similar solution could be achieved (using an expect-actual way of accessing the environment on those two platforms). However the problem is more tricky for the browser platform. I was wondering if there is a way to pass information from gradle into the test, in build time. The information will be the same for all target platforms but will depend on where the build is happening. Thanks!
c
You can generate Kotlin sources from within Gradle.
Copy code
val generateBuildConfigTask = tasks.register("buildConfig") {
    val generatedDir = "${layout.buildDirectory.asFile.get()}/generated"

    val someData = "asdf" // you can read an env variable, gradle property, compute something...

    inputs.property("someData", someData)

    outputs.dir(File(generatedDir))

    doLast {
        val outputFile = File("$generatedDir/your/org/BuildConfig.kt")
        outputFile.parentFile.mkdirs()

        outputFile.writeText(
            """
// Generated file
package <http://your.org|your.org>

object BuildConfig {
    const val SOME_DATA: String = "$someData"
}
""".trimIndent()
        )
    }
}
Copy code
getByName("jvmTest") {
    dependencies {
                kotlin.srcDir(generateBuildConfigTask)
    }
}
f
Thanks.