https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
b

bod

11/01/2019, 7:29 PM
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

h0tk3y

11/05/2019, 3:41 PM
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

bod

11/05/2019, 4:29 PM
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

h0tk3y

01/20/2020, 1:14 AM
You're welcome, glad I could help anyway. :)