https://kotlinlang.org logo
Title
j

Jakub Sieprawski

04/27/2023, 3:22 PM
Hey, I am working on multi module application for iOS and android. My project include 4 modules: 2 KMM (messenger, mainModule) and 2 native ones (biometry, faceCheck). Messenger is used across all other modules in project. When I am trying to consume Messenger’s common code (KMM module) in different KMM module I have to add .aar/.xcframework file to native parts, create service for each architecture and use it, but doing it this way I need to duplicate common code for each architecture. Is there any other way of using common code of KMM module in different KMM module? Is it possible to use common code of KMM module in common code of different KMM module?
v

Vsevolod Ganin

04/27/2023, 4:55 PM
Is it possible to use common code of KMM module in common code of different KMM module?
Yes, it should totally work. Are you connecting dependency like so?
// build.gradle.kts

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation(project(":messenger"))
            }
        }
    }
}
j

Jakub Sieprawski

05/04/2023, 9:03 PM
Thank you for answer and sorry for late reply. Messenger module has 2 methods: send, register. Send allows you to send message to registered receivers and register allows you to register callback as a receiver. I am connecting Messenger to mainModule the same way like you have shown. After doing so everything works fine on android. On iOS I can see and use contents of messenger module in mainModule, but if I will send message from mainModule using method from messenger I am not receiving it in any other module even though it is the same reference of messenger .xcframework. When I am connecting messenger to mainModule through native parts (Cinterop) everything works fine. Doing so I have to multiply common code of mainModule for every architecture like I said at the beginning. Is there something I am not seeing or doing wrong? Should adding by project tag work this way?
t

Trevor Stone

05/04/2023, 10:00 PM
In your KMM module are you specifying all supported platforms in gradle? Kotlin doesn't support just a generic "kotlin" library. You need to add each target in gradle you want to support even if you only are writing code in commonMain
j

Jakub Sieprawski

05/05/2023, 12:02 PM
Yes, i am specifying all supported platforms. First photo contains my project structure (supported architectures), on second one you can see mainModule source set configuration. I am adding messenger to every architecture using "project" tag. Doing so I can see and use contents of Messenger on every architecture without any errors but when I generate final binaries for both modules - .xcframework and try to test it on swift app, it is not working. I mean I am adding messengerModule, mainModule as dependencies to swift testApp, next I am registering callback in test app to receive messages using messengerModule. Next I am starting process using mainModule, which should send message using messengerModule and I am not receiving it in test app callback, even though it's the same reference of messengerModule. I really don't understand why. On third photo you can see other way of using messengerModule in mainModule, I am adding messengerModule as dependency using C-Interop. Doing so I can see and use contents of messenger just in native architectures, I cannot use it from commonMain what results with multiplying code. But when I am testing it from swift app, and doing same process as described above I am receiving messages in testApp callback sent from mainModule using messengerModule. Can someone explain why I am not receiving messages in swift testApp callback from messenger used in mainModule by "project" tag even though it is the same reference? Am I doing something wrong?