https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
r

Richie Rodríguez

03/04/2024, 9:50 AM
Hi! Hope you all are doing well. I'm integrating an existing iOS app with an existing Kotlin Multiplatform project. That project already includes iOS support. I'm following the guide at https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-integrate-in-existing-app.html#make-your-cross-platfo[…]pplication-work-on-ios . Once all steps were finished I was able to compile the KMP project from Xcode and to import the project into iOS code but... I can't import any element inside de project. I mean, it seems that I am importing an empty framework into the iOS code. It would be great if you could give me some pointers on what things I could check to make sure I've followed the guide steps correctly and find my mistake.
r

Robert Munro

03/04/2024, 12:18 PM
does the generated
shared.xcframework
have your functions in the header file?
r

Richie Rodríguez

03/04/2024, 2:27 PM
Not exactly the same. Under
core/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)/core.framework/Headers
, I get a
core.h
headers file with the definitions (not shared, but core project). For example, Xcode complains about
CoreBuild
and I can see
[...] @class [...], CoreCoreBuild, [...]
.
r

Robert Munro

03/04/2024, 5:02 PM
is that your iOS app? - you should have a shared framework that you add to the iOS app in Frameworks, Libraries .. section which would be a xcframework build the KMM module. not sure how you build but for me in the shared build folder
e.g.
shared/build/XCFrameworks/release/shared.xcframework
r

Richie Rodríguez

03/04/2024, 10:25 PM
Sorry, Robert, I didn't understand you. My "shared" code is call "core" and it is configured to export a single platform framework:
core/build/xcode-frameworks/Debug/iphonesimulator17.0/core.framework
. I can update the
build.gradle.kts
script to export an XCFramework. I will let you know. Thank you very much for your suggestions.
r

Robert Munro

03/08/2024, 11:21 AM
Sorry I got waylaid. Sure I have only ever used the xcframework approach. i use Touchlab's
kmmbridge
to do the build. which works quite well for me.
r

Richie Rodríguez

03/14/2024, 11:47 AM
In Android Studio Hedgehog | 2023.1.1 Patch 2 I can see the generated frameworks after Xcode compiles the core library. See attached screenshot. But then complains about missing types:
Copy code
AppData -> Cannot find type 'CoreBuild' in scope
If I check the headers file
core.h
, I can find the missing types:
Copy code
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("CoreBuild")))
@interface CoreCoreBuild : CoreBase
I run
Copy code
if [ "YES" = "$OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED" ]; then
 echo "Skipping Gradle build task invocation due to OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED environment variable set to \"YES\""
 exit 0
fi
cd "$SRCROOT/../.."
./gradlew :core:embedAndSignAppleFrameworkForXcode
To build the core library Then, in Build Settings -> Search Paths I have added the
$(SRCROOT)/../../core/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)
Path to Framework Search Paths and,
$(SRCROOT)/../../core/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)/core.framework/Headers
Path to Header Search Path. If I delete de build folder, the artifacts are reproduced again after running the build script from Xcode. Everything seems correct. Obviously I’m missing something. Do I have to include a dependency to my core library in the iOS project Podfile? https://kotlinlang.slack.com/docs/T09229ZC6/F06PWMHDRA5
r

Robert Munro

03/14/2024, 12:54 PM
Have you included the shared library at the top of your file?
For podfile builds you might have to make a cocapods lib. But not certain. But you should be able to add your shared xcframework in you project settings.
r

Richie Rodríguez

03/15/2024, 10:17 AM
I have finally found the issue (I admit I feel a little embarrassed for not realizing it sooner). I added the search path to the Xcode's project, but not the project itself. I added the framework to the Xcode project using "General -> Frameworks, Libraries, and Embedded Content" and it worked. I also tried to modify "Build Settings -> Linking -General -> Other Linker Flags", adding
-framework core
and it worked too. But... I'm afraid it's not finished yet. As the framework may be created under different paths:
$(SRCROOT)/../../core/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)
I can't just add the framework to "General -> Frameworks, Libraries, and Embedded Content". And, afaik, if I just add
-framework core,
I have to manually copy de framework to my app bundle before distributing it. Am I correct? How can I add the framework to "General -> Frameworks, Libraries, and Embedded Content" using environment variables?
r

Robert Munro

03/15/2024, 1:56 PM
In my projects usually the ios app and the shared module are in the same repo so relative path addressing works. If they are in different repos then maybe you might have make a copy task to copy the directory somewhere or possibly publish it somewhere xcode can find it. I wouldn't use an env variable to point to a library as it might vary for other developers and make the build brittle, also for CI. If i'm understanding your correctly your app should build now?
simplest is check out both repos to a common dir parent and then you can relative address.
r

Richie Rodríguez

03/17/2024, 11:17 AM
The shared module (named
core
in our project) and the iOS application are in the same repo, and I use a relative path to indicate it in the Search Path in Xcode. I'm afraid my days as a C developer are further behind than I'd like to admit, but I understood that by linking it, I was indicating that the said library (framework in this case) should be available on the target device. My knowledge in this area of Xcode is not sufficient to understand exactly what the option
-framework core
does (that's the name of the 'shared' module in this project). That's why I thought it would be necessary to copy it to the iOS application bundle.
r

Robert Munro

03/17/2024, 11:35 AM
You shouldn't have to copy the build will do that while running.
This is what I meant for including the framework, you can see shared. xcframework is included here. You have to use add other and nav to the build folder
3 Views