Fix - Fix all kmp generation from commonMain to native side#258
Conversation
…then relay on native module generation
| class AppModule | ||
|
|
||
| @Module | ||
| @ComponentScan("com.jetbrains.kmpapp.native") |
There was a problem hiding this comment.
If I try to component scan the same exact package in both common and platform specific source set(Android) the dependencies in Common are scanned but platform(Android) are not...
There was a problem hiding this comment.
It will only scan for current sourceSet, which is normal.
@Module
@ComponentScan("com.jetbrains.kmpapp.native")
class NativeModuleA()This module will scan for annotated components in common. this will detect this:
@Factory
expect class PlatformComponentA() {
fun sayHello() : String
}Each platform has an actual implementation.
Then it doesn't scan directly for a given sourceSet
There was a problem hiding this comment.
I see, in my project I have defined an interface in common and implementations in each source set(that are annotated). The package path I provide to component scan has annotated classes that need to be scanned in both common and platform specific derictories. For the desktop sources set it seems to work as expected (both common and desktop annotated classes are recognized by Koin) but for Android the scanning doesn't seem to be working correctly, is this something we can expect to have support for in the future?
There was a problem hiding this comment.
@arnaudgiuliani I think I have the same issue as @Anthony17serrato , I'm not able to make it work for Android where I annotate modules in both common, and android (and ios) source sets with ComponentScan because all of these modules have its own set of dependencies - common ones and platform-specific ones (in the same package), with a combination of using impl/interfaces and expect/actual classes (depending on use-case). Such scenario was working with Koin Annotations 2.0.0; now reading the docs I have trouble to make it compile with the recent versions.
There was a problem hiding this comment.
I updated this doc: https://insert-koin.io/docs/reference/koin-annotations/kmp#sharing-patterns
does it help? @krzdabrowski
KMP expect/actual is now using the right level of data, at right source level. Here is an extract of the updated doc, to help understand sharing patterns.
Also added
example-cmplocal app to maintain this.Declaring Common Modules
In your commonMain sourceSet, you just need to declare a Module to scan the package that will have native implementations of your expect class or function.
Below we have a
PlatformModule, scanning incom.jetbrains.kmpapp.platformpackage where we havePlatformHelperexpect class. The module class is annotated with@Moduleand@ComponentScanannotations.Using Modules and Expect/actual for Kotlin Native Components
in a Kotlin Multiplatform application you will need to have specific implementation per platform on some components. You can share those components at definition level, with expect/actual on the given class.
Or you can share an entire module, with expect/actual on the class module.
Sharing Definitions - Scanning for expect definitions
From your commonMain code sourceSet, you can scan for expect classes that will have their own implementation on each native platform. Be aware to use
expect/actualdefinitions, even on constructors.In commonMain:
in native sourceSets:
Sharing Definitions - Module with expect definition function
From your commonMain code sourceSet, you can define an expect class definition with their own implementation on each native platform. Be aware to use
expect/actualdefinitions, even on constructors.In commonMain:
in native sourceSets:
Sharing Module - expect/actual Module
In commonMain:
in native sourceSets:
:::note
Your module needs to not have any definition from the commonMain source set, to be considered as used only on the platform.
:::