Hello, World! I'd like to generate a small file a...
# multiplatform
b
Hello, World! I'd like to generate a small file and include it to the common sourceset. In 'normal' (non multiplatform) projects, I have been doing this this way:
Copy code
// Create a Version.kt file and add it to the source set
task generateSources {
    def outputDir = file("$buildDir/generated/source/kotlin")
    outputs.dir outputDir
    doFirst {
        def outputWithPackageDir = new File(outputDir, "com/example/mylib")
        outputWithPackageDir.mkdirs()
        new File(outputWithPackageDir, "Version.kt").write("package com.example.mylib\ninternal const val VERSION = \"$project.version\"")
    }
}
compileKotlin.dependsOn generateSources
compileKotlin.source += generateSources.outputs.files
But that doesn't work in my multiplatform module. I've tried to replace
compileKotlin
by
compileKotlinCommon
but doesn't work either. Ideas?
h
Try this:
kotlin.sourceSets.commonMain.kotlin.srcDir(generateSources.outputs.files)
. IIRC it will also add the appropriate task dependencies, so no need to specify
<...>.dependsOn generateSources
. If it doesn't, use
generateSources.outputs.files.builtBy(generateSources)
.
b
Thanks a lot! I'll try ASAP and keep you posted.
by the way, I'm 2 months late, but that did work 😛 Thanks a lot
h
You're welcome, glad I could help anyway. :)