KMP-NativeCoroutines icon indicating copy to clipboard operation
KMP-NativeCoroutines copied to clipboard

Ignore Jvm annotations

Open DatL4g opened this issue 9 months ago • 4 comments

[!CAUTION] Declaration annotated with '@OptionalExpectation' can only be used in common module sources

A method like this:

@JvmOverloads
@NativeCoroutines
open fun load(
    key: CharSequence,
    retry: Boolean = true
): Flow<String> = flow { emit("Hello World") }

Will result in the following:

@JvmOverloads
@ObjCName(name = "load")
public fun Test.loadNative(key: CharSequence, retry: Boolean): NativeFlow<String> = load(key, retry).asNativeFlow(null)

The above error is basically caused by the @JvmOverloads (same with @JvmStatic probably) annotation, can you just ignore/remove it when generating code with KSP, so it looks like this then:

@ObjCName(name = "load")
public fun Test.loadNative(key: CharSequence, retry: Boolean): NativeFlow<String> = load(key, retry).asNativeFlow(null)

DatL4g avatar Mar 01 '25 16:03 DatL4g

Hi! Could you try the k2Mode and let me know how that works for you?:

// build.gradle.kts
nativeCoroutines {
    k2Mode = true
}

In K2 mode only some specific annotations are copied and all others are ignored.

rickclephas avatar Mar 02 '25 11:03 rickclephas

@rickclephas K2 mode does not seem to work. It does not generate any sources, tested with KSP1 and KSP2.

As soon as K2 mode is turned off, it generates the sources again but with the error.

DatL4g avatar Mar 02 '25 14:03 DatL4g

The K2 mode doesn't use KSP anymore. It generates the required declarations with a Kotlin Compiler plugin. With the compiler plugin you won't see generated source files, but the declarations should be there if you try to use them from Swift. If that isn't the case could you share some more details about your setup (Kotlin and dependency versions) and any relevant logs?

rickclephas avatar Mar 02 '25 15:03 rickclephas

Got it, it's working now!

And it's even better for my use-case since I use it in a library and KSP causes some trouble when publishing sometimes. You can close this issue if you want (or not), not sure if you still want to fix the KSP part then.

DatL4g avatar Mar 02 '25 17:03 DatL4g

FYI K2 mode is now the default and KSP support will be removed in the next release, so this should no longer be an issue.

rickclephas avatar Nov 09 '25 14:11 rickclephas