Guilherme Delgado
09/02/2023, 3:50 PMcommonMain
source set so that it can be used in iosMain
, be it by dependingOn commonMain
or if the generated files are created/moved in/to build/generated/ksp/iosMain
(even better).
However, I’ve encountered some challenges. KSP does not currently support Native, and I can’t make commonMain
implement my lib-ksp
directly due to issues with iOS targets. I came across a helpful that suggests porting the library to multiplatform instead of just targeting JVM. In our KMM project’s “shared” module, we should apply the “android.library” plugin and include lib-ksp
in the dependencies outside of the Kotlin source sets. I attempted this, and it worked for androidMain
but not for commonMain
, which was not the outcome I expected. In the end I see no difference from making androidMain
implement this dependencies.
I’ve been studying how @Rick Clephas achieved a similar setup in his KMP-NativeCoroutines library, but I haven’t been able to reproduce it yet.
Can anyone provide guidance on the current solution or a workaround for this issue? Your help would be greatly appreciated. Thanks!Rick Clephas
09/02/2023, 5:45 PMGuilherme Delgado
09/03/2023, 6:02 PMbuild.gradle.kts
of my library (let’s call it lib-ksp
):
plugins {
alias(libs.plugins.kotlin.multiplatform)
}
kotlin {
explicitApi()
jvmToolchain(11)
jvm {
compilations.all {
kotlinOptions.freeCompilerArgs = listOf("-Xjvm-default=all")
}
}
iosX64()
iosArm64()
iosSimulatorArm64()
val commonMain by sourceSets.getting {
dependencies {
implementation(libs.ksp.api)
}
}
val jvmTest by sourceSets.getting {
dependencies {
implementation(libs.test.junit)
implementation(libs.test.kotlin)
implementation(libs.test.kotlinCompile)
implementation(libs.test.kotlinCompileKsp)
}
}
}
And when I try to import it in a KMM project:
val commonMain by getting {
dependencies {
implementation("lib-ksp")
configurations["ksp"].dependencies.add(implementation("lib-ksp"))
}
}
val iosMain by creating {
dependsOn(commonMain)
}
I get this output:
> Could not resolve com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13.
Required by:
project :shared > project :lib-ksp
> No matching variant of com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13 was found. The consumer was configured to find a library for use during 'kotlin-api', preferably optimized for non-jvm, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native', attribute 'org.jetbrains.kotlin.native.target' with value 'ios_arm64' but:
- Variant 'apiElements' capability com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13 declares a library for use during compile-time, preferably optimized for standard JVMs:
- Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_arm64')
- Variant 'runtimeElements' capability com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13 declares a library for use during runtime, preferably optimized for standard JVMs:
- Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
- Other compatible attribute:
- Doesn't say anything about org.jetbrains.kotlin.native.target (requoired 'ios_arm64')
What am I doing wrong?Rick Clephas
09/03/2023, 6:23 PMlib-ksp
is your KSP plugin, right? In that case it should be a plain Kotlin JVM project.
KSP only runs on the JVM (during compilation).
If your library also provides Kotlin code that should be used in the client project, you'll need to create a separate Kotlin Multiplatform module for that.
E.g. KMP-NativeCoroutines has a kmp-nativecoroutines-annotations
module that contains multiplatform annotations that can/should be used in the client project. The actual KSP plugin is in the kmp-nativecoroutines-ksp
module (which is JVM only).Guilherme Delgado
09/03/2023, 10:14 PMSoIt’s equivalent to youris your KSP plugin, right?lib-ksp
kmp-nativecoroutines-ksp
module, not kmp-nativecoroutines-gradle-plugin
, in other words, it’s where I have my symbol processing logic.
If your library also provides Kotlin code that should be used in the client project, you’ll need to create a separate Kotlin Multiplatform module for that.Alright, I’ve noticed that you’ve also separated the annotations into a KMP module, and this one covers all the desired targets. Now, correct me if I’m wrong, I believe your “glue” resides in the
KmpNativeCoroutinesPlugin: KotlinCompilerPluginSupportPlugin
which is your gradle plugin, correct? Because your sample shared
imports this logic by doing: id("com.rickclephas.kmp.nativecoroutines")
.
I have never created a gradle plugin, only conventions, but I believe the logic is the same, correct?
Sorry if I’m misunderstanding these concepts, but it’s my first time trying, and your project is the best example I’ve found so far. 😅Guilherme Delgado
09/04/2023, 1:23 AMval iosMain by creating {
dependsOn(commonMain)
dependencies {
implementation("lib-annotations")
}
}
listOf(iosX64, iosArm64, iosSimulatorArm64).forEach { target ->
target.binaries.framework { baseName = "shared" }
getByName("${target.targetName}Main") { dependsOn(iosMain) }
val kspConfigName = "ksp${target.name.replaceFirstChar { it.uppercaseChar() }}"
dependencies.add(kspConfigName, "lib-ksp")
}
Rick Clephas
09/04/2023, 5:15 AMGuilherme Delgado
09/04/2023, 9:07 AMiosSimulator64Arm
, is not capable of "seeing" the code from iosMain
, for example, some imports cannot be resolved. My build.gradle
already specifies that all ios Targets depend on iosMain
. Shouldn't be enough? Maybe I'm missing something 🤔Rick Clephas
09/04/2023, 10:13 AMGuilherme Delgado
09/04/2023, 10:22 AM#KMP
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.androidSourceSetLayoutVersion=2
kotlin.mpp.stability.nowarn=true
org.jetbrains.compose.experimental.uikit.enabled=true
xcodeproj=./iosApp
Rick Clephas
09/04/2023, 10:30 AMGuilherme Delgado
09/04/2023, 10:32 AMGuilherme Delgado
09/05/2023, 9:36 AMRick Clephas
09/05/2023, 9:38 AMGuilherme Delgado
09/05/2023, 9:57 AM