Joel Denke
02/19/2024, 9:26 AMThis 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 ...Joel Denke
02/19/2024, 9:27 AMChanjung Kim
02/19/2024, 1:59 PMJoel Denke
02/19/2024, 2:12 PMfun 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.Joel Denke
02/19/2024, 2:13 PMChanjung Kim
02/20/2024, 10:32 AM-opt-in
should work, but if the compiler still emits the warning, try optIn.addAll
instead of freeCompilerArgs.addAll
. Mine is like the following:
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")
}
}
}
}
Joel Denke
02/20/2024, 10:34 AMJoel Denke
02/20/2024, 10:35 AMChanjung Kim
02/20/2024, 10:51 AMtest
, 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:
arrayOf(
iosX64(),
iosArm64(),
iosSimulatorArm64(),
mingwX64(),
linuxX64(),
).forEach { target ->
target.compilations.configureAll { ... }
}
Joel Denke
02/20/2024, 1:05 PMJoel Denke
02/20/2024, 1:07 PMtargets.configureEach {
sourceSets.all {
languageSettings.optIn("kotlin.ExperimentalUnsignedTypes")
}
Joel Denke
02/20/2024, 2:05 PMChanjung Kim
02/20/2024, 2:20 PMProject.afterEvaluate
? Try setting that optIn in the gradle script directly, not in your plugin.Chanjung Kim
02/20/2024, 2:21 PMJoel Denke
02/20/2024, 2:23 PMChanjung Kim
02/20/2024, 3:30 PMJoel Denke
02/20/2024, 3:31 PMChanjung Kim
02/20/2024, 3:40 PMkonanc
by passing --scan
argument to gradlew
. You can check whether -opt-in is actually passed or not to the compiler.Joel Denke
02/20/2024, 3:48 PMChanjung Kim
02/21/2024, 3:50 AM./gradlew :linkDebugFrameworkIosArm64 --scan
in the terminalChanjung Kim
03/07/2024, 3:08 AMkotlin.sourceSets.configureEach {
languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
}
solved the problem.