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

6

9

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

Venegas answered 11/1, 2021 at 17:17 Comment(1)
Hi have you succesfully solved this problem? really appreciate if you can share some articles or resources about it. thanks!Chico
C
6

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).

Clapperclaw answered 11/1, 2021 at 17:31 Comment(1)
Hello Sir, i would like to ask something. I have been following kotlin multiplatfrom from experimental phase, and we used to run the packForXcode. now i am facing missing arm64 for simulator build in m1 mac. Do you have any idea how to solve. this problem? thanksChico
B
5
  1. Go to your terminal
  2. Run ./gradlew tasks
  3. Find the task with name like linkDebugFrameworkIos
  4. Run ./gradlew linkDebugFrameworkIos

Now you have your shared module in your workspace which you opened via Xcode

Breana answered 4/1, 2022 at 11:29 Comment(1)
./gradlew linkDebugFrameworkIosArm64 helped me on MacAirArdell
I
4

Kotlin Multiplatform (KMP) has come a long way since this question was asked, but the basic problem remains.

As @Kevin noted in the accepted answer:

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

Fortunately, the details are covered in the KMP tutorial Make your Android application work on iOS, and particularly in the part Connect the framework to your iOS project. The method described there consists of seven arcane (but easy to follow) steps for changing the iOS project settings.

It does not make sense to duplicate them here, and as long as they support KMP, JetBrains presumably has an incentive to keep that page (or its successor) up to date.

But in summary, you need to add a script invoking a Gradle task that will build the 'shared' framework before the 'Compile Sources' phase, add a 'Framework Search Path', specify a 'Linker Flag' and disable 'User Script Sandboxing'.

Impeditive answered 15/12, 2023 at 10:30 Comment(1)
In my opinion, this should be marked as the answer.Ulani
F
3

Since kotlin 1.5.20 you can use embedAndSignAppleFrameworkForXcode outlined here.

https://blog.jetbrains.com/kotlin/2021/07/multiplatform-gradle-plugin-improved-for-connecting-kmm-modules/

The cocoapods approach can be overkill for small projects, and the packForXcode often fails to rebuild on shared code changes or sim/device switching, making it a pain to use for iOS developers.

Farm answered 29/12, 2021 at 15:32 Comment(0)
I
0

I also faced this. I fixed it by updating my JDK from 1.8 to 11.

Isidoro answered 2/4, 2023 at 22:34 Comment(0)
K
0

according to this documentation

In the Xcode Run Script build step, change packForXcode to embedAndSignAppleFrameworkForXcode and remove all the passed flags. The task will use the Xcode environment configuration and build only the needed artifact automatically.

./gradlew :shared:embedAndSignAppleFrameworkForXcode
Klondike answered 24/2, 2024 at 7:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.