Hello guys, I'm trying to properly configure my so...
# ksp
d
Hello guys, I'm trying to properly configure my sources to include KSP generated files (for Koin), but I keep failing 😅 I have 4 types of modules: • KMP jvm only • KMP jvm + Android (with some code generated from
androidMain
) • Android library • Android app As now I see them in the IDE, all the tests are ok, the app runs ok on debug and release, but somehow it crashes on TestLab (debug) because a class generated from a KMP+Android module is not there. I'll share my current config soon in the thread.
There are some custom extensions, but the purpose should be clear by their signature KMP jvm:
Copy code
target.extensions.configure<KotlinMultiplatformExtension> { ext ->
  ext.sourceSets.getByName("commonMain").kotlin.srcDir("build/generated/ksp/jvm/jvmMain/kotlin")
 }
KMP w/ android:
Copy code
target.extensions.configure<KotlinMultiplatformExtension> { ext ->
    ext.targetFromPreset(AndroidTargetPreset(target), ::configureAndroidTarget)
}
private fun configureAndroidTarget(target: KotlinAndroidTarget) {
    target.compilations.all { compilation ->
        compilation.defaultSourceSet.kotlin.srcDir(
            "build/generated/ksp/${compilation.name}/kotlin"
        )
    }
}
Android app/lib:
Copy code
target.extensions.configureIfPresent<AppExtension> { appExtension ->
    appExtension.applicationVariants.all { variant ->
        target.kotlinExtension.sourceSets.getByName(variant.name).kotlin.srcDir(
            "build/generated/ksp/${variant.name}/kotlin"
        )
    }
}
target.extensions.configureIfPresent<LibraryExtension> { libraryExtension ->
    libraryExtension.libraryVariants.all { variant ->
        target.kotlinExtension.sourceSets.getByName(variant.name).kotlin.srcDir(
            "build/generated/ksp/${variant.name}/kotlin"
        )
    }
}
y
Might be a build order issue. Those hardcored build dirs doesn't tell anything to Gradle about dependencies so it doesn't know that a task must be run to generate them. Locally, if those tasks already run, you wouldn't notice the problem
To properly set dependencies, otherwise, you can also just use dependsOn to ensure the completion waits for the producer task
d
I followed the doc from Koin Annotations and I tried to adapt it to my project 😄 I don’t find that part anymore tho 😕 What would be the right way to use generated code from sources, then? Should it work out of the box on most recent versions maybe?
y
No, idk about the documentation but Gradle cannot discover task dependencies from hardcoded strings (afaik). You need to find the task that generates it and flat map from it. (Assuming srcDir accepts a provider). If not, just do dependsOn for the generating task
i have something similar to that here: https://github.com/yigit/kotlin-sqlite-bindings/blob/main/sqlitebindings/build.gradle.kts#L73 where there is task that collects shared libraries from the native portion of a KMP project and adds them as resources for the jvm/android. (to be used via JNI). you need to find the task that generates
Copy code
"build/generated/ksp/${variant.name}/kotlin"
and then do:
Copy code
kotlin.srcDir(
  generatorTask.flatMap { "build/generated/..."
   }
)
that
flatMap
(or
map
) from a taskProvider tells gradle about the relationship
d
Ok, thank you! I'll take a look tomorrow, then, hoping I can solve it
I found that bit I mentioned tho, not from the KSP doc, but from Koin sample
y
You don't need that for KSP though, it already does it for you
d
I see, so I’ll try to drop those pieces of code and see where the problem is
It seems like I was missing a
kspAndroid
in a KMP+Android module 🤦‍♂️
Waiting for the truth to be told by TestLab 🙂
It is fixed now 🎉
101 Views