anyone added with success some kotlin generated co...
# kotlin-native
g
anyone added with success some kotlin generated code in a kotlin multiplatform module?
g
There is no simple solution, you of course can generate some code before compilation, for example SQLDelight does this for MPP, but you have no access to kapt because it’s JVM only API
d
I currently do this in
kgl
using kotlinpoet. There's no nice way of reading/modifying existing Kotlin code (at least outside the JVM backend). We're sorta waiting for IR to land before anything like that is available.
g
modifying existing Kotlin code
It’s also impossible on JVM, JSR-269 allows only read existing code and generate new code
g
if i’m generating a static configuration class with kotlin poet, there is no possibilities to add this class to the classpath and use this fields? basically i need something that works like the Android BuildConfig
d
You can do that easily.
You just need to generate code into some folder in the build directory and add that folder to you source set.
g
this is what i was trying to do :
Copy code
import gen.BuildConfigGeneratorPlugin

plugins {
    id 'kotlin-multiplatform'
}

apply plugin: BuildConfigGeneratorPlugin

sourceSets {
    generated {
        kotlin.srcDir += "generated/source/main/kotlin"
    }
}

kotlin {
    targets {
        fromPreset(MPPTools.defaultHostPreset(project), 'runner') {
            compilations.main.outputKinds 'EXECUTABLE'
            compilations.main.entryPoint 'com.danger.runner.main'
        }
    }
}

compileKotlinRunner{
    dependsOn(generateBuildConfig)
}
but i’ve got this error:
Copy code
BUILD FAILED in 0s
Could not get unknown property 'srcDir' for object of type org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension.
d
You'll probably need to look into using
buildSrc
.
g
yes i’m generating the code with a plugin in buildSrc
d
Move the
sourceSets
block into the
kotlin
block.
You'll also want to add a source set dependency from
runnerMain
to
generated
.
g
mmmh how? 😄 now is compiling but the buildconfig is still not resolved
d
g
unfortunately is still failing:
Copy code
.BuildConfig
                         ^

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':{moduleName}:compileKotlinRunner'.
here my build.gradle:
Copy code
import gen.BuildConfigGeneratorPlugin

plugins {
    id 'kotlin-multiplatform'
}

apply plugin: BuildConfigGeneratorPlugin


kotlin {
    sourceSets {
        generated {
            kotlin.srcDirs += "generated/source/main/kotlin"
        }

        runnerMain {
            dependsOn generated
        }
    }
    targets {
        fromPreset(MPPTools.defaultHostPreset(project), 'runner') {
            compilations.main.outputKinds 'EXECUTABLE'
            compilations.main.entryPoint 'com.danger.runner.main'
        }
    }
}

compileKotlinRunner {
    dependsOn(generateBuildConfig)
}


MPPTools.createRunTask(project, 'runProgram', kotlin.targets.runner) {
    
}
@Dominaezzz thanks for your time btw
d
Move
sourceSets
below
targets
and if that doesn't work just rename
generated
to
runnerMain
and call it a day lol.
g
before it was below targets already 😄 lol.. ok let me try
hahah same.. 😅
d
You might need to add stdlib dependency to
generated
.
y
btw you can use this plugin to generate somthing similar to BuildConfig 😉 https://github.com/yshrsmz/BuildKonfig
🎉 1