I have in my convention plugin for KotlinNative ad...
# compose-ios
j
I have in my convention plugin for KotlinNative added freeCompiler argguments like this, see ๐Ÿงต for code. But in my iosMain code + on clean builds I still get warnings:
This declaration needs opt-in. Its usage must be marked with '@kotlinx.cinterop.ExperimentalForeignApi' or '@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)'
How do I remove these things? Sometimes work adding them into all sourceSets, but then it complains the optins cannot be resolved in some modules not using it instead ...
Configuration for KotlinNativeTarget.kt
c
That should fix the warning you encountered, are you putting that part before using the kotlin { iosArm64() } block?
j
After having this:
Copy code
fun Project.configureKotlinMultiplatform() {
    kotlinMultiplatform {
        applyDefaultHierarchyTemplate()

        androidTarget()

        iosX64()
        iosArm64()
        iosSimulatorArm64()

        targets.withType<KotlinNativeTarget>().configureEach {
            compilations.configureEach {
                compilerOptions.configure {
                    freeCompilerArgs.addAll(
                        "-opt-in=kotlinx.cinterop.ExperimentalForeignApi",
                        "-opt-in=kotlinx.cinterop.BetaInteropApi"
                    )
                }
            }
        }

        targets.configureEach {
            sourceSets.all {
                languageSettings.optIn("kotlin.ExperimentalUnsignedTypes")
            }
            configureFreeCompilerArgsArguments(
                "-Xexpect-actual-classes"
            )
        }

        configureKotlin()
    }
}
Its in a convention plugin if impacting anything.
I can compile, but my code in iosMain always red highlighted and clean compile gives warnings all the time, very annoying as the code is correct and this setup as well as I know of. Not sure if a bug in which order using plugin aware Gradle things? Maybe need to add this earlier somehow?
c
-opt-in
should work, but if the compiler still emits the warning, try
optIn.addAll
instead of
freeCompilerArgs.addAll
. Mine is like the following:
Copy code
kotlin {
  iosX64()
  iosArm64()
  iosSimulatorArm64()
}

// In a plugin:
targets.forEach { target ->
  if (target is KotlinNativeTarget) {
    target.compilations.getByName("main") { compilation ->
      compilation.compilerOptions.configure {
        optIn.add("kotlinx.cinterop.ExperimentalForeignApi")
      }
    }
  }
}
j
@Chanjung Kim Thanks! ๐Ÿ™‚ Whats the reason behind using target.compilations.getByName("main")? Why not like compilations.configureEach {?
Also can add it works for all other targets not being iOS ๐Ÿ˜› So its only KotlinNativeTarget problematic.
c
> Whats the reason behind using target.compilations.getByName("main")? Other compilations are something like
test
, so in my case configuring the main compilation was sufficient. I don't think
compilation.configureAll
would make a difference. The above code works for targets like
linuxX64
or
mingwX64
, because they are also instances of
KotlinNativeTarget
. I just omitted them for brevity. You can actually do like the following as well:
Copy code
arrayOf(
  iosX64(),
  iosArm64(),
  iosSimulatorArm64(),
  mingwX64(),
  linuxX64(),
).forEach { target ->
  target.compilations.configureAll { ... }
}
j
Ah I see ๐Ÿ™‚ I will test this and see if it works for my case, I tried before find something similar but had some issues ๐Ÿ™‚ I would think maybe it dependant of order of convention plugins vs Jetbrains plugins as well.
Btw in same topic, do you know whats difference using this vs:
Copy code
targets.configureEach {
            sourceSets.all {
                languageSettings.optIn("kotlin.ExperimentalUnsignedTypes")
            }
K 1
@Chanjung Kim Sorry to say your config didnt help, same issue. Any ideas? ๐Ÿ˜„
c
How about using
Project.afterEvaluate
? Try setting that optIn in the gradle script directly, not in your plugin.
A compilation depends on multiple source sets, and AFAIK, setting optIn in a sourceSet affects that sourceSet only, while setting optIn in a compilation affects all the source sets.
j
I will not do that, will break Gradle cache and things, not recommended. I dont understand why it works on all other Kotlin multiplatform tarhets, but not KotlinnativeTarget :S
c
ExperimentalForeignApi is available only on native targets.
j
Yes, I mean why does it work using other opt ins for Android target, with same compiler setup, but not for iOS.
๐Ÿ˜ข 1
c
You can inspect the actual arguments passed to
konanc
by passing
--scan
argument to
gradlew
. You can check whether -opt-in is actually passed or not to the compiler.
j
How do I pass --scan to KMP run configs Jetbrains plugin setup itself? ๐Ÿ˜› Especially for iOS I dont understand how to.
c
Just run something like
./gradlew :linkDebugFrameworkIosArm64 --scan
in the terminal
๐Ÿ™ 1
I had the same issue, and maybe this is because you were using metadata (I assume, I don't know the exact name) sourceSets like nativeMain, which is different from real native sourceSets like iosArm64Main.
Copy code
kotlin.sourceSets.configureEach {
  languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
}
solved the problem.