Search code examples
androidioskotlin-multiplatformkotlin-multiplatform-mobile

KMM: How to reference the shared module into an already existing iOS project


I have followed the KMM hands-on tutorial on how to build a sample app with KMM and I was able to complete all the steps successfully! (Yu-huu!) Now I am trying to do a small POC in the production application. So I created a new KMM shared module and added it to the Android project. For the Android part it works fine, but I can't figure out how to include the shared module into the iOS app.

According to this link, the iOS project directory should be referenced in the gradle.properties file:

xcodeproj=~/iOSProjects/TestKMM

but when I try to import the shared module, then xcode complains that there's no such module:

enter image description here

So I suppose that simply referencing the iOS project in the gradle.properties is not enough. There must be something else that I am missing.


Also, I checked the build directory of the shared module, and as far I can tell, there's no iOS artifact generated. (This is different from the hands-on tutorial project, where a bin/iosX64/debugFragmework/shared.framework/ .... exists.)

enter image description here


Solution

  • So I suppose that simply referencing the iOS project in the gradle.properties is not enough.

    Correct. The hands-on does not appear to describe how you'd actually integrate with Xcode. We generally use cocoapods to make this all a little simpler, but in the hands-on, you need to run :shared:packForXCode from the gradle command line to build the actual framework (you could maybe run it from Android Studio).

    That should build a framework in your build dir. Look at the Xcode project in the hands-on. You will see a linked framework pointing at the KMM framework. In your prod Xcode project, you'll need to add that. In the hands-on, look in the build scripts for the one that runs gradle. You'll want to carry that over to your prod Xcode project (see here).

    To manually set all that up, you'll need a pretty solid understanding of Xcode config.

    If your prod Xcode uses Cocoapods, I'd suggest that route (and self-promo KaMP Kit as an example).