Hello everyone, I have a problem. I'd be very grat...
# multiplatform
h
Hello everyone, I have a problem. I'd be very grateful if you could share the source video or your own experiences. We have a CMP project, and we're integrating Paywall. I'll define it as native on Android and iOS. We're using it with the Expect Actual structure in the Common Module. We write the native code for Paywall and IAP in iOS and call it with Cinterop in CommonMain on iOS. We want to use this structure as a module or lib in other projects. How can we do this? How do we import a CMP project into another project as a module or library and use it? The real problem was on the iOS side. When we imported the module, it worked fine on CommonMain on Android, but it couldn't find Cinterops on iOS. Do we need to configure a different Gradle configuration for iOS, such as xcframewrok, etc.? Or, how do we add the KMP module to the standard project and write iOSnative code there to use it in other projects? Thank you for your time.
a
Hi, If I understood correctly you want to introduce some Kotlin Multiplatform (with compose?) library and re use it in other KMP projects. For your reasons you have implemented this on native level. I.e. there is some objc/swift AppCode project that is used in kotlin. And on Kotlin Level you then align both IOS and Android to common API surface? So the question is how to publish (to local repo) such Kotlin Library, so that other projects can re-use it? Is it correct?
h
Hi Anton, yes — you’ve understood it correctly, thank you! We have a Kotlin Multiplatform (Compose-based) architecture, and in many cases we need to integrate platform-specific SDKs like Adapty (Paywall), Firebase, AdMob, Facebook Ads, etc. Since many of these libraries don’t offer official KMP support, we implement native bridges: • In androidMain, we use native Android SDKs • In iosMain, we use Swift/ObjC via cinterop • Then expose a unified API in commonMain via expect/actual In one project, this setup works fine. Now I’d like to extract this setup into a reusable KMP module and use it in other KMP apps — either as a standalone module or a local Maven/Gradle library. The main issue is on iOS: When I import the shared module into another project: It works on Android On iOS, it fails to locate cinterop bindings and native symbols (especially for Swift/ObjC-based SDKs inside iosMain) So my questions: What’s the correct way to publish or reuse such a KMP module with native iOS code? Do I need to build something like an XCFramework for iOS side? Or is there a way to package the module (including cinterop + Swift/ObjC) so that another KMP project can simply depend on it? Thanks again — any advice, patterns or working examples would be a huge help
a
Yes, that is a very common problem. How to publish library with cinterops and their related binaries. However, I'm not sure that it is possible to distribute native binaries as is. So it might be a pre-requirement to have these binaries ready before adding a dependency to your custom module. I cant check this right now as I currently dont have my laptop next to me. Pergaps @Andrey Yastrebov can clarify the details. As he recently worked with CocoaPods integrations. I see one possible way to workarpund this. Write your own Gradle Plugin, that will integrate with KMP. for example: It can expose custom DSL to add a dependency to your paywall module. like this:
Copy code
plugins {
  kotlin("multiplatform")
  id("your.company.kmp-modules-kit") // this is your plugin

kotlin {
  // targets and other configurtions
  
  kmpModulesKit { // your plugin adds this extension 
    addPaywall() 
  }
and addPaywall function call, should configure everything required. i.e. add paywall dependency to commonMain source set just like you do right now by hand. And provide necessary binaries. OR configure cinterops again. It depends what is convinient for you. but In general I encourage to take deeper look on custom Gradle Plugins as conventions for your projects. As it should help with bootstraping new gradle projects. Like by applying one or two reusable conventional plugins they would configure KMP targets, cinterops and other boiler plate code.
a
Hi Hasan, you need to link your main module with native cinterops. You can refer to this sample, I think it has similar setup https://github.com/Kotlin/kmp-with-cocoapods-compose-sample
a
But the problem is that KMP project with CInterops and native binaries should be published somewhere. To be consumed in other projects as external dependency not as a Gradle Project.
true 1